Brackets keyboard shortcuts now available in Dash

Dash, the documentation browser and code snippet manager for OSX, now includes a cheatsheet for Brackets editor, version 1.3

 
Dash is an excellent offline documentation browser and code snippet manager for OSX and iOS (though there are also “officially sanctioned” 3rd-party versions for Windows and other platforms).

Yesterday I submitted a pull request to the cheatsheets repo on GitHub that adds a cheatsheet for Brackets 1.3 keyboard shortcuts to Dash. Earlier today my change was accepted by Kapeli (a.k.a. Bogdan Popescu) and the cheatsheet is now available for download from within Dash.

To install it, first open the Dash Preferences then:

Select Downloads
Step 1
Choose Cheat Sheets
Step 2
Locate Brackets and click Download
Step 3
The Brackets cheatsheet as it appears in Dash
The Brackets cheatsheet as it appears in Dash


This version of the cheatsheet covers keyboard shortcuts for Brackets for Mac, version 1.3.



More:

The work #5

We are all many things. We all do many things. We will do many more things throughout our lives. Yet, when we are gone, most will primarily remember only one of them. They will pick from the lot and remember you as that. What they pick will, in their mind at least, be all that you are. Therefore, it is our job to ask ourselves with all the things we allow ourselves to do, with each and every one, “Is this something I want to be remembered for?”

— Patrick Rhone, “What Will You Be Remembered For?”



More:

Use concept naming to simplify and clarify your Javascript conditions

Concept naming comes naturally to most people but is still rarely seen in Javascript despite the benefits. In this post I’ll illustrate how a simple technique can make your Javascript code significantly more readable.

 
The benefits of writing simple, clear and human-readable Javascript code are well known if not always well practiced.  The other day, while skimming the source of some of the rules bundled with ESLint, I was pleasantly surprised to see many of them made use of “concept naming” for conditions. This is a practice common in many other programming languages but not something I see being used in Javascript very often, despite the benefits.

Let me illustrate the technique with a simple code snippet:


if (charsToMatch[line.charAt(cursorPos.ch)]) {
    // do something
} else {
    // do something else
}

The condition on line 1 is trivial but even something this simple can make us stop and pause as we try to work out what the test is doing, and why. If you’re new to the code or just haven’t worked on it for a while a lot of valuable time can be wasted trying to get an overview of the application flow. Each time we hit a condition we have to pause and ask “what are we trying to achieve here?”.

We could, of course, add a comment immediately before the condition but comments can quickly become out-of-date, incomplete or just plain wrong. For that reason there has been a general trend over the last few years to minimize or reduce the number of comments in source code, or even to abandon them altogether.

When it comes to conditions, there is a simple and reliable way of making your code more understandable – give a name to the concept that is expressed in the condition. In our simple example the intent of the condition might be to determine if the character at the cursor position is one that we don’t want to allow a user to enter. This is determined by checking if it appears in an array – charsToMatch.
We can rewrite our snippet to illustrate this concept:

var charAtCursorIsNotAllowedInInput 
       = charsToMatch[line.charAt(cursorPos.ch)];

if (charAtCursorIsNotAllowedInInput) { 
    // do something 
} else { 
    // do something else 
}

If I were to glance at the snippet above for the first time, I still wouldn’t necessarily know the specifics of the conditional test but I would immediately be able to glean the intent of the condition. It now more closely reflects how we think – in concepts rather than conditional statements – and is much more readable.

Of course, as conditions increase in complexity the benefits will become more apparent:

var charAtCursorIsNotAllowedInInput 
       = (charsToMatch[line.charAt(cursorPos.ch)])
         || (cursorPos.ch >= 0 
         && charsToMatch[line.charAt(cursorPos.ch - 1)]);

if (charAtCursorIsNotAllowedInInput) { 
    // do something 
} else { 
    // do something else 
}

In this snippet the test is more complex but the concept is unchanged and what is trying to be achieved is instantly understandable.

Am I suggesting you should always name your conditions? No. There is, of course, a slight overhead in the “noise” introduced by the extra variable (which I consider a small price worth paying) but, just as you wouldn’t find it necessary to comment every condition, you wouldn’t necessarily want to assign a name to every one either. As always, common sense and consideration for those that will come after you goes a long way.

