Wednesday, 5. March 2008, 10:36:48
I've gathered a few rules that, when followed, increase productivity of code writing.
Rule No. 1: Solve the problem in the simplest way possibleWe are often tempted to think of intricate mechanisms that solve a problem. We are often tempted to generalize the problem and prepare for a future that never comes. We often solve another problem or we solve the right problem but in a complicated way.
I can empathize with this. There's a certain beauty to a system where many wheels turn in a perfect way and work together to solve a problem.
The trouble is that a complex system is complex to maintain. What initially seems a simple but powerful generalization, or the use of a beautiful method for solving a problem proves in 99% of the cases to be off target, difficult to understand, non-functional, difficult to modify, difficult to maintain. Therefore,
a small "exageration" in coding produces a cascade effect that may live up to the end of the project.
When you think at it this way, you understand why everybody says that the code should solve only the current problem and only in the simplest way possible. There's always time for smarter approaches later (see rule no. 3).
Similar rules: Don't overengineer, Keep It Simple and StupidRule No. 2: Write first, edit laterWe have the tendency to think at better ways of solving the problem during coding. Don't do that: separate writing code from making it better.
Programming is creative work. You have to let ideas flow and to put them on screen as soon as they come. But when you do that, don't concentrate on all aspects. For example, error management can be added later. Methods can be extracted from existing code, once it's written. New classes can be created later.
I'm not saying that you shouldn't do any of the above when you write the code. Write whatever you feel natural to write, but avoid getting stuck in boilerplate or code organization issues. Make decisions on the way, as soon as possible, so that you continue to write code uninterrupted.
I've seen this work while writing the parsing code for the DSL I'm developing. If I thought of all the patterns, of the structure of the code a.s.o., it was ready in twice or three times the workload it took me. Sure, I have to revisit the code and refine it, but that's exactly the point.
Good code doesn't get born out of thin air; it is the result of multiple refinements.So strive first to minimize the time needed for obtaining functional code. Add the bells and whistles later.
Rule No. 3: Refactor periodicallyThis third rule completes the circle. You solve only the current problem, in the simplest way. You write first, edit later. And you do this on a number of functionalities.
After a while, you realize that you can generalize parts of the code, or that some methods or classes repeat almost identically in different parts of the project. It's refactoring time.
But be careful:
Refactoring means modifying the organization of the code without modifying its functionality. You are not allowed to add functionality (maybe only as a by-product). You are not allowed to change the existing functionality. You can do only a number of things: extract method, extract class, move method, simplify code etc. (you can find them all in Martin Fowler's book about refactoring).
And here you are: three rules for writing code better and faster.
Personal advertisement: I am PassionIT and I help teams develop high quality software. I train, I help improving and I work hands-on, depending on the needs. Contact me if you need help.
Alexandru Bolboaca-Diaconu
