tag:blogger.com,1999:blog-18533887933638733572024-03-06T06:15:13.789+01:00esbenaIncreasing productivity by using the right tools.Esben Andreasenhttp://www.blogger.com/profile/11470363690410827426noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-1853388793363873357.post-50751078437746584912010-11-30T17:31:00.000+01:002010-11-30T17:31:12.258+01:00Writing Java effectively in a manually compiled DSL<h2>Introduction</h2>
When I write Java, I write in a DSL similar to Java which is compiled to Java manually using the UI-actions of the Java-plugin for eclipse. If I ever change IDE, I have to learn a new DSL from scratch. Let me explain:
<br/><br/>
For me, the Java language and the Eclipse IDE with the Java plugin forms a new language. The language I program in _is_ Java, but not if you look at my keystrokes. The Autocompletion, Refactorings, Quick Helps and other automated source code changing tools provided by Eclipse (henceforth Tools) allow me to program at a much higher level than pure Java. Three years of experience with Java/Eclipse allows me to leverage the Tools a lot.
<br/><br/>
I almost never declare anything (variables, parameters, fields, methods, constructors, classes)! Instead, I rely on the Tools to do that for me. The lack of declaration will be the focus of the examples in this post.
<h2>UI-actions</h2>
In the following examples I will be using the UI-actions of Eclipse a lot, they will be mentioned by the name you see in:
<pre class="myplain">Windows -> Preferences -> General -> Keys.</pre>
I will always be using keyboard shortcuts to invoke them. A UI-action invocation will be denoted in brackets: e.g. <span class="myplain">[Quick Fix]</span> A sequence of UI-action invocations will be comma separated in a pair of brackets: e.g. <span class="myplain">[Next, Quick Fix]</span>.
<br/><br/>
The cursor position will be denoted by <span class="myplain">_</span>.
<br/><br/>
Specific keyboard key presses will be denoted by capital letters in braces: e.g. <span class="myplain">{ENTER}</span>.
<h2>Example 1</h2>
Scenario: need to invoke the method "Qux foo(Bar, Baz)", and store the result in a local variable.
<br/><br/>
I type in "foo;" and move the cursor one place to the left:
<pre class="mycode">foo_;
</pre>
<span class="myplain"> [Content Assist] </span> will immediatly give me:
<pre class="mycode">foo(bar, baz);</pre>
Eclipse will now complain about the missing variables "bar" and "baz". I then invoke: <span class="myplain">[Quick Fix, {ENTER}, Next, Quick Fix, {ENTER}]</span>. Both times Quick Fix will suggest to introduce the variables "bar" and "baz", I let them be introduced. This will automatically import the right version of Bar and Baz.
<pre class="mycode">
Bar bar;
Baz baz;
foo(bar, baz);
</pre>
Eclipse will still complain, this time about uninitialized variables, we need to [Save] in order to write our changes to the underlying AST, which all the UI-actions operate on. Once this is done we use: <span class="myplain">[Quick Fix, {ENTER}, Next, Quick Fix, {ENTER}, {ENTER}]</span> to initialize the variables. Focus will be given to the initialized value, allowing an immediate definition of the value, but I prefer to finish the task at hand first:
invoking foo.
<pre class="mycode">
Bar bar = null;
Baz baz = null;
foo(bar, baz);
</pre>
Finally the return value of foo should be stored in a variable, and as we still are on the line of the invocation: <span class="myplain">[Quick Assist - Assign to local variable, {ENTER}]</span> will do just that.
<pre class="mycode">
Bar bar = null;
Baz baz = null;
Qux foo = foo(bar, baz);_
</pre>
<h2>Comparison</h2>
This is definitely not a complete trick you should try to remember. It consists of a lot of small steps, and a lot of experience with the Tools of Eclipse. But it is certainly worth trying to understand the steps involved in the process. For me it is worth it for two reasons: I type less, and I keep focus at the task at hand instead of being bugged down in details about declaring a variable of the right type.
<br/><br/>
As a meassure of the "less typing" proposition, let's count the number of keystrokes needed to write in a completely naive way (without even using autocompletion or autoformatting) and compare that with the steps just described.
<h4>Naive way</h4>
<pre class="mycode">
Bar bar = null;
Baz baz = null;
Qux foo = foo(bar, baz);_
</pre>
Contains 56 characters including whitespace, thus 56 key presses. For simplicity, I assume that the Bar, Baz and Qux types are imported already.
<h4>My way</h4>
Initial typing:
<pre class="mycode">
foo_;
</pre>
contains 4 characters and one cursor move: 5 key presses.
The rest: <span class="myplain">[Content Assist], [Quick Fix, {ENTER}, Next, Quick Fix, FNTER}], [Save], [Quick Fix, {ENTER}, Next, Quick Fix, {ENTER}, {ENTER}], [Quick Assist - Assign to local variable, {ENTER}]</span>.
<br/><br/>
Now, my keyboard bindings to UI-actions are very customized, so you have to trust me here. I do not consider modifiers (ctrl, alt) a key press, as they are done at the same time as the eeypress they modify, and as they are done by my thumbs which rests above the modifier anyway.
<table border="2" cellpadding="6" cellspacing="0" frame="hsides" rules="groups"><col align="left"></col><col align="right"></col><col align="right"></col><col align="right"></col> <thead>
<tr><th>UI-action</th><th>Key presses</th><th>Times used</th><th>Toal key pressses</th></tr>
</thead> <tbody>
<tr><td>"foo_;"</td><td>5</td><td>1</td><td>5</td></tr>
<tr><td>[Content Assist]</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>[Quick Fix]</td><td>1</td><td>4</td><td>4</td></tr>
<tr><td>{Enter}</td><td>1</td><td>6</td><td>6</td></tr>
<tr><td>[Save]</td><td>1</td><td>1</td><td>1</td></tr>
<tr><td>[Next]</td><td>1</td><td>2</td><td>2</td></tr>
<tr><td>[Quick Assist - ...]</td><td>2</td><td>1</td><td>2</td></tr>
</tbody> <tbody>
<tr><td></td><td></td><td></td><td>21</td></tr>
</tbody> </table>
21 key presses versus 56 key presses, that's a huge saving!
<h2>Example 2</h2>
Extending the previous example, I present one more example: I want to instantiate an object of a subtype of Baz, which depends on a Bar object.
<pre class="mycode">
Bar bar = null;
Baz baz = null;
Qux foo = foo(bar, baz);_
</pre>
I start by trying to instantiate a subtype of Baz instead of the null value:
<pre class="mycode">
Bar bar = null;
Baz baz = new BazSubtype(bar)_;
Qux foo = foo(bar, baz);
</pre>
Eclipse will complain about the missing type BazSubtype. We generate it: <span class="myplain">[Quick Fix, {ENTER}, {ENTER}]</span>
<pre class="mycode">
public class BazSubtype implements Baz {
}
</pre>
Our focus is now the newly generated class BazSubtype which implements Baz, but we need to implement the constructor: we go back to the previous editor and make Eclipse generate the
constructor for us: <span class="myplain">[Next Editor, Quick Fix, {Arrow Down}, {Enter}]</span>
<pre class="mycode">
public class BazSubtype implements Baz {
public BazSubtype(Bar bar) {
// TODO Auto-generated constructor stub
}
}
</pre>
Eclipse complain about the missing type Bar, as it wasn't imported automatically (which is odd!) so we import it: <span class="myplain">[Organize Imports]</span>.
<br/><br/>
We want to assign the parameter to a field. We move the focus to the parameter and generate the field and an assignment: <span class="myplain">[Quick Assist - Assign parameter to field]</span>
<pre class="mycode">
public class BazSubtype implements Baz {
private Bar bar;
public BazSubtype(Bar bar) {
this.bar = bar;
// TODO Auto-generated constructor stub
}
}
</pre>
The "// TODO " should now be removed, and the field should be used for something, such as <span class="myplain">[Generate getters and setters], [Generate hashCode() and equals()]</span> or perhaps some custom code.
<br/><br/>
If we save this class, and go back to the class we started in, we should have completed the task. <span class="myplain">[Save, Next Editor, Save]</span>
<br/><br/>
Apart from navigating to the "bar" parameter and typing the initial constructor invocation, 14 keystrokes are used to complete the task. It is left as an excercise for the reader to compare this to a more manual way (not necessarily completely naive, like before).
<h2>Final remarks</h2>
Now it could be claimed that these examples are contrived (they are). But I assure you that this heavy use of UI-actions (and proper bindings for them!!) really boosts my productivity when coding Java.
<br/><br/>
This way of coding removes a lot of the boilerplate code which is often associated with Java. I hardly ever type the name of the Types in my programs, thus keystroke-wise my Java DSL becomes a language with stronger type inference than regular Java.
<br/><br/>
In general, the <span class="myplain">[Next, Quick Fix, {Enter}]</span> combination is incredible effective, but it requires that you know _what_ Quick Fix will suggest as the first action to perform - this requires usage experience. Using <span class="myplain">[Next, Quick Fix]</span> followed by an inspection of the suggestions is the way to get that experience.
<br/><br/>
It should be noted that <span class="myplain">[Quick Fix]</span> isn't the only UI-action you should learn, but it is the one you can use most often. Others, such as <span class="myplain">[Generate getters and setters], [Generate hashCode() and equals()]</span> generate a lot of (bug free!) boiler plate, but are not used as often. You should look through the list of available actions in the categories Quick Assist, Refactor and Source, you _will_ be surprised about the amount of usable actions!Esben Andreasenhttp://www.blogger.com/profile/11470363690410827426noreply@blogger.com0tag:blogger.com,1999:blog-1853388793363873357.post-38502642817499340912009-11-26T12:35:00.012+01:002009-11-26T17:02:42.768+01:00Emacs screencast: rectanglesI've just made a <a href="http://www.youtube.com/watch?v=yrHki_pEgQI">screencast</a> about using rectangles in Emacs, so I figured I should explain how I made it.<br />
<br />
I made it with <a href="http://www.emacswiki.org/emacs/ScreencastMode"> ScreencastMode</a> for Emacs - which I happen to be the author of.<br />
With it, you can create a screencast by writing the manuscript for the screencast in a mix of natural language and elisp. The playing of the screencast itself is handled by Emacs - by stepping through the manuscript at pace equivalent to as if a human had done them. The video-recording at youtube is done using <a href="http://www.emacswiki.org/emacs/ScreencastRecord">this</a>. <br />
The mode supplies utility functions for the producer such as pauses, slow typing, and blinking regions. <br />
<br />
A simple example screencast file looks like: <br />
<table border="1"><tbody>
<tr> <td><pre>(require 'screencast)
(screencast
'(
"This is the message-buffer."
(i "This is the command-buffer.")n
"I will now show you how to save the command-buffer:"
(save-buffer)
)
"sample"
1.1
)
</pre></td> </tr>
</tbody></table>Which results in the following buffer output when the screencast has been played to the end. <br />
<table><tbody>
<tr><td><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-_2m-bTjkWLwgnznCYOQ0r-dN20AKMxymi9yyucyUeoJBbLNw3Eyzj3qwhN0et1hdFcJTXP13uscb8djRcwokKkGR4CPl8J6hveutgZRffTLjXOVDSCkgv1KAmdfHIDBht3Xpx7MmQY/s1600/2009-11-26_13:09:55_1280x800.png" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5408395194649785986" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK-_2m-bTjkWLwgnznCYOQ0r-dN20AKMxymi9yyucyUeoJBbLNw3Eyzj3qwhN0et1hdFcJTXP13uscb8djRcwokKkGR4CPl8J6hveutgZRffTLjXOVDSCkgv1KAmdfHIDBht3Xpx7MmQY/s400/2009-11-26_13:09:55_1280x800.png" style="float: left; margin: 0pt 10px 10px 0pt;" /></a> <br />
</td></tr>
</tbody></table>Notice how the message-buffer contains the description of the commands done on the command-buffer. That is the way it works: the message-buffer describes what is going on in the command-buffer <br />
<br />
More screencast sources can be found <a href="http://www.emacswiki.org/emacs/ScreencastSources">here</a>. <br />
<hr />Some critical questions has been asked about ScreencastMode, here are my attempts at answering them: <br />
<ul><li>Why not read the info pages, which contains more details?</li>
<ul><li>some people finds it easier to watch movies than reading static text.</li>
<li>screencast-mode <i>displays </i>the capabilities of a mode, instead of <i>describing</i> them.</li>
<li>those who just need to know how to use a mode doesn't need all the details of the info-page.</li>
</ul>
<li>If videos are superior, why not just create a regular video?</li>
<ul><li>a video is static, it's hard to change it if you discover an error - you'll have to do it in one take unless you got some video editing software. </li>
<li> the screencast sources can be edited by others, inviting collaboration (also it's a lot smaller size-wise) </li>
<li>the screencast will tell the user if his Emacs is configured right to use the feature it explains</li>
</ul>
</ul><hr /><hints id="hah_hints"></hints>Esben Andreasenhttp://www.blogger.com/profile/11470363690410827426noreply@blogger.com1tag:blogger.com,1999:blog-1853388793363873357.post-42279548844037816022009-11-25T19:21:00.012+01:002009-11-25T20:01:58.375+01:00Writing blog posts with EmacsI'll be writing all my blog posts with Emacs (using
html-mode), as Emacs is my favorite editor. But a big part
of why I use Emacs for this, when a fancy web editor already
is present in my browser, is the Firefox plug-in <a href="https://addons.mozilla.org/en-US/firefox/addon/4125">"It's All Text!"</a>.
This simple plug-in allows me to pop into Emacs from
any text box in Firefox, edit the content of the text box as
if it was a normal file, save it, pop back to Firefox and
watch the content of the text box change to what I just
wrote in Emacs! The only setup required was to point "It's
All Text!" to my editor program, and setup a shortcut to
activate it - and voila: seamless integration between
Firefox and Emacs. As a nice side effect, the texts are
saved locally, thus I don't have to worry about a browser
failure which usually would require me to write the text
from scratch once more.<br/><br/>
Before switching to "It's All Text!", I used <a href="https://addons.mozilla.org/en-US/firefox/addon/4141">Firemacs</a>.
Firemacs provides Emacs like bindings/shortcuts to the text
boxes of Firefox - but when it comes to editing longer texts
(such as this), then a fully fledged Emacs is superior to a
few bindings.Esben Andreasenhttp://www.blogger.com/profile/11470363690410827426noreply@blogger.com0tag:blogger.com,1999:blog-1853388793363873357.post-53317282118482145492009-11-25T17:56:00.007+01:002009-11-25T19:25:35.035+01:00Hello world!Being a computer science student, I had to make this my
first blog entry.
The future content of this blog will be about being
productive using the right tools. You can be guaranteed to
see Emacs, Eclipse and Zsh mentioned often. Posts about
shortcuts and general keyboard usage will also be present.Esben Andreasenhttp://www.blogger.com/profile/11470363690410827426noreply@blogger.com2