Code Smell - Falmouth-Games-Academy/comp350-research-journal GitHub Wiki

Overview

Code Smells are characteristics of the source code of a program that can lead to the discovery of a deeper problem. Determining whether or not a program has "smelly code" is subjective and can vary due to a number of things including; the language the program is written in and the developer writing it 1(https://en.wikipedia.org/wiki/Code_smell).

Code Smells are not usually bugs, although they do increase the risk of the code developing bugs and failures. They can indicate weakness in the design that may cause problems down the line in the development cycle 1(https://en.wikipedia.org/wiki/Code_smell). Code smells don’t always indicate a problem. Long methods aren’t always bad but required for the program to work in the correct way 6( https://martinfowler.com/bliki/CodeSmell.html).

Locating code smells provide a good indication on if and where the refactoring of source code is required. Having knowledge of what code smells are is useful for any programmer as it makes spotting them much easier 2(http://wiki.c2.com/?CodeSmell). One of the pleasant qualities of code smells is that they are often easy to find even for the inexperienced, although some knowledge of the code is required 5(https://martinfowler.com/bliki/CodeSmell.html).

For information on identifying code smells, see Identifying Code Smells.

Occurrences

Smells can occur throughout all of source code both in and out of classes. They stem from poorly written comments to classes that have grown too large 3(https://blog.codinghorror.com/code-smells/). A small selection of coding smells is as follows: 4(https://sourcemaking.com/refactoring/smells)

  • Comments - Not explaining the "what" and maybe the "why" instead, perhaps the code can be refactored so they aren't even needed. What is obvious to one programmer may not be as clear to another.

  • Duplicated Code - Duplicated code should ideally be kept to a minimum. Duplication typically occurs when multiple programmers are working on different parts of the same program 7(https://refactoring.guru/smells/duplicate-code ). This can lead to similar looking code and as a result waste time and resources, even more so in bigger projects. Another example of duplication is when parts of the code look different but perform the same job, e.g. different variable names and addition/removal of code.

  • Dead Code - if it isn't being used, it doesn't need to be there! Dead code is a section of code in a program that gets executed but never used, effectively wasting computation time and memory. Such examples include: unused variables, dead functions and unreachable statements 8(https://www.aivosto.com/articles/deadcode.html). Dead code usually happens as a result of code changing (obsolete functions, replaced constants, etc.) and reusing (e.g. copying old code to a new foundation) as programs are often maintained, fixed and developed further 8(https://www.aivosto.com/articles/deadcode.html).

  • Inconsistent Naming Convention - a standard set of terms should be used in your code to ensure that it can be interpreted clearly and easily.

  • Long functions - Could be split into smaller more maintainable functions. This often goes unnoticed until the codebase turns into a large and unreadable block.

Some tips to avoid long functions include 9(https://refactoring.guru/smells/long-method):

To reduce the length of a method body, use Extract Method.

If local variables and parameters interfere with extracting a method, use Replace Temp with Query, Introduce Parameter Object or Preserve Whole Object.

If none of the previous recipes help, try moving the entire method to a separate object via Replace Method with Method Object.

Conditional operators and loops are a good clue that code can be moved to a separate method. For conditionals, use Decompose Conditional. If loops are in the way, try Extract Method.

References

[1] https://en.wikipedia.org/wiki/Code_smell
[2] http://wiki.c2.com/?CodeSmell
[3] https://blog.codinghorror.com/code-smells/
[4] https://sourcemaking.com/refactoring/smells
[5] https://martinfowler.com/bliki/CodeSmell.html
[6] https://github.com/Falmouth-Games-Academy/comp350-research-journal/wiki/Identifying-Code-Smells
[7] https://refactoring.guru/smells/duplicate-code
[8] https://www.aivosto.com/articles/deadcode.html
[9] https://refactoring.guru/smells/long-method