code smells

55
Software Craftsmanship Code Smells

Upload: mrinal-bhattacaharya

Post on 09-Apr-2017

646 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Code Smells

Software Craftsmanship

Code Smells

Page 2: Code Smells

Software Craftsmanship Manifesto

“Not only working software, but also well-crafted softwareNot only responding to change, but also steadily adding valueNot only individuals and interactions, but also a community of professionalsNot only customer collaboration, but also productive partnerships”

Page 3: Code Smells

Technical Debt

Software FinanceSkipping (or deferring) design Borrowing moneyRefactoring (i.e. behavior-preserving design improvements) Repaying principal

Slower development due to code smells Paying interest

Page 4: Code Smells

Code Smells

• Lost Intent• Inefficient Name• Duplicated Code• Deodorant Comment• Long Method• Large Class• Lazy Class More…

Page 5: Code Smells

Lost Intent

Just as a traffic sign may be obscured by shrubs, Code that doesn't easily communicate it's author's intention is hazardous: hard to understand, extend and modify.

Page 6: Code Smells

Lost IntentIntention-Revealing Code Lost Intent

The purpose of the code is clear. Low-level or complex logic obscures the code's purpose.

You can easily find the code you seek. The location of code makes little sense.

Every step of an algorithm is clearly defined.

The steps of the algorithm are blended together.

Page 7: Code Smells

Lost Intent

Page 8: Code Smells

Inefficient Name

Where The Streets Have No Name

Page 9: Code Smells

Inefficient Name

tmp = finder.fetch("0182901");Account theAccount = finder.fetch(transaction.getSource());account = finder.fetch( new StateMatcher("VA") );for(Account item : finder.fetchAll()) { … }

getDrctn()getDir()getDirection()

These are five lines chosen at random from a class.Each is grabbing a fetch result and putting it in a variable of type Account.

Page 10: Code Smells

Duplicated Code

If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.— Refactoring[page 76], Martin Fowler and Kent Beck

Page 11: Code Smells

Duplicated Code

Page 12: Code Smells

Deodorant Comment

Comments often are used as a deodorant. — Refactoring [page 87], Martin Fowler and Kent Beck

Page 13: Code Smells

Deodorant Comment

Page 14: Code Smells

Comment Guidelines

• Whenever possible make your code express the intent of the comment and remove the comment.

• Comments are to provide intent that is not expressible in code.

• Any comment that duplicates what the code says should be deleted.

Page 15: Code Smells

Long Method

A long method is too much to digest

Page 16: Code Smells

Long Method

BeforeLongMethod.txt AfterLongMethod.txt

Page 17: Code Smells

Large Class

Take on too many responsibilities

Page 18: Code Smells

Large Class

WebServicesProviderContoller• performValidationCB()• executeBusinessProcessCB()

Page 19: Code Smells

Lazy Class

A lazy class isn't doing enough to justify its existence.

Page 20: Code Smells

Lazy Class

Page 21: Code Smells

Lazy Class

Employee constructor.

Passing Jobs in Employee constructor.

Page 22: Code Smells

Oddball Solution

When a problem is solved one way throughout a system and the same problem is solved another way in the same system, one of the solutions is the oddball or inconsistent solution.

Page 23: Code Smells

Oddball Solution

Page 24: Code Smells

Primitive Obsession

Primitive Obsession exists when code solves a problem using a tool that's too simplistic.

Page 25: Code Smells

Primitive Obsession

Page 26: Code Smells

Switch Statement

Most times you see a switch statement you should consider polymorphism. — Refactoring, Martin Fowler and Kent Beck (page 82).

Page 27: Code Smells

Switch Statement

Move each leg of the conditional to an overriding method in a subclass. Make the original method abstract.

Page 28: Code Smells

Switch Statement

• Not every occurrence of a switch statement (or if...else if...else if... statements) should be replaced with a polymorphic solution.

Page 29: Code Smells

Speculative Generality

You get this smell when people say "Oh, I think we will need the ability to do that someday" and thus want all sorts of hooks and special cases to handle things that aren't required. — Refactoring, Martin Fowler and Kent Beck (page 83).

Page 30: Code Smells

Speculative Generality

Page 31: Code Smells

Long Parameter List

Methods that take too many parameters produce client code that is awkward and difficult to work with.

Page 32: Code Smells

Long Parameter List

user = userManager.create(USER_NAME, group, USER_NAME, “test", USER_NAME, LANGUAGE, false, false, new Date(), "blah", new Date());

Page 33: Code Smells

Conditional Complexity

Conditional logic is innocent in its infancy, when it is simple to understand and contained within a few lines of code. Unfortunately, it rarely ages well. — Joshua Kerievsky, Refactoring to Patterns, page 41.

Page 34: Code Smells

Conditional Complexity

Page 35: Code Smells

Combinatorial Explosion

When new combinations of data or behavior further bloat an already bloated design, you've got a Combinatorial Explosion smell.

Page 36: Code Smells

Combinatorial Explosion

Page 37: Code Smells

Alternative Classes With Different Interfaces

This subtle smell results when differences in the interfaces of similar classes leads to duplicated code.

Page 38: Code Smells

Alternative Classes With Different Interfaces

Page 39: Code Smells

Inappropriate Intimacy

Sometimes classes become far too intimate and spend too much time delving in each others' private parts.— Refactoring [page 85], Fowler and Beck

Page 40: Code Smells

Inappropriate Intimacy

Page 41: Code Smells

Indecent Exposure

We don't normally expose wires inside a wall or ceiling.

Page 42: Code Smells

Indecent Exposure

Page 43: Code Smells

Refused Bequest

Subclasses get to inherit the methods and data of their parents. But what if they don't want or need what they are given? — Refactoring[page 87], Martin Fowler and Kent Beck

Page 44: Code Smells

Refused Bequest

Page 45: Code Smells

Black Sheep

Page 46: Code Smells

Black Sheep

Page 47: Code Smells

Solution Sprawl

When code and/or data used to perform a responsibility becomes sprawled across numerous classes, Solution Sprawl is in the air. — Joshua Kerievsky, Refactoring to Patterns, page 43.

Page 48: Code Smells

Solution Sprawl

Page 49: Code Smells

Feature Envy

Page 50: Code Smells

Feature Envy

Page 51: Code Smells

Temporary Field

An object's field (a.k.a. instance variable) should have meaning during the full lifetime of the object.

Page 52: Code Smells

Temporary Field

Page 53: Code Smells

Side Effect

Page 54: Code Smells

Side Effect

Page 55: Code Smells

References

• Refactoring: Improving the Design of Existing Code by Martin Fowler

• Refactoring to Patterns by Joshua Kerievsky.• https://elearning.industriallogic.com/gh/subm

it?Action=AlbumContentsAction&album=recognizingSmells&devLanguage=Java