Be a Developer, not a Blender

Good Practices that Helps

A lot of developers think that only design patterns could keep your project in order.

Good practices will help to keep your project healthy and clean. In this blog you will find several posts about clean code (01, 02, 03, 04, 05), but there are two practices that I would like to talk about: KISS and DRY.

KISS

Keep It Simple, Stupid! This is the best translation of the KISS acronym.

Why do we want to use complicated solutions instead of the easy one? Why don’t we reuse instead of writing new code?

I have found a code like below several times across projects:

if (value == null || value.trim().equals(“”)) {
    // do something
}

Instead of creating the code above, we could use the StringUtil from the Apache framework:

if (StringUtil.isBlank(value)) {
    // do something   
}

The advantage of using this frameworks is that we will reduce our code and increase the cohesion. Apache has open source code, tested by several developers across the world.

Always look for a simple solution to a problem. Imagine the following task:

  1. Create a payment consuming a webservice

Why some developers want to code right away instead of slicing this task first? With small tasks it will be easy to edit the code, easy to merge and review. We could have the task like:

  • Create the payment in DB
  • Consume the webservice
  • Update the payment in DB
  • Send an email

To finish about KISS, take a look at the code that I found:

customerTO.setBirthDay((customer.getBirthDay() != null) ? customer.getBirthDay() : null);

What is the purpose of that if statement? Why not doing customerTO.setBirthDay(customer.getBirthDay());?

DRY

Don’t Repeat Yourself” it is a simple approach and very useful in the OO world. If you remember seeing a block of code just like the one that you just wrote, go search for it.

The problem with code duplication is that we could duplicate some important business rule, and if a bug appears in one place of the code it would not be fixed in the duplicated code.

If you isolate the repeated code in classes with the name “Util”, “Helper”, “Factory” it will help other developers when searching for a code that is already created.

2 thoughts on “Be a Developer, not a Blender

Leave a Reply to Luiz Ricardo Cancel reply