Eric Wendelin's complete blog can be found at: http://eriwen.com

Items:   1 to 5 of 24   Next »

Thursday, March 11, 2010

MooTools 1.2 Beginner's Guide coverI have liked the works of Jacob Gube of Six Revisions and Garrick Cheung of the MooTools Community Team, so when Packt Publishing wanted me to review their book, I accepted.

I hope authors, as well as readers, will gain some insights. Here is my review of MooTools 1.2 Beginner’s Guide.

Book Structure

The book basically takes you, step-by-step, through downloading the MooTools library parts and building simple examples with HTML, CSS, and, of course, MooTools. It seems to touch on many of major parts of MooTools: Core, DOM selection, Events, Ajax, and Fx. Finally, it introduces you to MooTools More and how to write your own MooTools plugins. it approaches everything with very simple, very “hands-on” examples. There are some deviations (like pop quizzes), but they are few.

The examples themselves generally introduce a topic, give an HTML/CSS template and then progressively fill in the MooTools bits.

The Good

You can tell that the authors are bloggers by their very informal writing style. They know how to use simple language so that their writing does not impede the learning ability of the reader. I was able to read the entire book in about 3 hours cover-to-cover, a testament to how easy-to-read it is.

This is a book that a real, never-coded-a-web-page-before dude(tte) could pick up and do something with immediately.

The Bad

It truly is a beginner’s guide, and having some sort of MooTools knowledge myself I was annoyed by the verboseness of the examples. There seemed to be a lot of times I was seeing a small code example stretched out to several pages of text.

I was slightly disappointed by the number of linguistic (spelling and grammar) mistakes throughout the book. These were obviously not cultural differences between the UK-based Packt and the US. I was always able to understand what the writer meant, but I kept getting hung up on the mistakes because that’s the kind of asshole I am ;)

I really missed a good reference or appendix section at the end where I could find links to more information about the parts of MooTools I was learning about. There was some stuff within the chapter texts, but not enough.

You should buy this book if…

… you are truly a beginning web developer. This book is a great introduction to web development with a core focus on MooTools. It seems to generally promote good web development practices in my opinion.

Conversely, this book is not great to use just for reference. If you want to lookup anything advanced, it’s not going to be your best bet.

MooTools 1.2 Beginner’s Guide »

Related posts:

  1. Guest Post: Save Text Size Preference Using MooTools and PHP
  2. Create a Color Palette Using CSS and MooTools 1.2
  3. Lessons learned from the SCWCD


Thursday, February 25, 2010

I had a great 2-and-a-half year journey with Sun, but my time there is sadly at an end. I was very fortunate to have worked with some very, very smart people. We’ll certainly keep in touch.

I worked on some really cool internal (and therefore not shareable) projects in Java, Jython, and Web languages and platforms. They were generally fun, diverse and challenging.

On to Return Path!

Return Path logoI’ll now be joining a small, agile team at Return Path. We will make email safer, and (I hope) win the war over spam. Audacious yes. Impossible… I think not.

I wish all my Sun/Oracle counterparts well. Good luck!

Related posts:

  1. Site monitoring with Python and cron
  2. Continuation-passing and tail call elimination in Javascript


<p>Those who pair program with me know that I&#8217;m an advocate of leaving comments like <code>// FIXME: Breaks layout on tablets</code> or <code># TODO: Extract to model</code>.
Little reminders of things I don&#8217;t want to forget before, say, pushing a new feature.</p>

<p>I have a non-trivial amount of tooling I use around these <em>task-oriented comments</em>, and I&#8217;ve been meaning to write about how I
use them. Here you go.</p>

<h2>My Approach</h2>

<p>Often while working on new features (and sometimes, bugs) I sketch out a plan of what I need to do in English with a collection
of comments. This helps me break the task into little pieces so I can make sure I don&#8217;t miss anything. It also has the benefit
of helping me discuss the general approach with my pair so they can consider better solutions.</p>

