Book: Code Complete 2nd Edition
Just finished reading Code Complete 2nd edition by Steve McConnell. It is a reference book for programming best practices. Generally programming best practices books focus on requirement gathering, design, quality and management aspects of the software development process, in contract, “Code Complete” focuses on the coding or construction phase. It consists of practical insights and research data to support the practices presented in the book. It is an excellent resource for coding standards, design guidelines and other aspects of writing code.
A big section of the books is about how to structure the code, how to indent it, how to comment it, how to name variables, how to use control structure etc. It contains code snippets in different programming languages to demonstrate the good and the bad practices. Other sections discuss the design, testing, integration, debugging and other areas.
I high recommend reading it and using it as guide for day-to-day programming activities.
Following are few important points from the book which I recollect now:
- Code readability and maintainability are far more important then being efficient in terms of code performance or number of lines of code. The more readable the code is, the longer is the lifetime of the code. Readable code is easier to fix and enhance. It reduces the complexity of the software which is the single biggest challenge in programming.
- Performance tuning and optimization should be performed after profiling and identification of bottlenecks, not as we write the code. Research has shown that 20% of the code runs 80% of the time. Significant performance gains can be achieved by targeting the right segment of code . Having performance tuning and optimization as one of the coding guidelines is counter productive as it affect readability and maintainability.
- Information hiding is the key to good design. When breaking down the software into packages and classes, information hiding should be one of the guiding principles. It is important to design the packages before breaking the program down into classes. Some rules should be set to limit the inter-dependency of packages.
- Code inspection is a very effective method of detecting bugs early. It could be even more effective than system testing. Research data shows that no single defect detection technique is able to find enough errors, more than one technique should be combined to achieve better quality. Some of the techniques are formal inspection, pair programming, unit testing, system testing, beta testing, prototyping etc.
- Inheritance is one of the key concepts of object oriented programming, but if not used with care it also has a potential to introduce more complexity than it reduces. In the desire to model the real world, at times we introduce too many interfaces and deep inheritance hierarchies which complicate the code. When we are using inheritance, Liskov substitution principle (LSP) is a good thing to keep in mind.
- Its good to program defensively, asserts should be used to check for pre-conditions and post-conditions of a function. Asserts are meant to check for errors which should not occur at all while exception handling should be used to handle errors which may occur at times like unavailability of network.
- Before we start to code a function, it would help if we write the pseudo code first. Then we can put the pseudo code as comments inside function block and add actual code after each pseudo code line. This helps in coding and also saves the time required to comment the code.
- Quality is free, it is basically a reallocation of resources. It’s free because if we don’t allocate proper resources to QA, later on we will have to incur more cost in fixing the quality issues.
- Design is a wicked problem. Wicked problem is a problem which cannot be defined clearly until its partially or completely solved. Till the time we code the design we cannot be absolutely sure that design is correct. Moreover, coding is not just a mechanical exercise of converting design into a computer language program, instead it is a creative process. From this point of view, we should have more focus on construction phase of software project.