I picked up Software Design Magazine today. I am not a regular subscriber but I picked up this magazine because of their headline.
"17 rules for clean coding"
Yes, I love all thing clean and simple!
Let's start with "Why write clean code?"
A. Codes are to be shared.
I often write codes for myself and "just myself".
If someone were to see my old codes.. well good luck, it's not going to be easy to ready. Sometime it's confusing for me too.
B. Codes are to be added
I often work on new code based on my own old codes. And you need to be able to read your old code to do that.
C. Clean code helps you organize.
Indeed.
Rule 1 Easy to write vs Easy to read
Take easy to read.
Rule 2 Follow the code rule, and know why there is a rule.
Don't just do it, know why you do it.
Rule 3 Naming is important
Name of function, name of variable, name of space, it's all important.
Rule 4 Unify the naming style
is it CreateSimpleWindow? createSimpleWindow? create_simple_window?, pick one and stick with one.
Rule 5 Be Stylish
This one seems easy because IDE does the style for you.
Rule 6 No Magic Number!
Dim book(1000) as Book
You will look back and wonder where the 1000 comes from.
Instead, do
Constant intBookNum = 1000
Dim book(intBookNum) as Book
Rule 7 No Copy Paste
Really? I do this so often (too often). If you are going to copy paste, make it modular.
Rule 8 No copying data
I think this may be arguable, but you can save memory space and few lines of codes.
Rule 9 Make it clear when to exist out process
Use of goto sometimes is OK and helps code to look clean and easier to understand
Rule 10 Make your scope narrow
Less global values, more restriction on accesses, because global values can be changed by too many functions. It also make it more difficult to read.
Rule 11 Memory length
Shorter the memory is better, don't hold on to memory for long. It makes it more difficult to read.
Rule 12 Static values
Less static values makes it easier to follow the process of programs. Well, I think this is not always true.
Rule 13 Commenting
Comment as clean and much as you can! Yes.
Rule 14 Self documentation
Use Enum to help defined the options for values.
Rule 15 KISS
Keep It Simple Stupid! Right.
Rule 16 Know the length of value.
Keep it minimum but make is sure it's enough.
Rule 17 Make it modular
Make it modular as much as you can. (to certain degree)
After all, just remember to write readable code, and make it clean as possible. It takes some practice to be able to master
the writing of clean codes.