Showing posts with label CleanCode. Show all posts
Showing posts with label CleanCode. Show all posts

Code Smells

Code smells are the indicators of potential flaws in the design and structure, not necessarily prevents the functionality but signifies the scope of improvement in implementation.

Code smells are prominent in legacy systems, developers hardly take chance to do major refactoring. However, all of these code smells can be detected and resolved during the development.

Most commonly can be identified by questioning the written code in following way,
  • Is there any duplication?
  • Is it difficult to understand?
  • Is is complicated to explain?
  • Is it difficult to test?
  • Will it be difficult to maintain?

If your answer is 'Yes' to any of these questions, most probably, your code contains one or more code smells.

Let's understand this with following code snippet,

If we check above class closely we can find following issues,

  • Six arguments makes the implementation complex and increases the responsibility of the function.
  • Method expects the caller to send the students in sequence which opens up the window for bug.
  • For new student, method needs to be changed,
  • Return statement is not required.
  • For invalid input method is not raising any exception.
  • The value of 50 is a magic number, difficut to understand what it is. 
  • Print method is again checking the student marks.
  • Subject parameter was not used.
  • ... and so on.
Many of the issues are nowadays identified by the smart IDEs. Out of these issues, few of them are actually code smells. 

Ask beforementioned questions again for this code and you will probably get 'Yes' for all the questions related to complexity, maintainability and readability of the code.

Following code smells are there in above class,
  • Long Parameter List
  • Duplication
  • Unnecessary Comments
  • Unnecessary Boxing-UnBoxing ( double & Double)
  • Unnecessarily Large Method
  • Dead Code 

By questioning your code you can identify such code smells and fix them for the sake of clean coding practice. There are many more code smells which can occur while writing the code. 

References: