Shonzilla, a pattern-seeking animal

Life is a game of patterns and chance, and those who play well will win.

Feeds
Add to Technorati Favorites


Twitter

See also (via Entrecard)

programming coupons powered by RetailMeNot.com

Thu Apr 1

HOW TO: Prevent Mac apps to quit on you

Have you ever accidentally typed Command+Q (which is the standard Quit command on Mac OS X) instead of Command+W (standard for closing a window or a tab)?

I know I did… one time too many after which I googled for like 2 minutes and found how to fix this. Another 10 minutes later, here’s a digest with two concrete examples for all of you to peruse.

Mac software can store various settings in the so called Mac OS X user defaults system. Windows users may liken this to Windows Registry. It’s quite similar but somewhat easier to use. For example, you can access the defaults system from the command line using the full glory of command line tools which is exactly what I’m using here.

  1. Choose the application which quit shortcut you want to reconfigure (e.g. Chrome) and note down the text next to the quit shortcut (e.g. Quit Google Chrome
  2. Find the application’s domain in the defaults system by running this from the command line (with “-i” we’re ignoring case and finding this more quickly):
    defaults read | grep -i “chrome” 
  3. In the result to the command above look for the application’s domain which looks similar to internet domains only in reverse (e.g. com.google.Chrome)
  4. Now reconfigure the shortcut by using the application’s domain, the shortcut text and shortcut definition:
    defaults write com.google.Chrome NSUserKeyEquivalents ‘{ “Quit Google Chrome” = “@$Q”; }’
  5. Restart the application for the change to take effect.

In case you want to define a different shortcut, use the following special characters for using Command (Apple sign), Option, Shift and Control buttons:

@ - Command (Apple sign)
~ - Option
$ - Shift
^ - Control

After running all the above 5 steps correctly, you should have changed the shortocut for quit operation from Command+Q to Command+Shift+Q. Changes you make in defaults system will also remain active after restarting your Mac.

Here’s the similar change I’ve made on my Mac to prevent Eclipse from accidentally quitting on me (by redefining the quit shortcut to Cmd+Control+Q):

defaults write org.eclipse.Eclipse NSUserKeyEquivalents “{ ‘Quit Eclipse’= ‘^@Q’; }”

To assign Quit Eclipse shortcut to Cmd+Shift+Q you would need to escape $ sign because of the way shell parses the command line (to access reference environment variables and command parameters):

defaults write org.eclipse.Eclipse NSUserKeyEquivalents “{ ‘Quit Eclipse’= ‘@\$Q’; }”

Other signs you do not need to escape.

Wed Jun 10

Extended code folding in Eclipse

The problem

I program Java. I use Eclipse to do it. I like concise code. I work with open-source projects. Sometimes I need to read someone else’s code. Sometimes (more so than not) people write ugly code or hard-to-read code at least. When I say “ugly” and “hard-to-read” I usually think of long methods.

Is there a solution?

If you fit the above description then you might want to use code folding. If you had a fair share of navigating through code, you probably already know about it. In a nutshell, code folding is an IDE feature which allows you to collapse (and later expand) a block of code that you do not want to look at all the times, thus leaving more screen real-estate for looking at code that matter to you at a given moment. Sounds useful? Well, it is!

However, you may not be happy with the code folding support bundled with Eclipse since it works with (nested) classes, methods but does not work with control flow structures (if, while, for, try-catch-finally, etc.). This full-blown code folding support that includes any code block including control flow structures I call extended code folding. You may have seen NetBeans and other IDEs sporting this feature and you wished you had it in Eclipse, your favorite IDE.

Solution indeed

Well, there’s a solution for this missing feature in the form of Eclipse plugin called Coffee Bytes Code Folding over at RealJenius.com. All you need to do is add the following update site:

http://eclipse.realjenius.com/update-site
Once installed go Preferences->Java->Editor->Folding and in Select folding to use drop-down menu choose newly installed Coffee Bytes Java Folding. This plugin, just as Eclipse’s built-in code folding support, allows you to see the folded code by hovering your mouse over a plus sign. While I’m there, let me suggest you to turn on line numbers via Preferences->General->Editors->Text Editors->Show line numbers as it will make it easier to not only recognize folded code sections but also to tell what line are you looking at without actually having to click at the precise line in code and read the number from status bar. This is good for linking compiler/run-time errors and what line your colleague is talking about with less effort in exchange for some 20-30 pixels. I also recommend using Modern icons for plus/minus folding handles so that you can easily distinguish whether you’re using extended or built-in code folding.

After using this plugin for a couple of minutes, I’ve noticed one thing I didn’t like. When a code block is folded, braces (a.k.a. curve brackets) are not hidden as they are with the built-in Eclipse code folder. Namely, the closing brace remains standing in a separate line thus needlessly wasting one line without providing any meaningful information.

Shortcuts

Shortcuts are your friends as they reduce the distance your mouse (and your hands) need to move (and programmers should do everything they can to avoid RSI). Also, each shortcut you learn improves your ninja skills. You may want to learn them:
  • Ctrl + <Numpad Plus> - Expand a single node
  • Ctrl + <Numpad Minus> - Collapse a single node
  • Ctrl + <Numpad Multiply> - Expand (toggle) all nodes
  • Ctrl + <Numpad Divide> - Toggle folding (turn code folding on and off)
  • Ctrl + Shift + <Numpad Multiply> - Reset structure
  • Ctrl + Shift + <Numpad Divide> - Collapse all

Final thoughts

This plugin will also allow you to define custom regions (as Microsoft Visual Studio .NET has had since first version), which Jeff Atwood (of Coding Horror and StackOverflow fame) calls glorified comments with which I fully agree. Having that in mind and understanding that “folding is used to sweep code under the rug” in mind, you should really use code folding ONLY on other people’s code that you have no control over. At the same time, you should make sure that you do not fold your own code as you may be led to believe your code is easy to read and you’ll end up creating horribly long methods and code blocks that are hard to read. Well it will be hard to read for people not using code folding, which are many. After all, nobody should be force-fed with a bunch of crutches plugins anyway. :-)

Conclusion (and a pattern)

So what’s the takeaway here? It’s a pattern to keep in mind:
If code requires code folding, that code must be bad.
Why do I say this? Because folded code hides what’s inside thus violating Separation of concerns principle since folding hiding code may lead you to squeeze all kinds of logic inside a single folded line.