from stupid to solid code
DESCRIPTION
From stupid to solid code Really helpfulTRANSCRIPT
![Page 1: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/1.jpg)
FROM STUPID TO SOLID CODE
March 28, 2015
FLORIN OLARIU
CENTRIC IT SOLUTION ROMANIA
![Page 2: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/2.jpg)
ABOUT ME
• I’m working in field since 1996
• I’ve started as Java developer– I’m SCJP certified
• I’ve continued as team leader for a mixt team(Java/.NET
developers working with Waterfall methodologies)
• I’ve continued with .NET technologies since in 2003– I’m MCPD Windows Forms 2.0 certified – since 2006
– I’m MCPD Windows Forms 4.0 certified – since 2010
– I’m MCPD Web Forms 4.0 certified – since 2011
• Technical Lead/Scrum Master in Centric
March 28, 2015TITLE PRESENTATION
![Page 3: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/3.jpg)
WHAT ABOUT?
Don't Be Stupid, Grasp Solid
March 28, 2015TITLE PRESENTATION
![Page 4: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/4.jpg)
AGENDA
March 28, 2015TITLE PRESENTATION
![Page 5: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/5.jpg)
STUPID CODE
March 28, 2015TITLE PRESENTATION
Stupid code, seriously?
![Page 6: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/6.jpg)
STUPID CODE
• Singleton
• Tight coupling
• Untestability
• Premature optimization
• Indescriptive naming
• Duplication
March 28, 2015TITLE PRESENTATION
![Page 7: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/7.jpg)
SINGLETON
• The most well-known pattern used• Program using global state are very difficult to test
• Program that rely on global state hide their dependencies
March 28, 2015TITLE PRESENTATION
![Page 8: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/8.jpg)
TIGHT COUPLING
• Aka : strong coupling
• The main goal is to reduce coupling between modules• Difficult to reuse
• Hard to test
• Coupling - the degree to which each program module relies
on each one of the other modules.
March 28, 2015TITLE PRESENTATION
![Page 9: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/9.jpg)
UNTESTABILITY
• The unit tests should be easy
• Untestability is cause by tight coupling
March 28, 2015TITLE PRESENTATION
![Page 10: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/10.jpg)
PREMATURE OPTIMIZATION
• Donald Knuth said: premature optimization is the root of all
evil. There is only cost, and no benefit.
• Two rules to optimize an application:• don't do it;
• (for experts only!) don't do it yet.
March 28, 2015TITLE PRESENTATION
![Page 11: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/11.jpg)
INDESCRIPTIVE NAMING
• Programming languages are for humans.
• “Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
– Martin Fowler
March 28, 2015TITLE PRESENTATION
![Page 12: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/12.jpg)
DUPLICATION
• DRY – don’t repeat yourself
• KISS – keep it simple(stupid)
• Be lazy in the right way – write code only once.
March 28, 2015TITLE PRESENTATION
![Page 13: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/13.jpg)
CODING KATA - INTRO
March 28, 2015TITLE PRESENTATION
![Page 14: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/14.jpg)
CODING KATA - INTRO
March 28, 2015TITLE PRESENTATION
![Page 15: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/15.jpg)
CODING KATA - INTRO
• KATA – came from martial arts
• Dave Thomas – has introduced Kata as learning technique
March 28, 2015TITLE PRESENTATION
![Page 16: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/16.jpg)
DEMO - I
• Fibonacci – Kata TDD
March 28, 2015TITLE PRESENTATION
![Page 17: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/17.jpg)
SOLID PRINCIPLES
• Single Responsibility Principle - SRP
• Open-Closed Principle - OCP
• Liskov Principle - LSP
• Interface Segregation Principle - ISP
• Dependency Inversion Principle - DIP
March 28, 2015TITLE PRESENTATION
![Page 18: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/18.jpg)
JENGA GAME
March 28, 2015TITLE PRESENTATION
![Page 19: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/19.jpg)
SOLID
March 28, 2015TITLE PRESENTATION
![Page 20: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/20.jpg)
SINGLE RESPONSIBILITY PRINCIPLE
A class should have
only one reason to
change.
March 28, 2015TITLE PRESENTATION
![Page 21: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/21.jpg)
SINGLE RESPONSIBILITY
March 28, 2015TITLE PRESENTATION
![Page 22: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/22.jpg)
SINGLE RESPONSIBILITY - TIPS
• Split big classes
• Use layers
• Avoid god classes
• Write straightforward comments
March 28, 2015TITLE PRESENTATION
![Page 23: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/23.jpg)
OPEN-CLOSED PRINCIPLE - OCP
Software entities
should be open for
extension and closed
for modification.
March 28, 2015TITLE PRESENTATION
![Page 24: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/24.jpg)
OPEN-CLOSED PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 25: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/25.jpg)
OPEN-CLOSED PRINCIPLE - TIPS
• Make all members variables private
• No global variables
• Avoid setters (as much as possible)
March 28, 2015TITLE PRESENTATION
![Page 26: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/26.jpg)
LISKOV SUBSTITUTION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 27: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/27.jpg)
LISKOV SUBSTITUTION PRINCIPLE
• LSP states that objects in a
program should
be replaceable with
instances of their subtypes
without altering the
correctness of the program.
March 28, 2015TITLE PRESENTATION
![Page 28: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/28.jpg)
LISKOV SUBSTITUTION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 29: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/29.jpg)
LISKOV SUBSTITUTION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 30: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/30.jpg)
LISKOV SUBSTITUTION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 31: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/31.jpg)
INTERFACE SEGREGATION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 32: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/32.jpg)
INTERFACE SEGREGATION PRINCIPLE
•ISP states
that many client-
specific interfaces are
better than one general-
purpose interface.
March 28, 2015TITLE PRESENTATION
![Page 33: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/33.jpg)
INTERFACE SEGREGATION PRINCIPLE
•You should not have to
implement methods that you
don't use. Enforcing ISP
gives you low coupling,
and high cohesion.
March 28, 2015TITLE PRESENTATION
![Page 34: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/34.jpg)
DEPENDENCY INVERSION PRINCIPLE
March 28, 2015TITLE PRESENTATION
![Page 35: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/35.jpg)
DEPENDENCY INVERSION PRINCIPLE
•DIP has 2 key points:
March 28, 2015TITLE PRESENTATION
![Page 36: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/36.jpg)
DEPENDENCY INVERSION PRINCIPLE
•DIP has 2 key points:–Abstractions should not depend upon
details – both should depend on
abstractions
–Details should not depend upon
abstraction – details should depend
on abstractions
March 28, 2015TITLE PRESENTATION
![Page 37: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/37.jpg)
DEPENDENCY INVERSION PRINCIPLE
•Note•DIP is not the same like
Dependency Injection
March 28, 2015TITLE PRESENTATION
![Page 38: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/38.jpg)
DEPENDENCY INVERSION PRINCIPLE
•Note•Dependency Injection is about
how one object knows about
another dependent object.
March 28, 2015TITLE PRESENTATION
![Page 39: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/39.jpg)
DEPENDENCY INVERSION PRINCIPLE
•Note•In other words, it is about how
one object acquires a
dependency.
March 28, 2015TITLE PRESENTATION
![Page 40: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/40.jpg)
DEPENDENCY INVERSION PRINCIPLE
•http://stackoverflow.com/questi
ons/6766056/dip-vs-di-vs-ioc
March 28, 2015TITLE PRESENTATION
![Page 41: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/41.jpg)
ON MORE THING…
March 28, 2015TITLE PRESENTATION
![Page 42: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/42.jpg)
SMILES
March 28, 2015TITLE PRESENTATION
![Page 43: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/43.jpg)
SMILES
• Give a man a program, frustrate him for a day. Teach a man to
program frustrate him for a lifetime - Muhammad Waseem
March 28, 2015TITLE PRESENTATION
![Page 44: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/44.jpg)
SMILES
• Give a man a program, frustrate him for a day. Teach a man to
program frustrate him for a lifetime - Muhammad Waseem
• What is the object-oriented way of getting rich?
March 28, 2015TITLE PRESENTATION
![Page 45: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/45.jpg)
SMILES
• Give a man a program, frustrate him for a day. Teach a man to
program frustrate him for a lifetime - Muhammad Waseem
• What is the object-oriented was of getting rich? Inheritance of
course.
March 28, 2015TITLE PRESENTATION
![Page 46: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/46.jpg)
SMILES
• Give a man a program, frustrate him for a day. Teach a man to
program frustrate him for a lifetime - Muhammad Waseem
• What is the object-oriented was of getting rich? Inheritance of
course.
• Walking on water and developing software to specification are
easy ….
March 28, 2015TITLE PRESENTATION
![Page 47: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/47.jpg)
SMILES
• Give a man a program, frustrate him for a day. Teach a man to
program frustrate him for a lifetime - Muhammad Waseem
• What is the object-oriented was of getting rich? Inheritance of
course.
• Walking on water and developing software to specification are
easy as long as both are frozen.
March 28, 2015TITLE PRESENTATION
![Page 48: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/48.jpg)
CONCLUSIONS
• Use your brain
• Writing SOLID code is not that hard
• Avoid STUPID code
March 28, 2015TITLE PRESENTATION
![Page 49: From Stupid to Solid Code](https://reader033.vdocuments.site/reader033/viewer/2022051005/55cf8f2f550346703b99c4f5/html5/thumbnails/49.jpg)
QUESTIONS?