<p>Next, I distribute these comments <em>to the places where I&#8217;m going to code the implementation</em> (if possible); that way
they give the reader context so it&#8217;s easier to think about the pieces involved. I&#8217;ll write the implementation and tests,
adding comments as I think of them. I want to write these intermittent notes down quickly so I can forget about them
until I&#8217;m ready to address them.</p>

<p>Lastly, I like to write myself notes before taking long breaks or quitting for the day.
Comments like <code># TODO: Finish injecting response into view</code> help me to pick up right where I left off later.
No more awkward &#8220;Now what was I doing yesterday?&#8221; moments.</p>

<h2>Properties of good TODO comments</h2>

<p>This is not so hard, you just have to always write them as if someone else will read them.
Remember to <em>make it specific</em>. If it&#8217;s a bug, you might <em>explain why</em>, especially if someone else is going
to be fixing it. Perhaps you might briefly explain why something should be implemented a certain way.
Finally, <em>don&#8217;t go overboard!</em> If you can write the code in 30 seconds, why waste time telling yourself what to do.</p>

<h2>Improving task-oriented comments with tools</h2>

<p>You might be saying to yourself: &#8220;But the advantage of a real TODO list is that it&#8217;s in one place&#8221;. (OK maybe not, but go with me here, eh?)
&mdash; and you&#8217;d be right. That&#8217;s where our tools come in; the simplest of which is <a href="http://www.eriwen.com/tools/grep-is-a-beautiful-tool/">grep</a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'>egrep -R <span class="s2">&quot;(TODO|FIXME)&quot;</span> *
</span></code></pre></td></tr></table></div></figure>


<p>Editors like IntelliJ IDEA/RubyMine keep track of comments with keywords like this automatically. Just hit ⌘6 (Alt-6 on Windows/Linux)
and the aptly named TODO pane comes up. I actually like to add new patterns in IntelliJ&#8217;s TODO settings panel, to make FIXME comments stand out in red.
Sublime Text seems to have a handy plugin called <a href="https://github.com/robcowie/SublimeTODO">SublimeTODO</a>
that aggregates these keyword comments, as well. Leave a comment with other neat TODO-tracking tools.</p>

<p>We can also utilize <a href="http://git-scm.com/book/en/Customizing-Git-Git-Hooks">git hooks</a> to tell us anything we missed
so that we can either fix them before we push or add stories to our favorite agile task board. Here&#8217;s the git hook that
I use <em>pre-commit</em>:</p>

<script src="https://gist.github.com/5193583.js"> </script>


