The coming weeks I will have 3 articles for you about best practices of software development. Following these methods could lead to more efficient development and better quality code. In this post: Programming Techniques and Strategies.
Purpose and planning
The purpose of this series is to have some understanding of how to write quality code to become a better developer. It should be a theoretical reference to go back to from time to time. It should complement the practical day-to-day learning.
- I) Programming Techniques and Strategies
- II) Good Coding Methodologies
- III) Maintaining Quality Code
Potentially I will follow up with a 4th article about Objected-oriented Coding, whenever I have some more experience with it.
Programming Techniques and Strategies
50% to 75% of development time goes into testing and validation of code. Why? Software development is a complicated process. In this posts some techniques to handle this.
4 things that define uncontrolled complexity and should be limited:
- Coupling: the more dependencies of subsystems the more complex the system overall and the easier bugs get introduced;
- Control flows; the number of independent paths (see also Cyclomatic complexity);
- Complexity of data structures;
- The overall size of the program.
To avoid or reduce complexity a developer can:
- Partition: break the program up in smaller units or classes;
- Add hierarchy to your programs. A good example is the OSI model: similar communication functions are grouped into logical layers. In programs, units are black boxes and are self contained. This way they are easier to test. Use local (not global) variables. Build in privacy: data should be local to each blackbox element (method or function);
- Limit functionality at the start. Adding functionality scales up complexity in a non-linear fashion;
- Use an iterative development process: have early and frequent feedback loops. See further down ...
Good software ought to be non verbose and elegant. The most efficient piece of software for a particular purpose uses the least amount of code and the smallest number of abstracted entities.
The human factor is important. Pair programming can reduce the amount of bugs introduced into the system. Same with peer reviews and collective code ownership (if communicated well and version controlled).
Good software can be measured by the following key metrics:
- External factors: availability, efficiency, flexibility, integrity/ security, interoperability, reliability and usability.
- Internal factors: maintainability (average time to correct faults), reusability (well documented, platform independent, modular), readability, portability, testability.
The best prevention measure is to write robust code that anticipates problems, validates data inputs and terminates gracefully providing debug info to de developer.
To be defensive a developer should use:
- Assertions: test for errors that should never occur;
- Input validations: test ALL data inputs (whether it comes from a database, users, or from methods);
- Error handling: these errors are expected but it depends the application how to handle them (a cash withdrawal machine should handle errors differently than a word processing program);
- Error containment: shut down parts of the program to prevent damage to other parts.
Software development methodologies:
There are different types of development styles:
- The Waterfall model is a sequential design process with its roots in the manufacturing industry. Progress flows from the top to the bottom. Requirements, design, implementation, verification and maintenance, each stage is completed before moving to the next one. The risk here is when requirements change throughout the process. In that case you would have to go through most of the process again.
- Iterative development is cyclic and a response to the weaknesses of the Waterfall model. It is more flexible because it works with smaller iterations. It is adaptive, starts with a small version, changes per cycle are smaller so easier to correct if wrong. The customer has an early demo ready.
- Agile methods are iterative and incremental. They are adaptive rather than predictive and people-oriented rather than process driven. The work is done in small self-organizing, cross-functional teams.
- Extreme Programming is a type of agile software development. It is characterized by simplicity and clarity in code (using coding standards), rapid iterations, team work (pair programming), refactoring and unit testing, frequent feedback from the customer (changes are a fact of life) and 40-hour workweeks (no overtime).
Two must-reads on this topic are: The Pragmatic Programmer: From Journeyman to Master and Clean Code: A Handbook of Agile Software Craftsmanship.