Keep It Simple, Stupid is the mantra by which I strive to develop solutions. But it wasn’t always like this. I have evolved as a developer as my skills and knowledge have gained the experience needed to make solid choices.
When I first started to write code, I had no idea what I was doing. I Just threw stuff together until it worked. Mind you this was well before the internet or the devil magic that is Stack Overflow. As I learned things, I applied them. It felt great! That is until I went back and tried to read things or worse yet explain them to another developer.
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” -- Brian Kernighan
It took me far too long to realize that clever code is usually bad code. Now, that isn’t to say it won’t solve the problem. It can solve the original problem. But if the solution is something that needs to be maintained or enhanced, clever code is not the best solution.
“Indeed, the ratio of time spent reading versus writing is well over 10 to 1. We are constantly reading old code as part of the effort to write new code. ...[Therefore,] making it easy to read makes it easier to write.” -- Robert C. Martin
The code I write now is hopefully understandable by all developers regardless of their individual experience level. At the very least, I can walk people through the code easier. Now I choose to write code that is more simple instead of landing there by accident.
This doesn’t mean the fewest lines of code to solve the problem, nor does it mean that I am writing the same code I would have when I was just starting out. It means I am trying to keep things simple. Less to read, while still easy to understand.
Here are some common tropes that I incorporate into my day-to-day development practices:
- Lint All the Things
- Keep it DRY (Don’t Repeat Yourself)
- Alpha-sort all the things
- No Magic Strings
- Naming is Hard
Lint All the Things
Using static analysis and coding standards is a great way to keep a code base readable by all. I try not to customize the linting too much, just take what comes with it and deal. That way the decisions are made for us, and there is no arguing over what should be used where and when.
Keep it DRY
Don’t Repeat Yourself, is a simple concept, but in code can be tricky. How much should you reduce duplication? Sometimes, it is better to duplicate parts in order to keep readability high. Like if you have to make dynamic class names in order for things to work… that might not be highly readable/traceable.
Alpha-Sort All the Things
Variables, constants, methods, functions, attributes, parameters, yes… all of it. Why? Because that is how our brains are wired. Sure it may make sense to group things into logical pieces of domain when writing them, but come back 2 months later and all that context is gone! Finding things when alphabetical is WAY easier!
No Magic Strings
While this may seem minor, having a random string inside your code can become very ambiguous over time. Worse, it can make changes and tracking hard. Put those strings (and numbers for that matter) into variables that explain what they are for.
Naming is Hard
It really is one of the hardest things we do. I need to name things so the person after me (which could even be me) will understand the intent of what I am trying to relay. Because of this I have four rules.
- Take the Time: You can put a dummy name in to start until you flesh out more details, but take the time at some point to have a descriptive and appropriate name.
- Appropriate Names: It shouldn’t be garbage, it shouldn’t be mis-leading. It should describe the intent.
- No Abbr. or Acronyms: Don’t abbreviate or use acronyms. Not everyone will know them, and it makes it harder to understand
- Short as Possible: This may seem to contradict the above rule, but do both.
Writing easy to comprehend code is extremely freeing. It helps everyone on the team level up and maintain a high performing solution. If you don’t believe me there are a ton of great resources out there to learn how to KISS. Read Clean Code by Robert C. Martin, The Art of Readable Code by Dustin Boswell and Trevor Foucher, or The Pragmatic Programmer by Andy Hunt and Dave Thomas.