Writing quality code

Writing quality codeFelix AndegoBlockedUnblockFollowFollowingFeb 11As you continue with your quest of becoming the ultimate programmer, there will come a point where just writing code will not be enough for you.

You will want write something more manageable, more scalable, more correct, more portable, more clean and more resilient.

  While this is non-exhaustive list, it is a great point to start learning how to do all that.


Write Orthogonal codeWhen two things are orthogonal, a change in one thing does not affect the other.

  When writing your code always remember to reduce the interdependence between the various modules.

Avoid global variables and always keep your code decoupled.

  The benefits of this are:Increased portabilityYou code becomes easier to testThere is less risk when changing any part of your system2.

Deal with all exceptionsDon’t leave any catch clause empty.

Always write a code for each event that can happen.

Leaving this out might be a very serious source of bugs in your system.

Generic exceptions should also be avoided as much as possible, always catch specific exceptions.

If you can’t handle an exception sometimes its even better to re-throw it back to the caller.


Functions should do only one thingFunctions should be short and should do only one specific thing which should actually be used to name the function.

If you realize that your function is actually doing more than one thing, it might be a good idea to divide it to the various parts.

This makes your code more readable and easier to test and debug.


Be consistentIf the framework you are building for uses some code styles, please stick to them and apply the style all over your code base.

If you decide to name variables and functions in a specific way, also stick to that.

This is actually so important when you are collaborating with other developers as people will easily infer a lot of things from the existing code and this will result to less confusion and increased familiarity.


DRY — Don’t Repeat YourselfDuplication is the root of all evil in software.

Imagine you had repeated the same URL four times in your code.

This would require that you update all the four variables in case the URL changes.

This is also a chance for an error to crouch in if in any case you missed to change any of the four variables.

This is just a simple example, sometimes it can be very critical and that’s why many principles and practices have been created for the purpose of controlling or eliminating it6.

Use code comments gracefullyNothing can be so helpful as well documented comments.

You should always keep your comments as updated as possible.

Here are some important tips for writing good comments.

Comments do not make up for bad codeDon’t repeat yourself, only use comments where necessaryAvoid commenting out code.

It might only be useful in the short term, in the long term it might lead to clutter and rot.

Avoid attributions and bylines, version control will take care of that.


Use meaningful namesWe do naming everywhere in our code.

In our functions, variables, classes etc.

It is therefore a good idea to choose good and easy to understand names.

Here are some tips:Use intention revealing namesAvoid disinformationUse pronounceable namesUse searchable namesAvoid mental mapping.

Readers shouldn’t have to mentally translate your names into other names they already know8.

Format your code appropriatelyYou should choose a set of simple rules that govern the format of your code, and then you should consistently apply those rules.

If you are working on a team, then the team should agree to a single set of  formatting rules and all members should comply.

It helps to have an automated tool that can apply those formatting rules for you.

You can actually use EditorConfig a tool that helps developers define and maintain consistent coding styles between different editors and IDEs.


Test your codeAs much as most programmers hate to do this, this is the only way to prove that your code actually works.

With testing you will be able to make big changes to your code without worrying about breaking anything since there will be tests to prove that everything is working fine.

Another benefit of testing is if you find a bug, write a unit test for it and fix it.

That particular bug is unlikely to ever appear again, and you can prove it with your test.


Know your editor wellChoose an editor, know it thoroughly, and use it for all editing tasks.

If you use a single editor across all text editing activities, you don’t have to stop and think to accomplish text manipulation: the necessary keystrokes will be a reflex.

The editor will be an extension of your hand.


Always use source code controlTo quote a friend“Code doesn’t exist unless it’s checked into a version control system”.

Use version control for everything you do.

Any version control, SVN, Git, even CVS, master it and use it.

Here are some questions you can answer with source code controlMade a change to code, realised it was a mistake and wanted to revert back?Wanted to review the history of some code?Wanted to share your code, or let other people work on your code?Wanted to experiment with a new feature without interfering with working code?and much more.


Don’t leave broken windows, refactor oftenAs the famous boy scout rule saysAlways leave the campground cleaner than you found itSame rule applies to your code.

Whenever you notice any wrongly named variable, duplicated code or wrongly formatted code always remember to change that.

No code will ever be perfect, you can only make it better.


Don’t program by coincidenceAlways know the effect of each line of code you write.

If you are using a library or framework before using any function or class, get interested in the code behind it.

Most of the code actually have side effects that you may be unaware of.

This is how bugs creep in to your system and it will be very hard for you to debug such errors.

Taking time to go through documentations can save you a lot of time down the road.

This concludes the list and if you have any more ideas or feedback please share them on the comment section below.


. More details

Leave a Reply