L14: Avoiding the "Bad" Flashcards
Code, design, and architecture smells (or anti-patterns)
Bad software is describe sing the methaphor of smells
- Code smells: any property of code that can hint at deeper problems
- Design smells: design structures that violate a design principle
- Architecture smells: undesired structures in the software architecture
Just symptoms, not defects by themselves!
Related, subjective, language/methodology-dependent, and overlapping
Technical debt (or design/code debt)
Metaphor for future rework needed due to poor software design/code
- Describes impact of poor software design (e.g. software smells)
- Can get worse over time (accumulates interest)
- Increases costs of implementing future changes (or makes future changes necessary)
Essentially:
- Long-term negative consequences of poor design…
- …that was motivated by short-term benefits
What causes technical debt?
- Deadlines
- Inappropriate planning
- Lack of well-defined processes
- Not adopting best practices
- Experience, knowledge, …
- Outdated/incomplete documentation
What are the consequences to technical debt?
- Low software quality
- Delays
- Low maintainability
- Rework
- Financial loss
- Dissatisfaction (team, stakeholders, …)
- Inadequate document
Refactoring
- Changes to a software’s internal structure…
- …to make it easier to comprehend, maintain, and modify …
- … without changing its (observable) behavior (so, fixing a bug, is not a refactoring, since it changes behavior)
Large and long/complex method
Properly abstracted methods/classes start small but often grow over time
Developers add new concerns to existing ones (i.e. do not separate concerns properly)
- Longer methods/classes are harder to understand
- May hide duplicate code and/or specific behavior
Closely related to “Spaghetti code” (code without a structure that is hard to maintain and comprehend)
What to do about the blob or god class?
- Separate concerns
- Extract classes/methods
- Introduce inheritance
- Separate into MVC
- Define interfaces
God class or the blob
A large (previous smell) class acts as controller of many data classes
Defines all functionalities and decisions (violating: separation of concerns, cohesion and coupling, single responsibility principle)
Long parameter list and what to do?
- Methods with too “many” parameters
- Merging different algorithms and controlling them via parameters
- Can be challenging to understand and use
So what to do?
- Check whether parameters are needed
- Define reference types for passing data
Alternative class with different interface
Exchangeable classes provide different interfaces (public methods) (remember strategy design pattern)
Makes it
- Harder to understand code
- Impossible to exchange behavior
What to do about long parameter list and alternative class with different interface?
- Following established design patterns and principles (strategy, template, abstract data types, …)
- Renaming methods, changing parameters, …
- Defining an inheritance hierarchy
- Contracts, documentation, and communication
Shotgun surgery and what to do?
- Modifying any code requires changes to various classes
- Violating single-responsibility as well as cohesion/coupling
What to do?
- Define proper software design
- Move methods or parameters
- Redefine units
Code clones or duplicate code
Code that occurs multiple times at different locations
- Copy and paste programming
- Clone and own development
Often used due to simplicity
Can lead to maintenance issues…
- Fixing bugs or updating each location
- Additional code
- Not properly separated
The problems of dealing with duplicated code
In the small (copy and paste):
- Identifying clones in a system (type 1 to type 4 clones)
- Deciding where to move a clone
- Duplicated abstractions
- Dealing with variations (e.g. template method)
In the large (clone and own):
- Merging long-living clones variants
- Redundant/new testing and features
- Identifying commonality and variations
- Expensive and time consuming
Dead code
Code that is (no longer) used or executed
- Unused variable
- Condition that is never reached
- Class that is not relevant/used anymore
Extensive support in modern IDEs (as far as identifiable considering the given visibility)