Software Craftsmanship

Clean vs Messy Code

October 24, 2016

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.

http://xkcd.com/1513/

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.

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
    • When the subclass refuses some of the behavior of the parent class
  • Alternative Classes with Different Interfaces
    • ​Two classes perform identical functions but have different method names

Change Preventers

  • Divergent Change
    • ​A new feature causes many changes in your class
  • Shotgun Surgery
    • A new feature causes a change in many classes
  • Parallel Inheritance Hierarchies
    • ​Whenever you create a subclass for a class, you find yourself needing to create a subclass for another class

The Dispensables

  • Lazy class, Dead Code, Speculative Generality
    • ​If the code isn't used, remove it!
  • Data class
    • The true power of objects is that they can contain behavior types or operations on their data!
  • Duplicate Code

The Couplers

  • Feature Envy
    • ​A method accesses the data of another object more than its own data
  • Inappropriate Intimacy
    • ​One class uses the internal fields and methods of another class
  • Message Chains
  • Middle Man

https://gist.github.com/ClassicThunder/2132ccc8154de4d13fb4034973880ce5

Software Craftsmanship - Clean vs Messy Code (Alyssa)

By cof_softwarecraftsmanship

Software Craftsmanship - Clean vs Messy Code (Alyssa)

2016 - Day 1.3

  • 403