BlogProgramming on the Toilet

PoT Episode #4: Code Refactoring & Decompose Conditional Technique

By July 18, 2019 No Comments

What is it?

Programming on the Toilet is a document which contains tips, tricks, snippets or explanations about various programming technologies and paradigms, which could help developers in their everyday job. This episode was brought to you by Edin Begić, Junior Software Engineer at Atlantbh.

Code refactoring

Refactoring is the process of changing a software system in such a way that it does not alter the behaviour of the code yet improves its structure. It is a disciplined way to clean up code. In essence, when you refactor you are improving the design of the code after it has been written.

Code smells are a set of common signs which indicate that the written code is probably not good enough and is in need for refactoring. Redundant comments are one of many documented code smells. A comment is redundant if it describes something that adequately describes itself. An example is a Javadoc that says nothing more than (or less than) the function signature.

Comments should say things that the code cannot say for itself, i.e. intentions or why something is the way it is.

Refactoring techniques: Decompose conditional

When code consists of a complicated conditional (if-then-else) statement, usually refactoring should be applied. One solution is to decompose the complicated parts of the conditional into separate methods: the ,   and  . By extracting conditional code to methods named, so that the intention of that block of code is specified, you make life easier for the person who will be maintaining the code later.

Want to share these tips in your workplace or at home? Download this episode of Programming on the Toilet here.