Showing posts with label People. Show all posts
Showing posts with label People. Show all posts

Tuesday, February 12, 2008

The Magic Number: Another Reason to Learn Your Tools

We developers are often paid to get productive quickly on a large, unfamiliar code base. There are many ways of doing this. One way of NOT doing this is to start from public static void main1 and work your way through each function or class or module till you understand what you want - that way lies failure and madness.

I can think of 3 ways of getting to grips with new code:
  • Use external sources: Talk to others who understand the code base better; Read the documentation; Watch a tutorial screencast; ...
  • Use the debugger: Step through the code and see what it does.
  • Use the Source, Luke: Read through the code.
Of these, I've found the last the hardest. It is this skill - reading through code - that I want to talk about today.

When I say "read through the code", I use read in a sense that is very different from reading a novel or even a text book. It's more of jumping around the code base, reading a bit here, taking in a whole class at a glance, poring over a single method for over an hour, finding the usages of said method, ... If you are a programmer, you know exactly what I'm talking about. While doing this, you are attempting to build a mental picture of what is going on in the code. This is where the Magic Number comes in.

The idea is that human beings2 can only handle a limited number of pieces of information in their working memory. You can think of working memory as the set of registers on your computer's CPU - all of your thinking happens on pieces of information that is in the working memory, and there is a limited amount of it available. Even though you may not agree with the range 5 to 9 specified in the article, for the purpose of this post, it is enough if you accept that there is a limit.

Here's how I think the code reading process works: You start off with understanding a small piece of code - piece A, let's say. Then piece B. And D. And then how B and D are related via C. And Z. Five of the slots in your working memory are now filled. Suddenly you realise that all these pieces are part of the English alphabet - and boom! five used slots shrinks to one used slot! The larger chunk - English Alphabet - occupies just one slot worth of space. You are essentially building up more powerful abstractions with which you are able to explain the purpose of ever larger swathes of code. So your ability to comprehend code depends on how many free slots you have in your working memory.

"Ok, even if all that is true, where do keyboard shortcuts come in?", you ask. Well, which keyboard shortcut to use is a piece of information too. If you are not very familiar with your IDE, then thinking about which keyboard shortcut to use will take up one or two registers in your working memory. Your brain has less free registers and hence is a slower computer. On the other hand, if you are really familiar with your IDE, your fingers automatically press the right key-chord when your brain thinks "I wonder what that method does?" and take you to the definition of the function. This process doesn't use the 5-9 registers at all, but is muscle memory a la riding the bicycle or swimming. More registers to understand the code implies faster understanding.

There is another factor involved as well: frustration. When you are struggling with the tool, the resulting frustration will make you want to take more breaks from your task of understanding the code. When the tool is almost an extension of you, the feeling of being in control will allow you to work longer periods. This is one way the human brain is different from computers - the latter doesn't get tired.

I don't have proof to offer for all the claims I'm making; I do have anecdotal evidence though.
Exhibit 1: While reading Java code, I'm much faster while using IntelliJ IDEA than while using Eclipse. I believe that Eclipse and IntelliJ IDEA are pretty much equal feature-wise. I attribute my higher productivity on IntelliJ IDEA entirely to my much greater familiarity with its shortcuts.
Exhibit 2: I find it easy to switch between reading C# and Java. I think a lot of the credit should go to JetBRAINS' - their masterstroke of reusing the IntelliJ IDEA's shortcuts in ReSharper means minimal muscle memory retraining when switching between these languages.

All of the above are arguments for learning to use your IDE really well - they could apply equally to using the mouse as well. Here's why I prefer the keyboard: accuracy and speed. With the mouse, I'm a bit of klutz. I click the wrong piece of code. I select the wrong region. I'm really slow at reading the available menu options and very likely to pick the wrong one even after all the reading. And my hands ache when I use the mouse for a while. With the keyboard, especially my trusty old Kinesis Classic, I'm fast and accurate. This leads to less mistakes, less frustration and more time spent reading and processing code. Ergo, Happy Developer.

