This post will discuss more bad practices when coding that focus less on design patterns (post about anti-patterns here: https://kathleenmlaw.com/2018/12/03/some-software-development-anti-patterns/) but more so on problems in source code that may cause a bigger problem in the program, called code smells (reference found here: https://sourcemaking.com/refactoring/smells).
The most common code smells can be grouped into several categories. I have included 3 examples of each code smell that can arise, though there are other
- Bloaters – code (like classes or methods) that grows so large that it is difficult to work with
- Long methods – methods longer than 10 lines are probably too long
- Primitive obsession – use of lots of primitives in code rather than creating smaller objects, or many different constants
- Long parameter list – more than 3 or 4 parameters used when calling a method
- Object-orientation abusers – code that incorrectly implements object-oriented concepts
- Switch statements – switch statement has many cases, or conditional has multiple “if” branches in a row
- Temporary field – only have a value when needed by objects in the program, and are otherwise empty
- Alternative classes with different interfaces – two classes with the same function but only differ in method names
- Change preventers – code that, when changed in one spot, requires change in other parts of the code as well
- Divergent change – changing many methods that are unrelated when making change to a class
- Shotgun surgery – when making any changes to code, the developer has to make many other modifications to different classes
- Parallel inheritance hierarchies – when creating subclasses, the developer has to create subclasses for other classes at the same time
- Dispensables – unnecessary code that would increase readability when removed from the program
- Comments – when code is full of comments that it is difficult to even read the program
- Lazy class – a class that doesn’t really have much function in the overall program
- Data class – a class that only has fields and accessor/mutator methods and serves as a data collector for other classes to use
- Coupling – code that contributes to coupling (where one section of code may depend heavily on another section, and changes in the former results in forced changes in the latter)
- Feature envy – when a method accesses data of another object more than its own
- Inappropriate intimacy – when one class uses internal fields of another class
- Message chains – when one method calls another, which calls another….
I have absolutely made several of these mistakes in my programs, so I’ll be sure to keep a look-out when coding in the future.