Sunday 16 September 2012

Become Zen of coding

            Do you want to get hold of your coding style and become the Zen of coding with less effort; the best place to go is to buy the book “Code Complete by Steve McConnell”. He teaches in a way that even layman can get it. After reading, I thought let me write the best he got in this book. I have carefully selected only the simple tips/tricks and techniques, which may be things you have already used in your project. But still it is good to revise.

 He goes through the book with the map of software life cycle from designing software to code tuning them.

In First few chapters he talks about stakeholders, STAKEHOLDERS are everyone who uses the software, starting from developer\QA\Project managers to actual end users and how to manage each stakeholder’s requirement through your requirement document.

            Let’s start with system architecture where we will concentrate more on how system architecture and what constitutes a good architecture.

What is System Architecture?

            It is the overview about the system which must address all the requirement of stakeholders with views and perspectives.

I have carefully chosen only important details that SHOULD be handled while creating the Technical design document which was explained by the author. I have left some of the known details that constitute the Technical document.

Details that needs to be covered in the technical documents are


            Alternatives of the current solution, why the system did not choose the alternatives and also the justification for the alternative

Major modules

            Major modules should be identified and should also be defined in the system, Identifying them is the key.

Change Strategy

            System should be designed to handle changes early. For e.g. changing requirement should be configurable.

Buy vs. Build Decision

            Deciding which module or the part of project should be developed by the development team or the software should be bought from third party vendor. If the project has an office document report that needs to be created, going for Microsoft DLL to create the report is not a good option, because Microsoft do not support office installed in server, so you have to buy any third party vendor software for office report utilities, I recommend ASPOSE.

Major Data Structure.

            Data structure chosen must be explained over another. E.g. choosing IList as parameter over dataset is the best option because IList is lighter than dataset.

Key Algorithms

            All key algorithms should be explained clearly. For example, if the module has a functionality which includes tax calculation and we use tax calculation with flexi benefits then those algorithms should be explained in detail. You also have to decide whether we can use any software or service that is available already in market.

Deciding critical factor to identify major objects

·        Responsibility of the object
·        How it is implemented
·        How objects interact with other objects
·        Description of Class hierarchies
·        State Transition and persistence

Memory Management

            Memory used for nominal and extreme cases. Requirement should be collected from the stakeholders. eg.., What might be the maximum number of users that will use the application and what are the frequently visited pages or application. Rough level of calculation must be done to test the results after development.

String Storage

            This seems to be trivial, how string will be manipulated and stored has to be decided and chosen as per the requirement of the project, because it saves lots of time. E.g. whether error message should stored in a config file or a resource file, if it is multilingual project.

Error Processing

            In any code written throughout ages, error processing is very important, the author gives example that only 10% is written for nominal case and other 90% is for error processing, so error processing strategy should be identified. Most important is the error detection, whether it is active or passive detection meaning, will the error be communicated to the user and action will be taken or the error just logged.

            Architect should provide and estimate the performance for major module and get it accepted by stakeholders, because business stakeholder really don’t care how much time the batch job takes to runs at night for data replication, but they CARE about the page load time.

Real time challenges in identifying the modules.

           Below are the list of areas you should concentrate on your requirement to identify modules.

Areas where changes are likely

·        Hardware Dependency
·        All complex Data\logic are likely to change
·        Business Rules

Areas to concentrate while coding

·        Places likely to change
·        Complex Data
·        Complex logic

Good reasons to abstract to a new Module

·        UI
·        Hardware dependency
·        System Management
·        I/O

 Things that I actively avoided in the article are below because I felt these are very primal knowledge and too basic for a programmer

·        Conditional Statement
·        Using Break & Continue Statement
·        Using Loop Variables
·        Recursion
·        Boolean expression

Let us see how the coding tuning should be done with principles in mind.

Guidelines of Initializing Data

Initialize data as you declare it. Always take advantage of your compiler warning

General issues in using variables

            Minimize the scope of the variable. Keep reference to variable together. Develop the habit of writing code assuming data isn’t persistent.

Statement whose orders don’t matter

            Principle of paradox
                        Keep related action together. Make code readable from top to bottom. Localize reference to variable, never scatter variables. Keep variable life as short as possible.

Controlling loops

            Any loops should have only one entry, author also recommends one exit but after reading Martin Fowler books I feel whenever you don’t have any more operation that needs to be done inside the loop, you can use return or break statement to come out of the loop. Read about Guard statements, if you want to know more on this choice. Choose while loop instead of for loop wherever possible. Avoid empty loops, keep house keeping variable either at start or end of the loop.

Taming Dangerous Deep Nesting

            Convert a nested if to SET of if then else. Factor deeply nested code into it’s routine. Redesign deeply nested code. Limit the nesting to three levels in for loop

Document Code

            Comment as you go along. Always document on why rather than how. Avoid abbreviation in comments

Encouraging Good Coding practises

Tips and Techniques
·        Assign two people for every part of the project.
·        Review every line of code.
·        Require code sign off
·        Routing good code for review

Techniques for improving software qualities
·        Define Software Quality objectives
·        Explicitly define quality assurance activities
·        Find the testing strategy earlier during design
·        Figure out software guidelines, may be very minimal, but it is better than none.

Methods to find error while Debugging

·        Be suspicious of routine that have had error before
·        Check the code that recently changed
·        Expand the suspicious region
·        Integrate incrementally
·        Set maximum time for quick and dirty debugging. This point fascinates me, author talks about setting a time limit say 15 minutes for a dirty debugging, this he calls as gut level debugging, where we figured out the place in our mind, where the bug might be without even debugging the code sequentially.
·        Take break from the problem.

Advanced Code Tuning Techniques

            My favourite one is removing the decision, if they are not changing the condition of the loop. I.e. removing the statement from inside the loop and placing it outside if they are not changing the condition of the loop


            Combine two loops that operate on the same set of elements

Sentinel Values

            Value that you put just past the end of search range and this guarantee to terminate the search. There should be condition that should be validated and it should break the control out of the loop

Busiest loop

            Pull the busiest loop to the inside. When you do this for any FOR loop operation which is more than one level deep, you will reduce the number of times the loop is executed.


            Use caching wherever possible, when you know data won’t change for a period of time or never changes then use caching.

Use strength reduction

·        Replace (+) with (*)
·        Replace long with short
·        Initialize at compile time

I have compiled only the simple guidelines, tips and tricks, so it is useful to everyone, if you want in depth knowledge then subscribe this book from library. It is very useful book; it has tons and tons of idea which will surely make you Zen in coding and helps you to figure out your own style.

Check out the book @ CodeComplete by McConnell  

IMPORTANT: Leave a comment to share what you learnt, and hit the “like” button to let others know about it (make sure you’re logged into Facebook to like and comment). 


No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Build Bot using LUIS