Recently I began to read a good book about code quality and how to avoid the so-called “spaghetti code”. I still haven’t finished the book but I’m half way there and my code writing improved a lot since I started reading it. The book is called Code Complete – Second edition and I definitely recommend it.
Here i would like to add some of the things I’ve read in the book and share it with everyone. Let’s not waste any more time.
Get started.
Software construction.
Software construction is often ignored. People like to get working on an idea right away without spending time to think about the design of the application they are writing. This often leads to spending more time at production state (when you write the code). The reason is simple – people often haven’t thought about application specifics, or library specifics or something of that kind which leads to rewriting the code.
Trust me – you are a better programmer if you waste a day to build up a strong architecture of the code and save the time of ten people, instead of diving into coding some cool feature that may get rewritten in the future.
Comparing program to real life objects.
A good way to start making an architecture for your code is to think about objects in real life. There is nothing better than a good metaphor to understand the specific of a task. However don’t think that a good metaphor will tell you how to find your answer. It will tell you where to find the answer, rather than telling you what the answer is. The latter is specified by algorithms. An algorithm tells you what the answer is directly – a heuristic (this is what a metaphor in this context is) guides you to how to find the solution yourself or at least where to start looking.
The metaphor you probably use every day is – “writing code”. This suggests that code should be written like a good book and so it should be read like a good book. It is a developers job to make this to happen. Of course a book is most likely written by a single person. Software is most likely written by many people.
Consider that writing software is like building a house. If you are building a dog house you would go to the store and buy some wood and nails. In a day you will be ready with the house and if you forgot to leave a door for your dog – don’t worry – you only wasted a day. This is appropriate for small projects – if you write 1000 lines of code they are easy to refactor.
However if you were building a house is a bit more complicated and your approach would most likely be different. You would take the time to think what house you want to build. Then you and an architect will make a design and get it approved. Think about what you will need for the house and go buy the already made things and so one. Those principles are 100% accurate for software development.
Setting up prerequisites.
It is a good idea to think about several things and discuss them with your team before you start working.
1. Agree on what convention you would use in your project.
– Aside from the language specific convention decide if your class variables will be named with “m” prefix for example. Or where and how to write comments in your code. Or what formatting you will use. The last is specially useful when using version control to avoid unnecessary conflicts.
2. Discuss the best coding practices.
– How you will handle error cases (maybe use a loger or an assert if your language allows it).
3. Always discuss the steps needed for an actual commit.
– It is a good idea to make code reviews before committing to the master repository. Maybe the most experienced programmers can check the code of the more unexperienced programmers for quality and logical errors before they make a commit. This will help both the unexperienced programmers to learn how to write better code and the whole team to avoid pulling broken code.
4. Consider the version control tool you will be using and sync each other versions.
– Consider if you are going to use git, svn or something else before starting to code. Consider your repository. Avoid transferring code through text documents or patches.
Designing Goals
- ” Does the design adequately address issues that were identified and deferred at the architectural level?
- Is the design stratified into layers?
- Are you satisfied with the way the program has been decomposed into subsystems, packages, and classes?
- Are you satisfied with the way the classes have been decomposed into routines?
- Are classes designed for minimal interaction with each other?
- Are classes and subsystems designed so that you can use them in other systems?
- Will the program be easy to maintain?
- Is the design lean? Are all of its parts strictly necessary?
- Does the design use standard techniques and avoid exotic, hard-to understand elements?
- Overall, does the design help minimize both accidental and essential complexity? “
Straight from the book as those are excellent questions.