Comments – Introduction

All of us have written comments for our code. Many times we have relied on comments to try to make sense of the code we’re seeing. As Uncle Bob puts it in his book, “comments are, at best, a necessary evil” and shouldn’t be used carelessly. Why we need comments To put it simply, we…

Continue reading

Functions – The DRY Principle

A very important lesson that every programmer has to learn at some point, is that duplication is bad. This is what the DRY Principle addresses. What is duplication? Simply put, duplication is having lines of code in your application, that are repeated in multiple places. Whenever you copy and paste a snippet of code, you…

Continue reading

Functions – Exceptions Instead Of Error Codes

Maybe at some point you’ve written a function that had several possible results, and depending on the result, you had to do different things or show different error messages. One thing you might be tempted to do is have the function return a number representing the result code, or maybe the function returned an enumeration….

Continue reading

Functions – Command Query Separation

The idea of Command Query Separation is simple. You should have functions that perform actions that change the state of your system, and you should have functions that return (query) the current state of some part of the system, but your functions shouldn’t do both.  That means you should have Commands and Queries, but should…

Continue reading

Functions – Side Effects

What is a side effect? According to Wikipedia, a function has a side effect when “it has on observable interaction with calling functions or the outside world”. Uncle Bob says that side effects are lies. A side effect happens when a function is supposed to do something, but it also does other things you don’t…

Continue reading

Functions – Arguments

A very important thing you should have in mind when writing functions, is how many arguments each functions should have. Just like shorter functions are easier to understand, functions with few arguments are easier to understand and use. How many arguments should a function have? The short answer for this question is that a function…

Continue reading

Functions – Levels of Abstraction

In the previous post, we talked about the importance of functions doing one thing. We briefly touched on something called abstraction levels.  One of the simplest ways to be sure that your functions only do one thing, is to make sure that they stay on one abstraction level. Let’s try and explain this using a…

Continue reading

Functions – Smaller Is Better

This will be the first post covering the topic of functions. So far, this has probably been the approach to clean code that has been the hardest for me to wrap my head around. Not because it’s difficult, but because it is opposite to the way that I have been writing functions for my entire…

Continue reading

Meaningful Names – Other Tips

As a finish to the meaningful names part of our journey to produce clean code here are some other general tips mentioned in the book. Use Pronounceable Names Use names that you can pronounce. If you’ve followed the previous guidelines given, then most of your names should be clear and easy to say. Talking to…

Continue reading