<p>All this does is print out any new <code>TODO</code> or <code>FIXME</code> comments. To use this git hook, just copy it to <code>.git/hooks/pre-commit</code>
in any repo (don&#8217;t forget to make it executable). It runs before every commit and you can prevent the commit by returning a
non-zero exit code. You can fork the <a href="https://gist.github.com/eriwen/5193583">gist here</a> for your own needs.</p>

<p>Finally, <a href="http://jenkins-ci.org/">Jenkins</a> has a plugin called <a href="https://wiki.jenkins-ci.org/display/JENKINS/Task+Scanner+Plugin">Task Scanner</a> that
lets you track comments matching certain patterns and give them priorities. I like to mark <code>FIXME</code> comments as high-priority and
<code>TODO</code> comments as medium or low priority. It&#8217;ll also provide reports and charts so you can keep track of your task-oriented
comments on a higher level.</p>

<p>Sometimes I don&#8217;t use this system if the task is small or I have a really good grasp of how to write the thing I want to write.
The point is to <em>program deliberately</em>. Cheers.</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/EricWendelin?a=J1RDhvIbvwE:pazBLvy_2O4:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/EricWendelin?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/EricWendelin?a=J1RDhvIbvwE:pazBLvy_2O4:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/EricWendelin?i=J1RDhvIbvwE:pazBLvy_2O4:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/EricWendelin?a=J1RDhvIbvwE:pazBLvy_2O4:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/EricWendelin?d=I9og5sOYxJI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/EricWendelin/~4/J1RDhvIbvwE" height="1" width="1"/>

<p>When <a href="https://codeclimate.com/">Code Climate</a> and <a href="https://twitter.com/tsaleh">Tammer Saleh</a> from Thunderbolt Labs
teamed up to offer free code reviews at <a href="http://rubyconf.org/">RubyConf</a>,
I seized the opportunity to get feedback on my first Ruby gem &#8211; <a href="https://github.com/eriwen/gnip-rule">gnip-rule</a>).
A number of refactorings came out of that session, but one I found most interesting was an approach to making
conditionals more maintainable in languages like Ruby and CoffeeScript.</p>

<p>They say that the use of <a href="http://ghendry.net/refactor.html#dc">conditionals in Ruby is a code smell</a>.
Decisions about how your team approaches conditionals should be discussed then outlined in your project styleguide.</p>

<p>This is the refactoring I&#8217;m talking about &#8211; I have a bunch of different checks according to
<a href="http://support.gnip.com/customer/portal/articles/600659-powertrack-generic#Rules">Gnip&#8217;s PowerTrack API constraints</a> to
ensure that a PowerTrack rule follows the constraints.</p>

<figure class='code'><figcaption><span>Old and Busted</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">GnipRule</span>
</span><span class='line'> <span class="k">class</span> <span class="nc">Rule</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">valid?</span>
</span><span class='line'> <span class="c1"># So many ORs</span>
</span><span class='line'> <span class="k">if</span> <span class="n">contains_stop_word?</span> <span class="o">||</span> <span class="n">too_long?</span> <span class="o">||</span> <span class="n">contains_negated_or?</span> <span class="o">||</span> <span class="n">too_many_positive_terms?</span> <span class="o">||</span> <span class="n">contains_empty_source?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="kp">true</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'> <span class="c1"># def contains_stop_word?..end etc.</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>&#8230;And here is the same method, refactored. We turned each condition into an early <code>return</code> if the conditional expression is true:</p>

<figure class='code'><figcaption><span>New Hotness</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">GnipRule</span>
</span><span class='line'> <span class="k">class</span> <span class="nc">Rule</span>
</span><span class='line'> <span class="k">def</span> <span class="nf">valid?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span> <span class="k">if</span> <span class="n">too_long?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span> <span class="k">if</span> <span class="n">too_many_positive_terms?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span> <span class="k">if</span> <span class="n">contains_stop_word?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span> <span class="k">if</span> <span class="n">contains_empty_source?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">false</span> <span class="k">if</span> <span class="n">contains_negated_or?</span>
</span><span class='line'> <span class="k">return</span> <span class="kp">true</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'> <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Some might cringe at seeing so many <code>return</code> statements, but I argue that it&#8217;s more maintainable this way because it&#8217;s
obvious to the reader what is being checked. If another check is added, it doesn&#8217;t hang off a long chain of <code>||</code>s (or
use a bunch of ugly <code>\</code>s to break lines).</p>

<p>Not only is this useful in Ruby, but CoffeeScript as well. Example:</p>