I hope you can see from these simple examples that, like the judicious use of clearly named variables, applying concept naming to your conditions can make your Javascript code significantly more readable and easier to maintain.

Sensible global shortcut key definitions for iTerm2

Despite being a Mac-only product, the terminal replacement iTerm2 defaults to using very un-Mac-like shortcut keys. Fortunately, this is easily remedied.

 
Earlier today I updated my configs repo on GitHub to add the settings I use for the OSX terminal replacement iTerm2. When I installed iTerm2 I left many of the settings unchanged from their presets but one section I immediately updated was the global shortcut keys. These are initially set to reflect the illogical, outdated and downright bizarre shortcuts used in the editors of yesteryear – Vi and Emacs – and I wanted them to behave more like the standard OSX shortcuts. Thankfully, iTerm2 makes this very easy to do.

The shortcuts can be configured in the Keys section of the preferences (accessed via the iTermPreferences menu item). Below are my updated settings:

iTerm2 global shortcut keys redefined to be more OSX-like
iTerm2 global shortcut keys redefined to be more OSX-like

Only 7 key combinations were changed:

  • ⇧⌘↑ became Scroll One Page Up
  • ⇧⌘↓ became Scroll One Page Down
  • ⌃⌘↓ became Scroll To End
  • ⌥← became Send ^[ b i.e. Move Cursor Left One Word
  • ⌘← became Send Hex Codes: 0x01 i.e. Move Cursor To Start Of Line
  • ⌥→ became Send ^[ f i.e. Move Cursor Right One Word
  • ⌘→ became Send Hex Codes: 0x05 i.e. Move Cursor To End Of Line

Shortcuts are redefined by double-clicking on the entry then selecting the new action to be applied. In most cases you simply want to chose a predefined action such as Scroll to End:

iTermAction

though for Move Cursor To Start Of Line and Move Cursor To End Of Line you should chose Send Hex Code as the action and enter the appropriate code (0x01 for start of line and ox05 for end of line):

iTermHexCode

 

Similarly, for Move Cursor Left One Word and Move Cursor Right One Word you should chose the Send Escape Sequence action and enter the appropriate code (b for left one word and f for right one word):

iTerm_EscapeCode

 

After closing the preferences dialog your new shortcuts will be available for use.


More:

  • iTerm2
  • configs – some of my config files, including the one I use for iTerm2

Use ‘eslint-annotated-reset’ to introduce linting to your legacy project

Introducing linting to a badly maintained legacy project can be a traumatic experience but the stress can be drastically reduced by careful use of ESLint and eslint-annotated-reset.

You’ve finally got agreement – you’re going to introduce linting of your Javascript code to your project, a project that’s desperately in need some TLC. Naturally, after careful evaluation, your linter of choice is ESLint, but when you run it against your code base you’re overwhelmed by the number of errors:

627 errors? When are you going to get time to fix those?
627 errors? When are you going to get time to fix those?

In the image above, taken from a real-life project that shall remain nameless to protect the guilty, we can see an all-too-common result of simply applying ESLint with the default settings; legacy code that generates hundreds, sometimes even thousands, of errors per file.

Of course, you want to resolve those problems. You want clean, consistent and maintainable Javascript. That’s why you’re introducing ESLint in the first place. But… you also want to meet your deadlines and they don’t necessarily allow for a massive refactoring of your code. Neither do you want to alienate your team, some of whom may already be hostile to linting and eager for opportunities to derail its introduction.

It was for exactly this situation that I created eslint-annotated-reset. This is simply an ESLint configuration file – .eslintrc – that you download from GitHub and drop into the root folder of your project. The next time you lint your code you’ll notice a drastic change; zero errors or warnings. The reason for this is simple, all of the linting rules have been turned off!

the easiest way to eat an elephant is one bite at a time

Why would you run ESLint will all of the rules turned off? eslint-annotated-reset is based on the principle that “the easiest way to eat an elephant is one bite at a time”. You may not have time to resolve 600 errors today but you might be able to resolve 6 or maybe even 60. After turning off all of the rules by dropping the .eslintrc file from eslint-annotated-reset into your project you can then selectively and methodically turn the rules back on, one at a time.

To turn a rule on, simply:

  • edit the .eslintrc
  • locate the rule you want to implement
  • change the setting from 0 (off) to 1 (throw a warning) or 2 (throw an error)

Let’s say we want to fix some of those 627 problems we saw earlier. Let’s start by making sure that all equality tests use “===” or “!==” rather than “==” and “!=”. That’s considered a best practice and can help prevent pernicious  bugs.

After opening .eslintrc  we locate the rule, in this case called “eqeqeq” (you can find these names in the console output from ESLint). There it is on line 63. We can confirm from the description that it’s the rule we want (that inline description is where the “annotated” in the name comes from).

"default-case": 0,     // require default case in switch statements
"dot-notation": 0,     // encourages use of dot notation whenever possible ***
"eqeqeq": 0,           // require the use of === and !== ***
"guard-for-in": 0,     // make sure for-in loops have an if statement
"no-alert": 0,         // disallow the use of alert, confirm, and prompt

A simple change to turn the rule on:

"default-case": 0,     // require default case in switch statements</pre>
"dot-notation": 0,     // encourages use of dot notation whenever possible ***
"eqeqeq": 2,           // require the use of === and !== ***
"guard-for-in": 0,     // make sure for-in loops have an if statement
"no-alert": 0,         // disallow the use of alert, confirm, and prompt

and we can rerun ESLint:

A much more manageable 26 errors
A much more manageable 26 errors

The eqeqeq rule is now the only one turned on and we have a much more manageable 26 errors. We can make and commit those changes and take a further bite of our elephant another day.

When you’re updating .eslintrc bear in mind that some rules take optional parameters in addition to the 0, 1 or 2. In eslint-annotated-reset these are indicated by three asterisks at the end of the description. You can find more details of the allowed options on the ESLint web site at:

http://eslint.org/docs/rules/rule-name-here

For example, the documentation for the ‘eqeqeq’ rule can be found at:

http://eslint.org/docs/rules/eqeqeq

Details of the eqeqeq rule from the ESLint website
Details of the eqeqeq rule from the ESLint website



The current (first) release of eslint-annotated-reset includes all rules included in version 0.20.0 of ESLint, released on 24th April, 2015. For convenience, the rules in .eslintrc are organised in the same way as they appear in the documentation pages of the ESLint website. As new rules are added to ESLint, eslint-annotated-reset will be updated to ensure they are always in sync.

ESLint allows you to choose which rules and stylistic conventions you want to apply to your Javascript code. With eslint-annotated-reset you can apply them at your own pace.


More:

The work #4

Your code isn’t important: what matters are the ideas your code brings to life. Shitty code that makes a point is better than perfect code that proves nothing.

Don’t waste your short life getting lost in the geeky details of the toolkit du jour. Spend it using your skills to create something that matters to you, that may even last longer than you.

Most developers have far more power than we realize but too many of us squander it building things we don’t care about. Now is your time to make a difference. If you don’t do it now it may never happen.

— Nick Bradbury, “Code is Temporary”



More:

Starting Brackets from the Mac terminal command line

With the imminent release of Brackets 1.3 we now have the much-needed ability to easily start the editor from the terminal.

 
Setup of the command line shortcut takes just a couple of minutes:


1. Install Brackets 1.3

The latest version should be released for automatic update in a few days but if you can’t wait you can download the pre-release version right now from the Brackets GitHub repository.


2. Install the Command Line Shortcut

Start up Brackets then select the File… Install Command Line Shortcut menu item which now appears at the very bottom of the File menu.

Install the Command Line Shortcut

You’ll be asked to confirm your password, then the shortcut will be installed.

If everything went smoothly you’ll see a confirmation message and Brackets will be ready to use from within terminal:

Brackets Command Line tool installed

Notice that you can open a single file or switch projects.


3. Start Brackets from within terminal

brackets_zsh

As is common with other GUI apps started from the terminal there is one peculiarity:

  • If Brackets is closed, it will be started, the file (or project) to be edited will be loaded and the app brought to the front
  • If Brackets is open but not on top (or in another space), the file (or project) to be edited will be loaded and the app brought to the front
  • If Brackets is minimised, the file (or project) to be edited will be loaded but the app will not be brought to the front. This can be confusing if you haven’t seen this behaviour before because it appears that nothing at all has happened.

More info about Brackets 1.3 can be found in the release notes on GitHub.
Command line startup can also be set up on Windows; brief details are in the release notes.