Software Craftsmanship
Clean vs Messy Code
October 11, 2016

http://xkcd.com/844/
What is clean code?
- Reads like well-written prose
- Clarifies the designer's intent
- Sustains simple and direct modules
- Preserves straightforward logical flow
- Employs efficient abstractions
- Fosters maintenance and enhancement
- Provides one way to do one thing
- Presents a clear and minimal API
- Includes unit and acceptance tests
- Requires minimal dependencies
Clean code is the reward for elegant design, planning, and execution.
What is messy code?
- Smells bad
- Follows anti-patterns
- Includes spaghetti code
- Contains duplication
- Comprises complexity
- Obscures intent
- Complicates logical flow
- Perpetuates inconsistencies
- Becomes unmaintainable
Messy code is a symptom of poor design or some other problem.
//Build Request for each and every Token
public String buildRequest(String npi,int npi_length) {
if (npi_length==9)
return "{\"value\":\"" + npi + "\", \"client\": \""+ProducerSSNTokenize.line_of_business+"\", \"type\": \""+ProducerSSNTokenize.SSNString+"\"}";
else if (npi_length==16)
return "{\"value\":\"" + npi + "\", \"client\": \""+ProducerSSNTokenize.line_of_business+"\", \"type\": \""+ProducerSSNTokenize.PANString+"\"}";
else
return null;
/* if (npi_length==9)
return "{\"SSN\":\"" + npi + "\", \"Client\": \""+ProducerSSNTokenize.line_of_business+"\"}";
else if (npi_length==16)
return "{\"PAN\":\"" + npi + "\", \"Client\": \""+ProducerSSNTokenize.line_of_business+"\"}";
else
return null;*/
}private JsonObject getPayloadBody(String npiData, String client, String type) {
JsonBuilderFactory factory = Json.createBuilderFactory(null);
JsonObject payloadBody = factory.createObjectBuilder()
.add(JSON_VALUE_KEY, npiData)
.add(JSON_CLIENT_KEY, client)
.add(JSON_TYPE_KEY, type)
.build();
return payloadBody;
}The Perils of Messy Code

Mess builds, productivity of team decreases
Under pressure, the team makes more messes
Entropy - The amount of disorder in a system. When disorder increases in software, it is called "code rot".
The only way to develop quickly and meet deadlines is to keep the code as clean as possible at all times.

Code Smells
"A code smell is a surface indication that usually corresponds to a deeper problem in the system".
- Martin Fowler
Bloaters
- Long Method
- Large Class
-
Primitive Obsession
- Using multiple primitives to represent concepts instead of an object.
- Long Parameter List
-
Data Clumps
- Data that always appears together and in the same locations
OO Abusers
- Switch Statements
- Temporary Field
- Refused Bequest
- Alternative Classes with Different Interfaces
Change Preventers
- Divergent Change
- Shotgun Surgery
- Parallel Inheritance Hierarchies
The Dispensables
- Comments
- Duplicate Code
- Lazy Class
- Data Class
- Dead Code
- Speculative Generality
The Couplers
- Feature Envy
- Inappropriate Intimacy
- Method Chains
- Middle Man
Code Smells Activity
Software Craftsmanship - Clean vs Messy Code
By dyanos91
Software Craftsmanship - Clean vs Messy Code
Plano 2016 - 1.2
- 434