<figure class='code'><figcaption><span>CoffeeScript Example</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='coffeescript'><span class='line'><span class="nv">stringify = </span><span class="nf">(obj) -&gt;</span>
</span><span class='line'> <span class="k">return</span> <span class="s">&#39;undefined&#39;</span> <span class="k">if</span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">undefined</span>
</span><span class='line'> <span class="k">return</span> <span class="s">&#39;null&#39;</span> <span class="k">if</span> <span class="nx">obj</span> <span class="o">is</span> <span class="kc">null</span>
</span><span class='line'> <span class="k">return</span> <span class="nx">obj</span> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">obj</span> <span class="o">is</span> <span class="s">&#39;string&#39;</span> <span class="o">or</span> <span class="nb">Number</span><span class="p">.</span><span class="nb">isFinite</span><span class="p">(</span><span class="nx">obj</span><span class="p">))</span>
</span><span class='line'> <span class="k">return</span> <span class="s">&quot;[</span><span class="si">#{</span><span class="nx">obj</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s">&#39;, &#39;</span><span class="p">)</span><span class="si">}</span><span class="s">]&quot;</span> <span class="k">if</span> <span class="nx">obj</span> <span class="k">instanceof</span> <span class="nb">Array</span>
</span><span class='line'> <span class="k">return</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">toString</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">obj</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Thank you again, <a href="https://twitter.com/tsaleh">Tammer</a>, for your very helpful advice.</p>

<p>What do you think of this formatting?</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/EricWendelin?a=uQrTuIbL_Aw:jYXvLnLMXUU:cGdyc7Q-1BI"><img src="http://feeds.feedburner.com/~ff/EricWendelin?d=cGdyc7Q-1BI" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/EricWendelin?a=uQrTuIbL_Aw:jYXvLnLMXUU:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/EricWendelin?i=uQrTuIbL_Aw:jYXvLnLMXUU:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/EricWendelin?a=uQrTuIbL_Aw:jYXvLnLMXUU:I9og5sOYxJI"><img src="http://feeds.feedburner.com/~ff/EricWendelin?d=I9og5sOYxJI" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/EricWendelin/~4/uQrTuIbL_Aw" height="1" width="1"/>

Let me tell you how to maximize your productivity on the Bourne Again SHell while minimizing your effort. bash has a ton of tricks and shortcuts that allow you to command it with little effort, and I intend to show you the features that help me day in and day out.

Today I’m going to explain the use of features like history, brace and file expansion, and other tricks by example and give you references for later.

Master your history

Those who forget history are doomed to repeat it. This is arguably one of the best productivity enhancing features of any shell.

You can check your history with the history command, which prints your last 500 commands (or so) by default. Alternatively, you can filter the list:

1
2
3
4
5
# Print last 10 entries
history 10

# searches history for cmd
history | grep cmd

Each entry has a number, which you can then execute with !<number>

Now suppose I want to copy a file to a directory and then change to that directory. The quick way to do that with history is:

1
2
cp myfile.txt my/directory/path
cd !$  # cd my/directory/path

or if I forget to run a command as super-user:

1
2
vi /etc/fstab  # oops!
sudo !!  # sudo vi /etc/fstab

to execute the last command starting with “mount”, since I don’t want to type it all out:

1
2
3
4
5
6
# Previously...
mount 192.168.0.100:/my/path/to/music /media/music

# Later...
!mount
# Repeats last mount command

Note that I often (but not always) prefer Ctrl-R, which will search history as you type. As an added bonus you can view the command before executing it.

Other examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
eric@sawyer:~$ echo foo -a bar baz
foo -a bar baz
eric@sawyer:~$ echo !:3-4
bar baz
eric@sawyer:~$ !-2 #2nd-to-last command
foo -a bar baz
eric@sawyer:~$ ^ba^ya #replace 1st "ba" with "ya"
foo -a yar baz
eric@sawyer:~$ !^:p #MUCH cooler than "echo ..." ;)
foo
eric@sawyer:~$ !?bar #Last command containing "bar"
foo -a bar baz
eric@sawyer:~$ !:gs/ba/ya #replace all "ba" with "ya"
foo -a yar yaz

Quick reference

!! expands to the last command and all arguments

!-3 3rd-to-last command and all arguments

!^ first argument of the last command in history

!:2 2nd argument of the last command

!$ last argument of the last command

!* all arguments of the last command, but not the command itself

!42 expands to the 42nd command in the history list

!foo last command beginning with “foo”

!?baz last command containing “baz”

^foo^bar last command with the first occurrence of “foo” replaced with “bar”

!:gs/foo/bar last command with all occurrences of “foo” replaced with “bar”

<any_above>:p prints command without executing

Helpful .bashrc entries for history

Copy and paste these into ~/.bashrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Don't put duplicate lines in the history
export HISTCONTROL=ignoredups

# Store a lot history entries in a file for grep-age
shopt -s histappend
export HISTFILE=~/long_history
export HISTFILESIZE=50000

# No reason not to save a bunch in history
# Takes up several more MBs of RAM now, oOOOooh
export HISTSIZE=9999

# Ignore dupe commands and other ones you don't care about
export HISTIGNORE="&:[ ]*:exit"

Another neat trick with .inputrc

If you are still particularly fond of the up and down arrows, copy and paste the following into a ~/.inputrc file. This will allow you to start typing a command and then hit the up-arrow to search backwards through your history for commands starting with what you typed. I prefer other methods usually but this is pretty cool, huh?

1
2
3
4
5
6
7
8
"\eOA": history-search-backward
"\e[A": history-search-backward
"\eOB": history-search-forward
"\e[B": history-search-forward
"\eOC": forward-char
"\e[C": forward-char
"\eOD": backward-char
"\e[D": backward-char

Further reading

Peteris Krumins has an excellent write-up called The Definitive Guide to Bash Command Line History which goes in-depth on many of the above topics, should you crave more bash history goodness.

Brace expansions

No shorthand list would be complete without the (in)famous brace expansions. Basically, they allow you to specify part(s) of an command to repeat substituting different values of a set within braces. Let me show you what I mean:

1
2
3
# Quickly make a backup
cp file.txt{,.bak}
# Equivalent to 'cp file.txt file.txt.back'

This is obviously very useful to prevent having to repeat parts of files or directory paths.

Suppose I wanted to make a template folder structure, I could make most of the directories I need with:

1
mkdir -p {src,test}/com/eriwen/{data,view}

This will expand every combination so I end up with src/com/eriwen/data, src/com/eriwen/view, test/com/eriwen/data, and so forth. Being able to create a template directory structure with one line is a big time saver!

Better filename expansion

I’m sure you often use the * operator to match files beginning or ending with something, but bash goes far beyond that. It should be noted, though, at some point a good find | grep is more powerful and useful. See Find is a beautiful tool for more information.

In addition to wildcards with *, you can use ? to match any single character. You can also limit matching to certain characters with []. For example:

1
2
3
4
5
6
ls
# prints "myfile netbeans.conf netbeans-6.5rc2 netbeans-6.5 netbeans-6.7 src"
ls netbeans-6.?
# matches "netbeans-6.5 netbeans-6.7"
ls netbeans-6.[1-5]*
# matches "netbeans-6.5rc2 netbeans-6.5"

.bashrc entries for better filename expansion

1
2
3
4
5
6
# Include dot (.) files in the results of expansion
shopt -s dotglob
# Case-insensitive matching for filename expansion
shopt -s nocaseglob
# Enable extended pattern matching
shopt -s extglob

cd shorthand

There are a couple quick tricks to change to oft-used directories. For example:

1
2
3
4
5
# Lame way to go home
cd ~

# The cool way
cd

You can also switch to the previous directory with cd - like so:

1
2
3
4
5
6
7
pwd  # prints /home/eriwen/src
cd /my/webserver/directory

# Do something...

cd -
# Now I'm back in /home/eriwen/src

To get more advanced with this, try mastering your directory stack with pushd and popd.

Conclusion

Effective use of history, brace expansions, and other shortcuts will to save you a lot of time. However, nothing is more productive than no typing. Automating things is best where possible. If you can’t automate, use smart aliases.

I know I did not cover tilde expansions, shell parameter expansions or bash key commands. You will want to check those out as well, but I find them less useful than what I’ve covered here.

Have any quick shortcuts you love? Share them in the comments!


Items:   1 to 5 of 24   Next »