Don’t be afraid to take short cuts


It has been said before that the best coder is a lazy one (Jeff Atwood). A lazy programmer tries his hardest to avoid writing monotonous code and always wants to find the path of least resistence to solve a problem.

Writing software, especially early in the process can be dull. This is the part of the process where you are setting up the solutions, the structure and laying down the foundations. The process many programmers will affectionately call boilerplate code. So why not take a few shortcuts?

Shortcuts in this context does not mean dropping quality to get the job done “faster”, missing the odd unit test or applying liberally the software duct tape to hold your software together. It refers to unleashing the inner coding couch potato so that you can do your job just as well but expending less energy on the mechanics of writing the code, leaving much more processing time to thinking, or in our case producing the best and most friendly finance software possible. After all this is where most great coders win or lose their battles.

Here at PiggyBank HQ, we love to take shortcuts and here are some of the things we use:

Keyboard shortcuts
Getting familiar with your IDE of choice’s keyboard shortcuts will make you faster. Once you get into full swing you will not be writing code so much as connecting letters with shortcuts to auto-complete your coding sentences. Here at PiggyBank HQ we use Visual Studio as our IDE, on its own it has incredible intelli-sense, coupled with JetBrains Resharper you have incredible power all without leaving the keyboard.

As with all things in life, practice makes perfect. So challenge yourself to navigate round your app code without the mouse, After all it only takes ten minutes a day ( You may also want to consider learning a coding kata to force the muscle memory (Clean Coder, Pragmatic programmer

To get you started here are a few links to shortcut cheat sheets for both VS2010 and resharper.

Shorthand code
So we know that the IDEs are designed to make us lazier, what about our language of choice? C# is a great language, supported by a great framework. With each new version of the language we are being spoilt with ways to make our jobs easier. Cast your mind back to the code you wrote on the dotnet 1.1 or even 2.0 frameworks to what you are writing now. Your younger self would call you lazy, then want to get involved.

The turnery ? operator is great for scenarios where you want a conditional statement with simple outputs. The basic structure is:

return A > B ? A : B which is equivalent to: if A is greater than B then return A else return B

Using statement
The using keyword provides a shorthand way to deal with objects implementing IDisposable. Rather that writing lengthy destructors wrap your disposable objects in a using statement to handle the heavy lifting for you

Object Initializers
The truly lazy way to populate objects with values. This works equally well inside a LINQ statement and condenses the setting of object values

Automatic properties
The are occasions where you want to have code inside your property setters and getters. I rarely do so I use automatic properties.

Why write this?

int _age;
int _name;

class Cat
public int Age
get {return _age;}
set {_age = value;
public string Name
get {return _name}
set {_name = value}

When you can write this!

class Cat
// Auto-implemented properties.
public int Age { get; set; }
public string Name { get; set; }

As a bonus, visual studio 2010 has a handy keywork. Type in “prop” then tab twice for VS to complete the property for you, these are known as snippets

Here are some other C# shorthand language idioms for further reading:

  • The var keyword
  • Lambda expression
  • Anonymous Types

Just remember that you are writing code so that coders changing it after you can maintain it. It is important to know when to write for humans and when to write for performance.