Garren Smith

The Perfect Kiss

. 9 Sep 2009 - Johannesburg

The acronym KISS (Keep It Simple Stupid) is used a lot when talking about code. What does it actually mean and how do we actually adhere to this principle?
Programmers justify many evil things in the name of KISS. Things like adding unnecessary complexity, procedural code and god objects spring to mind... And don't think that bundling code up in comments suddenly makes it all simpler.
But what does it actually mean to keep your code simple?
In Lean Software Development the elimination of waste by removing unnecessary code and functionality would be keeping it simple. Extreme Programming says that simple code:
  1. Runs all the tests.
  2. Expresses every idea that we need to express.
  3. Says everything OnceAndOnlyOnce.
  4. Has no superfluous parts.
Sounds good to me, and this is how I find it fits into practice. Maintainable code Maintainable Code is code that after a nice long summer holiday on the beach is still easy to add to or change. That means good code coverage, so that if I make a change the tests will tell me if I have broken anything. The code's tests must be self documenting, if I want to know how to use the code I can look through the tests for a explanation. Readable code This follows on from maintainable code - code must be easy to read. Having to stare at a for loop for hours to understand what it is doing is quite tedious. Comments don't necessarily make code more readable. As Kent Beck said through Ron Jeffries "comment is the code's way of telling us that it wants to be more clear". Making code modular and keeping methods short, with loosely coupled code and good level of extraction all help contribute to making code more readable. Simplest possible An extreme principle that is very valid here is Doing the simplest thing that could possible work. This is by far the hardest thing to do and requires dedication and focus. Refactoring is helpful here to remove complex code. Making code simpler will make the code more maintainable and easier to read and ultimately cleaner. Short Kiss Goodbye From my short experience in software development these principles have proved invaluable, however if I have left anything out or there are other things to consider, please add them in the comments and if they are really good, I will update them in the blog.
blog comments powered by Disqus
Fork me on GitHub