How to write awesome code
31 May 2007 in Personal Experience & Programming
I’ve been wanting to write a HowTo, but couldn’t find an appropriate subject. Finally occurred to me to talk about something I really appreciate: techniques to write good, simple and comprehensible code.
Writing good code always was a challenge to me, mainly because the definition of good code isn’t somehow very clear.
So, what is Good code? I believe that is a program that works flawlessly and has no bugs. Unfortunately, this can’t be easily archived, at least in practice.
Design, I think is the key to writing the good code. Planning in advance what the code must archive is crucial.
Whatever is the language of choice, all good programming must show this same good qualities:
-
Simplicity means you don’t do in ten lines what you can do in five. It means you make extra effort to be concise, but not to the point of obfuscation. It means you abhor open coding and functions that span pages. Simplicity—of organization, implementation, design—makes your code more reliable and bug free. There’s less to go wrong.
-
Readability means what it says: that others can read your code. Readability means you bother to write comments, to follow conventions, and pause to name your variables wisely. Like choosing “taxrate” instead of “tr”.
-
Modularity means your program is built like the universe. The world is made of molecules, which are made of atoms, electrons, nucleons, quarks, and (if you believe in them) strings. Likewise, good programs erect large systems from smaller ones, which are built from even smaller building blocks. You can write a text editor with three primitives: move, insert, and delete. And just as atoms combine in novel ways, software components should be reusable.
-
Efficiency means your program is fast and economical. It doesn’t hog files, data connections, or anything else. It does what it should, but no more. It loads and departs without fuss. At the function level, you can always optimize later, during testing. But at high levels, you must plan for performance. If the design requires a million trips to the server, expect a dog.
-
Elegance is like beauty: hard to describe but easy to recognize. Elegance combines simplicity, efficiency, and brilliance, and produces a feeling of pride. Elegance is when you replace a procedure with a table, or realize that you can use recursion—which is almost always elegant:
-
int factorial(int n) {
return n==0 ? 1 : n * factorial(n-1);
} -
Clarity is the boss of good programming, the platinum quality all the others serve. Computers make it possible to create systems that are vastly more complex than physical machines. The fundamental challenge of programming is managing complexity. Simplicity, readability, modularity, efficiency, and elegance are all time-honored ways to achieve clarity, which is the antidote to complexity.
All of these mean nothing if you don’t really understand what you’re doing. Good programs are more about a clear goal than coding skills. That’s why planning in advance is important. If you can’t write it down, if you can’t explain it to others, you don’t really know what you are doing.
A good programmer, is the one who applies such quality’s on their code 100% of the time. They understand the benefits of such code. But such skill, needs practice to grow. Writing great code doesn’t come fast and easy.
One other very important point in writing really good code, is using automated tools to test, profile and verify your code. There are actually many tools for almost every *popular* language to simplify the task of the programmer in simplifying, optimizing and speedup the code.
For .NET programmers I recommend using this tools:
- FxCop (code analysis tool) to check the code conformance to MS .NET Framework Design Guidelines.
- NUnit (unit-testing framework) for creating tests on your code.
- Lutz Roeder’s .NET Reflector is a class browser and decompiler that can examine an assembly and show you just about all of its secrets.
- NDoc (code documentation tool) will automatically generate documentation for your code using reflection to examine the assembly and using the XML generated from your C# XML comments.
There are others great tools, and not only for .NET. As always, a quick search away.
Finalizing, there’s so much I’ve left out, but I want to reinforce the idea of Simplicity. Writing code to make you look clever isn’t a good idea. Even if you are all that clever, don’t try to create obfuscated code, making to many in only on row of code. This seems something good, but it completely costs you when you face the inability to quickly fix a bug in it. The point here, is that we shouldn’t make code even less comprehensible. The programmer should apply some skills of code refactoring. There are others ways of ending on Worse than Failure.
I would also recommend you to read Paul Stovell post “We are what we repeatedly code”.
Happy coding!
Search
Pages
Top Posts
- 15 Visual Studio .NET Add-Ins you won't live without
- Using SQLite in .NET
- How to get started with Silverlight Streaming
- Avoid chaos, don't let bugs take your project away
- Best-Ever Ad from Microsoft
Categories
- All
- Random tidbits
- Links
- Windows
- Fun
- Portuguese
- WebDev
- Programming
- Blogging
- Personal Experience
- Microsoft
- Tech
- OS
- Linux
- Security
- TV
- Software
- Databases
- Hardware
- WPF
Leave a Comment