Best code is the code that is easily readable. But again it is not always possible to write complex logic that way. I such cases your best option is to explain the code in simple English! yup comments were invented for a reason you know :D
There are few philospphies that I have seen people adopting:
1> Comments are for noob
Follower of this philosophy don't write comments at all. I have seen thousands of lines of code without comments. Only place they use comments are obsolete code :(
This makes code hard to understand and hard to maintain. Always remember the maintainability of the code takes precedence over readability and writability.
2> Comments for everything
This is some thing that my instructor in university wanted from us. Though this philosophy has it's own issues. I would recommend that all the instructors adopt this philosophy. This way kids are trained not to skip comments when they are under pressure.
In professional environment this is not that great an idea. Sometime the green block of code takes too much attention. This makes code hard to read as well. There is a very big fat line between the first 2 categories and one should always reside on that line :)
3>Comments are for coder
This is most popular style people adopt. Developers use comments where the logic is so complex that even the write of the code got confused. So he wrote some comments so that he can recall the magic used there when he reads the comment.
There are 2 issues here. First is that the usually the code could have been broken down in simpler sub sections. Second issue is that why didn't developer think of simpler logic in the first place :D
4> Write code for human beings, not machines
This is perhaps the most efficient way of writing comments. Always write code for a human reader. The machine will eventually understand you code once it is compiled. But there is no software that will make your code readable for a human reader. BTW I think this is an excellent idea for a product :P
If your code is not readable... and there is nothing you can do to make it human reader write comments to make it readable :)
Tuesday, December 7, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment