Six Easy Tips for More Maintainable Code
I’ve recently had the opportunity to work with a large, unfamiliar code-base. The experience has made me reflect on some of my own coding practices and on writing more maintainable code. Much has been written on this topic, but here are the things that I feel are most important and have a high payoff without much (if any) additional work.
Make use of inherent metadata
Programming languages have inherent metadata in how you name things, what types you use, scoping, visibility, etc. This information is invaluable for someone new looking at the code and helps make the code self-documenting. There are several things you can do here:
Use good names
- Give classes, variables, methods, functions, etc. descriptive, differentiable names. Things that are related should have similar names, while things that are not should not.
- Keep the names up-to-date. If functionality changes, update the name.
- Variables with different scopes should be differentiated. For example, prefix instance variables with an underscore.
- Be consistent.
- Don’t create variables for things you only use once - just put them inline. Then I don’t have to figure out where else the variable is used.
Use appropriate types
- If possible, don’t use multi-level typedefs. It’s a real pain in the ass to go through 5 header files to figure out that foo_bar is an unsigned integer. And don’t typedef pointers away - I want to know if something is a pointer or not. Especially don’t typedef pointers away by inventing your own syntax (e.g. CFArrayRef is a CFArray pointer). The language already has syntax for that.
- Do use typedefs for things like enums.
- Use the most-specific type possible. If something should not be negative, make it unsigned, etc.
Use appropriate visibility
If something should only be used within a class, make it private. People looking at the code then don’t need to worry about external clients.
Keep things short
Nothing is more depressing than trying to grok a long method/function/class. There shouldn’t be hard rules on length, but I try to write methods/functions that fit on one screen, and to keep my classes under around 1000 lines of code. Without caution, this can lead to a method/class explosion, but I’d rather grok a small class cluster than one gigantic class.
Keep block nesting to a minimum
Don’t have a while loop inside an if, inside an else, inside a for loop, inside an else if, inside an do/while loop, inside a …
Reduce coupling
If I have to understand more than a small portion of the code to change one class, there is a problem.
Write useful documentation
You don’t need a lot. Here is what I think is important:
- Describe briefly what a method/function does. No method or function should be so complicated that this is more than a sentence or two.
- If a method is overriding a superclass, note that and explain why.
- Explain anything unusual or complicated.
- If a method has concurrency issues (should only run on the main thread, etc), note those.
Think of those that come after you
Whenever you write some code, try to put yourself in the shoes of the next programmer to work on the code. This is the programmer’s version of the seven generations rule.
October 25, 2006 at 9:57 am
[...] Discovered this blog posting by accident (searching for some contact details of a friend who is a namesake of this person!) and thought it was quite interesting. It still amazes me when reading code how often people neglect to do even the simple things. [...]
April 11, 2007 at 11:16 pm
Please, give me contact address (email or msn) of this site administrator…
Thanks!
April 12, 2007 at 4:40 am
Why?
May 31, 2007 at 6:27 am
Great entry! Let me make a small comment if I may. You write:
3. Explain anything unusual or complicated.
I would rather say: if you find code which you think is unusual or complicated then you are probably doing something wrong or there might be simpler way. If you think the code is complicated / unusual while you are writing it then in two months it will be a complete nightmare. Chances are you could probably make is simpler by thinking a bit more about it.
This is something I read in Code Complete written by Steven C. McConnell (http://cc2e.com/) which is an excellent book on this subject.
May 31, 2007 at 4:41 pm
That is a good point. However, sometimes performance or other concerns win out over simplicity or readability.
August 14, 2007 at 7:40 am
[...] Six Easy Tips for More Maintainable Code [...]
August 30, 2007 at 12:10 am
[...] Cohen) (how to write maintainable code), 6 советов от Шона Келли (Sean Kelly) (more maintainable code) и 12 советов от Джоэла Сполски (Joel Spolsky) (12 steps to better [...]
October 15, 2007 at 12:28 pm
[...] Six Easy Tips for More Maintainable Code [...]