Blog

Articles in category Clean code

Comments are deodorant for your code

2016-02-16 · Clean code Programming · Thomas Sundberg

Writing comments in a program is often considered a good habit. I hear people talking about code as "good and well commented". This always makes me skeptic. What do people mean with "well commented"? It turns out, they often mean that every method has a lot of comments.

Read more →

What is a good test?

2012-03-08 · Clean code Java TDD Test automation · Thomas Sundberg

A colleague asked that question the other day. What is a good test? It is a really difficult question to answer. There are a number of properties that hold true for a good test. I will list some of them and discuss why I think they are important. Others may think that the order of importance is different or that other properties are more important then those that I list.

I will show some examples using Java and JUnit of what I think are bad tests and how they can be refactored to something better.

Tests are automated in my world. I always try to avoid manually testing. The main reason for this is that manual testing is slow and hard to repeat arbitrary many times. Therefore, test should be read as automated test in for the rest of this text.

Read more →

How many train wrecks are lurking in your code?

2011-12-30 · Clean code Java · Thomas Sundberg

Train accidents are mostly considered to be bad things. People tend to get hurt when trains have accidents. Never the less, it is not so uncommon with train wrecks in software development.

Train wreck code is code that calls a method on the return value of another method call. This chain can be very long if a value is excavated deep down in a object graph.

Read more →

The simplest possible solution

2011-11-16 · Agile Clean code Java Software craftsmanship TDD · Thomas Sundberg

The simplest possible solution that could work. Ever heard that expression? What does it mean? Really?

The answer is obviously something that in a really simple way satisfies the test you currently are working on. Nothing more, nothing less.

Read more →

What is the difference between i++ and ++i?

2011-08-05 · Clean code Java Programming Teaching · Thomas Sundberg

During a coaching session I got a question about a loop.

Would there be any difference in the output from a loop defined as

for (int i = 0; i < 3; i++) {
    System.out.print(i + " ");
}

and a loop defined as

for (int i = 0; i < 3; ++i) {
    System.out.print(i + " ");
}

in Java?

Read more →

Magic numbers

2011-07-28 · Clean code · Thomas Sundberg

The other day a colleague asked me, what is a magic number? He had noticed that one of the things our Sonar was complaining about was Magic numbers. I got a great teaching moment and told him my view of Magic numbers.

Magic numbers are any numbers in the source code that just appears without any explanation.

Read more →