code smell

19
Alex .C.Punnen Code Smells Alex.C.Punnen

Upload: alex-punnen

Post on 13-Apr-2017

154 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Code Smell

Alex .C.Punnen

Code Smells

Alex.C.Punnen

Page 2: Code Smell

Code Smell ?

Page 3: Code Smell

The problem

What is the problem with “smelly” ?

– It could be the indication of a problem – “Something is rotten in the state of Denmark”

• - Hamlet Act 1, scene 4

– Sometimes there might not be any problem at all

Page 4: Code Smell

Code Smell -The Dangers Is an indication of a violation of one or more basic

programming principles

– Programming Principles

The Open /Closed Principle - [wikilink] • Open for extension,closed for modification (thought of many

as the foundation principle of OOD)

The Principle of Abstraction-

• Abstracting to reduce duplicated code

The Liskov Substitution Principle [link]

– Reading -http://en.wikipedia.org/wiki/Category:Programming_principles

– Being aware is the beginning of the journey to programming salvation....

– Note – For most of these principles the Aha! effect happens when you experience it in real world

Page 5: Code Smell

Code Smell -Duplicate Code

•Problems

– Update Anomalies

– Hiding of intent

Identical or similar code spread in multiple places

Page 6: Code Smell

What to do -Duplicate Code

•Extract a method

Note -For unit tests a certain amount of code duplication may be needed, and could be tolerated

Page 7: Code Smell

Code Smell -Large Methods

• A method that does more than one thing., many things,sometimes unconnected things

•Problems

– Could indicates low levels of abstraction,low level of class design,reduced re-usability

– Harder to test,poor readability – http://c2.com/cgi/wiki?TallerThanMe

Page 8: Code Smell

What to do – Large methods

•Split to multiple methods

•Make the method

do just one thing

...well

Page 9: Code Smell

Code Smell -Large Class

• Okay you split into a zillion small methods and now you have a large class - http://en.wikipedia.org/wiki/Anti-pattern

•Problems

– Very hard to subclass,extend – kills OCP

– Why did Dinosaurs become extinct ?

Page 10: Code Smell

What to Do ? - Large Class •A Dinosaur class is created usually when there is no proper design done

•Pretty easy to create a Dinosaur; don't believe? – watch Jurassic Park

•Pretty hard to point out the ill effect to programmers who are not able to understand the concepts of abstraction, interfaces ,virtual functions and inheritance

•Solution

– Read more ,and do deliberate practice, design and re-factor your design at least N times. (N inversely proportional to experience)

– Read - http://en.wikipedia.org/wiki/Solid_(object-oriented_design)

Page 11: Code Smell

Other Code Smells

When you become more experienced, you might have a tendency to over engineer

– Deep Inheritance hierarchies with virtual base classes

– Complex frameworks /generalization where there is no reuse envisioned

It is knowledge that separates the wise from the rest; the former acutely aware of what they don't know, the latter acutely aware of what they know.....

Page 12: Code Smell

Code Smell

Programmer - “So how do I.. ? er.. smell it ?

It is easy ! use FindBugs,PMD,Checkstyle & other code analysis tools or/and integrate and check via SONAR

•Step 1. Find it when you write it ! – Integrate code analysis tools -FindBugs, PMD ,

CheckStyle

– with your IDE – Eclipse/NetBeans

– And/or Cobertura Plugin with Maven

•Step 2. Fix it as you write it.

Industry Best Practice- On May 13-14 2009, Google held a global fixit for FindBugs tool ..More than 700 engineers ran FindBugs from dozens of offices. Engineers filed more than 1,700 bug reports, of which 600 have already been marked as fixed More on the study

Page 13: Code Smell

Author - [email protected]

FindBugs Configuration in Eclipse

Make sure you review and

configure only relevant rules

Page 14: Code Smell

Author - [email protected]

PMD Configuration in Eclipse

Make sure you review and

configure only relevant rules

Page 15: Code Smell

So tell me Connoisseur, how does my code smell ?

Programmer -Okay I have fixed/not fixed things in Sonar,now how ?

Consultant – Okay let me see...

Page 16: Code Smell

So tell me Connoisseur, how does my code smell ?

• If you are aware and still you have chosen, then you would have a reason

• and if your reasons are right, then,

• You have balanced between reuse and unuse

• Balanced between elegance and purpose

• Balanced between effort spent and value gained

Page 17: Code Smell

That's all Folks

Backup Slides -->

Page 18: Code Smell

Statutory Warnings & License

•Acknowledgment

– Thanks to Wikipedia, Articles in ObjectMentor, C2.com & Google Images

•Advice Warning

– Some of the books which I have put here I have not personally read and I wont be liable for any monetary damage that you could possible incur by buying on my advise.

– Some of the advice that I have put here,I have not personally practiced- at least consciously, but looked like very good advise nevertheless; so though I could take credit on the mileage you might garner,I wouldn't be responsible for any adverse ill effects including mental strain and frustration you might experience in following my advise.

• License

– You are freely allowed (and encouraged) to pass on the advice and use these slides as long as you put these warnings and acknowledgments in your slides

Page 19: Code Smell

The Open/Closed Principle

The “Open/Closed Principle” (OCP) was first advocated by Bertrand Meyer in the first edition (1988) of his book “Object Oriented Software Construction” and is considered by many as “the most important principle of object-oriented design.

•Object-Oriented Software Construction is a book by Bertrand Meyer, widely considered a foundational text of object-oriented programming

http://en.wikipedia.org/wiki/Object-Oriented_Software_Construction