Monday, October 8, 2012

General Coding and Software Development Aspects

My favorite aspects in software development is coding and code reviews. Over the years, I have developed my coding habits and my religion which I have narrated in this blog post.

At the same time I have also realized that, code reviews are also extremely important as that can,
  • trap bugs way early in the dev cycle
  • improve the code readability which also means maintainability
  • help junior engineers to follow the coding conventions and patterns
  • help junior engineers to understand how important it is to write proper code
  • help code get optimized (even though little premature)
    • although premature optimization is the root of all evil, I am all for that 3%
I have been doing code reviews for quite some time now. While doing that, I have myself learned a lot and most of it comes from the fact that when you look at someone else's code as a critic, you often also improve yourself.

General Coding Philosophy

  • Understand Occam's Razor
  • Remember to keep it (your code) simple (but not simpler) and stupid (kiss) - Einstein Principle
  • Write as less code as possible (be a lazy programmer), but write smart code
  • Constantly question yourself - is there a different and better way to do this?
    • and in most cases there usually is one
  • Indent your code (your IDE can also do it for you)
  • Code itself should act as a documentation resource
    • it's like it's all documented there in the code
  • Follow Sun's (now Oracle's) Java coding convention
  • Know the rules and break some

Code Structure

  • If possible try to identify a design pattern before starting to write the code and use it as much as possible
  • Modularize the code components (it's an art) so that those are re-usable
  • Favor Composition over Inheritance (Inheritance breaks Encapsulation) - GoF
  • Transparency is motto, so use many interfaces, but not too many!!
  • Program to the interface and not to the implementation - GoF
  • Keep methods as small as possible, break larger methods into multiple smaller methods. This way code re-use will also increase and you will realize how easy it is to write better code.
  • Make sure code blocks are not repeated, if you have to repeat the code, turn those into smaller methods and re-use
  • Identify utility methods that are not related to any particular class and put such methods in some kind of Utils class, for example DBUtils, StringUtils.

Performance and Program Correctness

  • Do heavy lifting in "init" methods at the program start-up (only once)
  • Need logging to be done judiciously, use debug level as much as possible
  • Invalid parameter values are handled properly early in methods (Fail-fast)
    • throw java.lang.IllegalArgumentException or your own exception
  • Use of try/catch/finally. Make sure you add finally block to try block and do all the resource clean-up in finally block.
  • Define as many local variables as you want. Local variables go on thread stack and hence get garbage collected as soon as go out of scope.
  • Use java.lang.StringBuilder, if you don't need to synchronize on the underlying string variable.
  • Use Buffered I/O as much as possible
  • Identify the static methods in the class and make those static. If a method is not modifying the state of the class or if it only works with the arguments passed, then most probably it's a static method
  • Never return a "null" from your API, that's gross
    • throw an exception instead
  • Try and learn how compiler or interpreter of the underlying language works (I know it's a huge statement)
  • Learn how recursion (self-reference) works and how to terminate it

Synchronization

  • Handle synchronization issues very carefully (senior developers need to make sure that code is well written in this regard)
  • Make sure you don't introduce race conditions
  • Please go through a very interesting read on synchronization: Synchronization Primitives: Implementation and Examples
  • Use java.util.concurrent API as much as possible instead of re-inventing the wheel

Documentation comments

  • Add descriptive documentation comments
  • Don't add comments in the middle of the code which says "Fix for bug xxxxx - John Doe, 30/02/1855". Instead put that as a comment during a code check-in.
  • Don't keep commented code around, you don't need it, just delete it, SVN (or the source code control system) will take care of the history.

Development aspects

  • Don't start development unless and until software design is ready
  • Come up with low level design document and get it reviewed
  • Do a small but very quick prototype for unknowns that may exist at the beginning of the development, This will give you further confidence during your low level design.
  • Discuss your design ideas loudly
  • Complete the big development items first (such as protocol definition or gadget framework) and then move onto the smaller items
  • Learn about Unicode and encoding schemes such as UTF-8.
  • Write unit test cases. Although, I personally hate to write unit tests but it's a nice thing to do.
  • Don't break the build. If you break it, take the entire team for a coffee :)

I hope this blog post helps to all those coding enthusists!!

Above coding or software development guidelines are based on my own experience as a developer and a code reviewer. But most importantly, my entire coding philosophy has been influenced by Robert Field (JPDA and Javadoc Architect). I pretty much owe this blog post to him. Thanks Robert!

No comments:

Post a Comment