And that's another reason to learn touch typing!

Are you a predominantly mouse person or a keyboard person? Either way, what are your reasons for preferring one over the other?



1 Or its moral equivalent in your favourite language.
2 That typically includes us programmmers as well.

Friday, January 25, 2008

Shortcuts to Impressing Developers

If you have been wondering whether it is worth your while to learn the keyboard shortcuts on your favorite IDE*, then here's another reason apart from the obvious one of blazing fast productivity.

I often teach a course on Object Oriented Programming and Design Patterns called the Object Bootcamp. The participants are typically other developers. I'm yet to see two developers who will not disagree about at least one aspect of Object Oriented Programming. Given that the course advocates a particular style of OO which would surprise the average programmer, getting the audience to listen is crucial.

Thankfully, the course is very hands on - it involves getting the participants to write some code to solve a problem and then critiquing** their code. I also normally refactor the code that I'm critiquing. Navigating the code and refactoring, both afford excellent opportunities to show off my skills with the IDE. Being able to select a block of code, do a Ctl+Alt+M and have IntelliJ Idea or ReSharper extract that block into a well-named method, as well as highlight and replace other similar code in the file, usually impresses other developers no end. Even simpler features like hitting Ctrl+B to get to the definition of the class or method are often quite alien to most developers. It makes you, in their eyes, a person who knows where his/her towel is and hence they are more amenable to letting you have your say.

The opportunities to impress others with your keyboard shortcuts are legion. Maybe you are giving a presentation to an audience which involves showing some code. Maybe you are interviewing for a company like my employer ThoughtWorks which invites you to pair program with one of your future colleagues as part of the hiring process. Maybe you are collaborating with your boss on creating a Word doc. In each of these situations, knowing and using keyboard shortcuts can give you that aura of competence - at least for a while.

What has your experience been with keyboard shortcuts? Have you seen others react differently to people who use keyboard shortcuts?


* You do use an IDE, right?
** Critiquing = PC terminology for projecting the code for the whole class to see and tearing it to pieces, thus publicly humiliating the author. The theory is that this builds character.;-). Just kidding! Developers seem to learn very well from their mistakes.

Saturday, January 19, 2008

The Seductive Charm of The First Post!

Take a look at the first blog entry on a random sample of a few of your friends' blogs. Chances are you'll find what I found recently while trying to decide what my first post should be: many blogs start with "This is my first post", "Finally, I've started blogging" or some variation on this general theme. Some of the blog entries stop there, while others go on to explain what the author will blog about and/or why the author is blogging. Eerily similar to the horde of "Programming in LanguageX" books which all start with a program to print "Hello, World". I'm betting, in ancient Egypt, after papyrus was invented, the first hieroglyphic diary started with "Dear Diary, I've decided to record my daily thoughts here" or something of that sort. I've never been any good as a rebel, so here goes my first post:
I'm blogging! This is my first post! As the name of the blog suggests, I'm a programmer* and I love coding. I hope to write about coding, programming languages, software development processes, problems I've faced (and their solution when I know them), how to teach others to code and people who inhabit the world of software development. Above all, I hope to blog regularly**.
There. I've done it. The first post in all its splendor is out there.

Let's talk about why people have this fascination with first posts.
  • I think we all have a fascination with beginnings. Count the number of people who make New Year resolutions, if you want evidence.
  • We like some fanfare when we begin an activity. And this might give us that initial determination to press ahead, till the activity becomes a habit.
  • If the first post makes the author think about and express his/her goals, it will probably help with the determination part as well.
What do you think? Have you done a "first post" blog? What was your motivation in doing that?


* Who obviously thinks he is God! Don't they all?
** Regularly = About once a week.


PS: If you are wondering "what is with the asterisks and footnotes", they are an attempt to ape my all time favorite author Terry Pratchett's style