SlideShare a Scribd company logo
REFACTORING CODE TO A SOLID
FOUNDATION
Adnan Masood
http://blog.adnanmasood.com
@adnanmasood
adnanmasood@acm.org
Presented at Inland Empire .NET UG –
12/11/2012
EXAMPLE APP: BEFORE AND
AFTER
OR HOW TO GET FROM HERE TO THERE?
Before After
CREDITS & REFERENCES
• Robert C Martin – Clean Coder https://sites.google.com/site/unclebobconsultingllc/
• Pablo’s SOLID Software Development | LosTechies.com
Derick Bailey - McLane Advanced Technologies, LLC
• PluralSight Training - SOLID Principles of Object Oriented Design
http://pluralsight.com/training/Courses/TableOfContents/principles-oo-design
• Marcin Zajączkowski - http://solidsoft.wordpress.com/
• Images Used Under License
• http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx
• SRP Article
• http://www.objectmentor.com/resources/articles/srp.pdf
• Solid for your Language
• http://stefanroock.files.wordpress.com/2011/09/solidforyourlanguage.pdf
HELP :ABOUT
Adnan Masood works as a system architect / technical lead for Green dot Corporation where he
develops SOA based middle-tier architectures, distributed systems, and web-applications using
Microsoft technologies. He is a Microsoft Certified Trainer holding several technical certifications,
including MCPD (Enterprise Developer), MCSD .NET, and SCJP-II.Adnan is attributed and published in
print media and on the Web; he also teaches Windows Communication Foundation (WCF) courses at
the University of California at San Diego and regularly presents at local code camps and user groups. He
is actively involved in the .NET community as cofounder and president of the of San GabrielValley .NET
Developers group.
Adnan holds a Master’s degree in Computer Science; he is currently a doctoral student working
towards PhD in Machine Learning; specifically discovering interestingness measures in outliers using
Bayesian Belief Networks. He also holds systems architecture certification from MIT and SOA Smarts
certification from Carnegie Melon University.
ADDITIONAL RESOURCES
Uncle Bob’s Principle Of Object Oriented Development:
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod
Pablo’sTopic OfThe Month: SOLID
http://www.lostechies.com/blogs/chad_myers/archive/2008/03/07/pablo-
s-topic-of-the-month-march-solid-principles.aspx
Agile Principles, Patterns,And Practices In C#
by Robert (Uncle Bob) Martin and Micah Martin
Pablo’s SOLID E-Book
http://www.lostechies.com/content/pablo_ebook.aspx
COHESION:
“A measure of how strongly-related and focused the various
responsibilities of a software module are” - Wikipedia
OBJECT ORIENTED PRINCIPLES - QUIZ
COUPLING:
“The degree to which each program module relies on each one of the
other modules” – Wikipedia
ENCAPSULATION:
“The hiding of design decisions in a computer program that are most likely
to change” -Wikipedia
SRP: SINGLE RESPONSIBILITY PRINCIPLE
OCP: OPEN CLOSED PRINCIPLE
LSP: LISKOV SUBSTITUTION PRINCIPLE
ISP: INTERFACE SEGREGATION PRINCIPLE
DIP: DEPENDENCY INVERSION PRINCIPLE
S.O.L.I.D. PRINCIPLES
THE SINGLE RESPONSIBILITY
PRINCIPLE
SRP:THE SINGLE RESPONSIBILITY
PRINCIPLE
The Single Responsibility Principle states that every object should
have a single responsibility, and that responsibility should be entirely
encapsulated by the class.
Wikipedia
There should never be more than one reason for a class to change.
Robert C.“Uncle Bob” Martin
COHESION AND COUPLING
 Cohesion: how strongly-related and focused are the
various responsibilities of a module
 Coupling: the degree to which each program module
relies on each one of the other modules
Strive for low coupling and high
cohesion!
RESPONSIBILITIES ARE AXES OF
CHANGE
 Requirements changes typically map to responsibilities
 More responsibilities == More likelihood of change
 Having multiple responsibilities within a class couples
together these responsibilities
 The more classes a change affects, the more likely the
change will introduce errors.
Demo
The ProblemWith Too Many Responsibilities
WHAT IS A RESPONSIBILITY?
“a reason to change”
A difference in usage scenarios from the
client’s perspective
Multiple small interfaces (follow ISP) can
help to achieve SRP
SRP: SINGLE RESPONSIBILITY
EXAMPLE APP: READ A FLAT FILE AND SEND AN EMAIL
EXAMPLE APP NEW REQUIREMENTS: SEND FROM NON-WINFORMS APP. READ XML OR FLAT
FILE
SRP: SINGLE RESPONSIBILITY
EXAMPLE APP: A BETTER STRUCTURE
SUMMARY
 Following SRP leads to lower coupling and higher cohesion
 Many small classes with distinct responsibilities result in a more flexible
design
 Related Fundamentals:
o Open/Closed Principle
o Interface Segregation Principle
o Separation of Concerns
 Recommended Reading:
o Clean Code by Robert C. Martin [http://amzn.to/Clean-Code]
THE OPEN / CLOSED
PRINCIPLE
OCP:THE OPEN/CLOSED PRINCIPLE
The Open / Closed Principle states that
software entities (classes, modules, functions,
etc.) should be open for extension, but closed
for modification
Wikipedia
THE OPEN / CLOSED PRINCIPLE
Open to Extension
New behavior can be added in the future
Closed to Modification
Changes to source or binary code are not required
Dr. Bertrand Meyer originated the OCP term in his 1988
book, Object Oriented Software Construction
CHANGE BEHAVIORWITHOUT
CHANGING CODE?
Rely on abstractions
No limit to variety of implementations of each abstraction
In .NET, abstractions include:
 Interfaces
 Abstract Base Classes
In procedural code, some level of OCP can be
achieved via parameters
THE PROBLEM
 Adding new rules require changes to the calculator every time
 Each change can introduce bugs and requires re-testing, etc.
 We want to avoid introducing changes that cascade through many
modules in our application
 Writing new classes is less likely to introduce problems
o Nothing depends on new classes (yet)
o New classes have no legacy coupling to make them hard to design or test
THREE APPROACHESTO ACHIEVE
OCP
 Parameters (Procedural Programming)
o Allow client to control behavior specifics via a parameter
o Combined with delegates/lambda, can be very powerful approach
 Inheritance /Template Method Pattern
o Child types override behavior of a base class (or interface)
 Composition / Strategy Pattern
o Client code depends on abstraction
o Provides a “plug in” model
o Implementations utilize Inheritance; Client utilizes Composition
WHEN DO WE APPLY OCP?
 Experience Tells You
If you know from your own experience in the problem domain that a particular
class of change is likely to recur, you can apply OCP up front in your design
Otherwise – “Fool me once, shame on you; fool me twice, shame on me”
 Don’t apply OCP at first
 If the module changes once, accept it.
 If it changes a second time, refactor to achieve OCP
Remember TANSTAAFL
 There Ain’t No Such Thing As A Free Lunch
 OCP adds complexity to design
 No design can be closed against all changes
SUMMARY
 Conformance to OCP yields flexibility, reusability, and maintainability
 Know which changes to guard against, and resist premature abstraction
 Related Fundamentals:
o Single Responsibility Principle
o Strategy Pattern
o Template Method Pattern
 Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C.
Martin and Micah Martin [http://amzn.to/agilepppcsharp]
THE LISKOV SUBSTITUTION
PRINCIPLE
LSP:THE LISKOV SUBSTITUTION
PRINCIPLE
The Liskov Substitution Principle states that
Subtypes must be substitutable for their base
types.
Agile Principles, Patterns, and
Practices in C#
Named for Barbara Liskov, who first described the principle in 1988.
SUBSTITUTABILITY
Child classes must not:
1) Remove base class behavior
2) Violate base class invariants
And in general must not require calling code to know they are
different from their base type.
INHERITANCE ANDTHE IS-A
RELATIONSHIP
Naïve OOP teaches use of IS-A to describe child classes’
relationship to base classes
LSP suggests that IS-A should be replaced with
IS-SUBSTITUTABLE-FOR
LSP: LISKOV SUBSTITUTION PRINCIPLE
EXAMPLE APP: VIOLATING LSP WITH DATABASE CONNECTION INFO
LSP: LISKOV SUBSTITUTION PRINCIPLE
EXAMPLE APP: CORRECTING FOR LSP – MOVE THE DATABASE READER
INVARIANTS
 Consist of reasonable assumptions of behavior by clients
 Can be expressed as preconditions and postconditions for methods
 Frequently, unit tests are used to specify expected behavior of a method
or class
 Design By Contract is a technique that makes defining these pre- and post-
conditions explicit within code itself.
 To follow LSP, derived classes must not violate any constraints defined (or
assumed by clients) on the base classes
THE PROBLEM
 Non-substitutable code breaks polymorphism
 Client code expects child classes to work in place of
their base classes
 “Fixing” substitutability problems by adding if-then
or switch statements quickly becomes a
maintenance nightmare (and violates OCP)
LSPVIOLATION “SMELLS”
foreach (var emp in Employees)
{
if( emp is Manager )
{
_printer.PrintManager( emp as Manager );
}
else
{
_printer.PrintEmployee( emp );
}
}
LSPVIOLATION “SMELLS”
public abstract class Base
{
public abstract void Method1();
public abstract void Method2();
}
public class Child : Base
{
public override void Method1()
{
throw new NotImplementedException();
}
public override void Method2()
{
// do stuff
}
}
Follow ISP!
Use small interfaces so you don’t
require classes to implement
more than they need!
WHEN DO WE FIX LSP?
If you notice obvious smells like those shown
If you find yourself being bitten by the OCP
violations LSP invariably causes
LSPTIPS
 “Tell, Don’t Ask”
o Don’t interrogate objects for their internals – move behavior to the object
o Tell the object what you want it to do
 Consider Refactoring to a new Base Class
o Given two classes that share a lot of behavior but are not substitutable…
o Create a third class that both can derive from
o Ensure substitutability is retained between each class and the new base
SUMMARY
 Conformance to LSP allows for proper use of polymorphism and produces more
maintainable code
 Remember IS-SUBSTITUTABLE-FOR instead of IS-A
 Related Fundamentals:
o Polymorphism
o Inheritance
o Interface Segregation Principle
o Open / Closed Principle
 Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
THE INTERFACE SEGREGATION
PRINCIPLE
ISP:THE INTERFACE SEGREGATION
PRINCIPLE
The Interface Segregation Principle states that
Clients should not be forced to depend on
methods they do not use.
Agile Principles, Patterns, and Practices in C#
Corollary:
Prefer small, cohesive interfaces to “fat” interfaces
WHAT’S AN INTERFACE?
Interface keyword/type
public interface IDoSomething { … }
Public interface of a class
public class SomeClass { … }
What does the client see and use?
ISP: INTERFACE
SEGREGATION PRINCIPLE
“THIS PRINCIPLE DEALS WITH THE DISADVANTAGES OF ‘FAT’
INTERFACES. CLASSES THAT HAVE ‘FAT’ INTERFACES ARE CLASSES
WHOSE INTERFACES ARE NOT COHESIVE. IN OTHER WORDS, THE
INTERFACES OF THE CLASS CAN BE BROKEN UP INTO GROUPS OF
MEMBER FUNCTIONS. EACH GROUP SERVES A DIFFERENT SET OF
CLIENTS. THUS SOME CLIENTS USE ONE GROUP OF MEMBER
FUNCTIONS, AND OTHER CLIENTS USE THE OTHER GROUPS.”
- ROBERT MARTIN
Demo
Violating ISP
ISP: INTERFACE
SEGREGATION PRINCIPLE
EXAMPLE APP: CLARIFYING THE EMAIL SENDER AND MESSAGE INFO PARSING
THE PROBLEM
Client Class (Login Control) NeedsThis:
THE PROBLEM
 AboutPage simply needs ApplicationName and AuthorName
 Forced to deal with huge ConfigurationSettings class
 Forced to deal with actual configuration files
Interface Segregation violations result in classes that depend on
things they do not need, increasing coupling and reducing flexibility
and maintainability
ISP SMELLS
 Unimplemented interface methods:
public override string ResetPassword(
string username, string answer)
{
throw new NotImplementedException();
}
Remember these violate Liskov Substitution Principle!
ISP SMELLS
Client references a class but only uses small
portion of it
WHEN DO WE FIX ISP?
 Once there is pain
o If there is no pain, there’s no problem to address.
 If you find yourself depending on a “fat” interface you own
o Create a smaller interface with just what you need
o Have the fat interface implement your new interface
o Reference the new interface with your code
 If you find “fat” interfaces are problematic but you do not own them
o Create a smaller interface with just what you need
o Implement this interface using an Adapter that implements the full interface
ISPTIPS
 Keep interfaces small, cohesive, and focused
 Whenever possible, let the client define the interface
Whenever possible, package the interface
with the client
o Alternately, package in a third assembly client and
implementation both depend upon
o Last resort: Package interfaces with their implementation
SUMMARY
 Don’t force client code to depend on things it doesn’t need
 Keep interfaces lean and focused
 Refactor large interfaces so they inherit smaller interfaces
 Related Fundamentals:
o Polymorphism
o Inheritance
o Liskov Substitution Principle
o Façade Pattern
 Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and
Micah Martin [http://amzn.to/agilepppcsharp]
THE DEPENDENCY INVERSION
PRINCIPLE
DIP: DEPENDENCY INVERSION
PRINCIPLE
“WHAT IS IT THAT MAKES A DESIGN RIGID, FRAGILE AND IMMOBILE? IT IS THE
INTERDEPENDENCE OF THE MODULES WITHIN THAT DESIGN. A DESIGN IS RIGID IF
IT CANNOT BE EASILY CHANGED. SUCH RIGIDITY IS DUE TO THE FACT THAT A
SINGLE CHANGE TO HEAVILY INTERDEPENDENT SOFTWARE BEGINS A CASCADE OF
CHANGES IN DEPENDENT MODULES.”
- ROBERT MARTIN
DIP:THE DEPENDENCY INVERSION
PRINCIPLE
High-level modules should not depend on low-
level modules. Both should depend on
abstractions.
Abstractions should not depend on details. Details
should depend on abstractions.
o Agile Principles, Patterns, and Practices in C#
DIP: DEPENDENCY
INVERSION PRINCIPLE
DIP: DEPENDENCY
INVERSION PRINCIPLE
DIP: DEPENDENCY
INVERSION PRINCIPLE
DIP: DEPENDENCY
INVERSION
PRINCIPLE
EXAMPLE APP: CONSTRUCTOR DEPENDENCIES IN A PROCESSING SERVICE
DIP: DEPENDENCY
INVERSION
PRINCIPLE
WHAT ARE DEPENDENCIES?
 Framework
 Third Party Libraries
 Database
 File System
 Email
 Web Services
 System Resources (Clock)
 Configuration
 The new Keyword
 Static methods
 Thread.Sleep
 Random
TRADITIONAL PROGRAMMING AND
DEPENDENCIES
 High Level Modules Call Low Level Modules
 User Interface depends on
o Business Logic depends on
o Infrastructure
o Utility
o Data Access
 Static methods are used for convenience or as Façade layers
 Class instantiation / Call stack logic is scattered through all modules
o Violation of Single Responsibility Principle
CLASS DEPENDENCIES: BE HONEST!
 Class constructors should require any dependencies the class needs
 Classes whose constructors make this clear have explicit dependencies
 Classes that do not have implicit, hidden dependencies
public class HelloWorldHidden
{
public string Hello(string name)
{
if (DateTime.Now.Hour < 12) return "Good morning, " + name; if (DateTime.Now.Hour
< 18) return "Good afternoon, " + name; return "Good evening, " + name;
}
}
CLASSES SHOULD DECLARE WHAT
THEY NEED
public class HelloWorldExplicit
{
private readonly DateTime _timeOfGreeting;
public HelloWorldExplicit(DateTime timeOfGreeting)
{
_timeOfGreeting = timeOfGreeting;
}
public string Hello(string name)
{
if (_timeOfGreeting.Hour < 12) return "Good morning, " + name;
if (_timeOfGreeting.Hour < 18) return "Good afternoon, " + name;
return "Good evening, " + name;
}
}
THE PROBLEM
 Order has hidden dependencies:
o MailMessage
o SmtpClient
o InventorySystem
o PaymentGateway
o Logger
o DateTime.Now
 Result
o Tight coupling
o No way to change implementation details (OCP violation)
o Difficult to test
DEPENDENCY INJECTION
 Dependency Injection is a technique that is used to allow calling code to inject
the dependencies a class needs when it is instantiated.
 The Hollywood Principle
o “Don’t call us; we’ll call you”
 Three PrimaryTechniques
o Constructor Injection
o Property Injection
o Parameter Injection
 Other methods exist as well
CONSTRUCTOR INJECTION
 Dependencies are passed in via constructor
Strategy
Pattern
 Pros
o Classes self-document what they need to perform their work
o Works well with or without a container
o Classes are always in a valid state once constructed
 Cons
o Constructors can have many parameters/dependencies (design smell)
o Some features (e.g. Serialization) may require a default constructor
o Some methods in the class may not require things other methods require (design smell)
PROPERTY INJECTION
 Dependencies are passed in via a property
o Also known as “setter injection”
 Pros
o Dependency can be changed at any time during object lifetime
o Very flexible
 Cons
o Objects may be in an invalid state between construction and setting of dependencies via
setters
o Less intuitive
PARAMETER INJECTION
 Dependencies are passed in via a method parameter
 Pros
o Most granular
o Very flexible
o Requires no change to rest of class
 Cons
o Breaks method signature
o Can result in many parameters (design smell)
 Consider if only one method has the dependency, otherwise prefer constructor
injection
REFACTORING
Extract Dependencies into Interfaces
Inject implementations of interfaces into
Order
Reduce Order’s responsibilities (apply SRP)
DIP SMELLS
 Use of new keyword
foreach(var item in cart.Items)
{
try
{
var inventorySystem = new InventorySystem();
inventorySystem.Reserve(item.Sku, item.Quantity);
}
}
DIP SMELLS
Use of static methods/properties
message.Subject = "Your order placed on " +
DateTime.Now.ToString();
Or
DataAccess.SaveCustomer(myCustomer);
WHERE DOWE INSTANTIATE
OBJECTS?
 Applying Dependency Injection typically results in many interfaces that eventually need to be instantiated
somewhere… but where?
 Default Constructor
o You can provide a default constructor that news up the instances you
expect to typically need in your application
o Referred to as “poor man’s dependency injection” or “poor man’s IoC”
 Main
o You can manually instantiate whatever is needed in your application’s
startup routine or main() method
 IoC Container
o Use an “Inversion of Control” Container
IOC CONTAINERS
 Responsible for object graph instantiation
 Initiated at application startup via code or configuration
 Managed interfaces and the implementation to be used are
Registered with the container
 Dependencies on interfaces are Resolved at application startup
or runtime
 Examples of IoC Containers for .NET
o Microsoft Unity
o StructureMap
o Ninject
o Windsor
o Funq / Munq
SUMMARY
 Depend on abstractions.
 Don’t force high-level modules to depend on low-level modules through direct
instantiation or static method calls
 Declare class dependencies explicitly in their constructors
 Inject dependencies via constructor, property, or parameter injection
 Related Fundamentals:
o Single Responsibility Principle
o Interface Segregation Principle
o Façade Pattern
o Inversion of Control Containers
 Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin
[http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
TRADITIONAL (NAÏVE) LAYERED
ARCHITECTURE
INVERTED ARCHITECTURE
THE PROBLEM
 Dependencies FlowToward Infrastructure
 Core / Business / Domain Classes Depend on Implementation
Details
 Result
o Tight coupling
o No way to change implementation details without recompile (OCP violation)
o Difficult to test
DEPENDENCY INJECTION
 Dependency is transitive
o If UI depends on BLL depends on DAL depends on Database Then *everything*
depends on the Database
 Depend on abstractions (DIP)
 Package interfaces (abstractions) with the client (ISP)
 Structure Solutions and Projects so Core / BLL is at center,
with fewest dependencies
SUMMARY
 Don’t Depend on Infrastructure Assemblies from Core
 Apply DIP to reverse dependencies
 Related Fundamentals:
o Open Closed Principle
o Interface Segregation Principle
o Strategy Pattern
 Recommended Reading:
o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp]
o http://www.martinfowler.com/articles/injection.html
DON’T REPEATYOURSELF
“Every piece of knowledge must have a single, unambiguous
representation in the system.”
The Pragmatic Programmer
“Repetition in logic calls for abstraction. Repetition in process calls
for automation.”
97Things Every Programmer Should
Know
Variations include:
 Once and Only Once
 Duplication Is Evil (DIE)
ANALYSIS
Magic Strings/Values
Duplicate logic in multiple locations
Repeated if-then logic
Conditionals instead of polymorphism
Repeated Execution Patterns
Lots of duplicate, probably copy-pasted, code
Only manual tests
Static methods everywhere
MAGIC STRINGS /VALUES
DUPLICATE LOGIC IN MULTIPLE
LOCATIONS
REPEATED IF-THEN LOGIC
CONDITIONAL INSTEAD OF
POLYMORPHISM
 Example of Flags Over Objects anti-pattern
 Violates theTell, Don’t Ask principle (aka DIP)
SUMMARY
 Repetition breeds errors and waste
 Refactor code to remove repetition
 Recommended Reading:
o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK
o 97 Things Every Programmer Should Know http://amzn.to/cAse1Y
STATIC METHODS
 Tightly coupled
 Difficult to test
 Difficult to change behavior (violates OCP)
 Cannot use object oriented design techniques
o Inheritance
o Polymorphism
SUMMARY
 Repetition breeds errors and waste
 Abstract repetitive logic in code
 Related Fundamentals:
o Template Method Pattern
o Command Pattern
o Dependency Inversion Principle
 Recommended Reading:
o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK
o 97Things Every Programmer Should Know http://amzn.to/cAse1Y
REPEATED EXECUTION PATTERNS
DUPLICATE, COPY PASTED CODE
Too Numerous to List
CommercialTool (free version available)
o Atomiq (http://GetAtomiq.com)
CONSIDER CODE GENERATION
 T4Templates
o Custom code generation templates
o Built intoVisual Studio 2010
 ORMTools (LINQ to SQL, Entity Framework, nHibernate, LLBLGen)
o Reduce repetitive data access code
o Eliminate common data access errors
 CommercialTools:
o CodeSmith
o CodeBreeze
o CodeHayStack
REPETITION IN PROCESS
 Testing
o Performing testing by hand is tedious and wasteful
 Builds
o Performing builds by hand is tedious and wasteful
 Deployments
o Performing deployments by hand is tedious and wasteful
 Are you seeing a trend here?
String.Format(“Performing {0} by hand is tedious and wasteful”,action);
SUMMARY
 Repetition breeds errors and waste
 Use an ORM tool to eliminate repeated data access code
 UseTools to Locate Repeated Code
 Automate repetitive actions in your processes
 Recommended Reading:
o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK
o 97 Things Every Programmer Should Know http://amzn.to/cAse1Y
 RelatedTool
o Atomiq http://GetAtomiq.com
EXAMPLE APP: BEFORE AND
AFTER
S.O.L.I.D. CONVERSION SUMMARY
Before After
LOW COUPLING: OCP, DIP, ISP
S.O.L.I.D. -> OO PRINCIPLES
HIGH COHESION: LOW
COUPLING + SRP, LSPS.O.L.I.D. -> OO PRINCIPLES
ENCAPSULATION:
SRP, LSP, DIPS.O.L.I.D. -> OO PRINCIPLES
QUESTIONS?
THANKYOU!
Adnan Masood
adnanmasood@acm.org
@adnanmasood
 Blog: blog.AdnanMasood.com
 Pasadena .NET User Group: www.sgvdotnet.org

More Related Content

What's hot

Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
agnes_crepet
 
The Solid Principles
The Solid PrinciplesThe Solid Principles
The Solid Principles
Luke Smith
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
Xiaoyan Chen
 
An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.
ONE BCG
 
Introduction to Unit Testing
Introduction to Unit TestingIntroduction to Unit Testing
Introduction to Unit Testing
Swanky Hsiao
 
IoC and Mapper in C#
IoC and Mapper in C#IoC and Mapper in C#
IoC and Mapper in C#
Huy Hoàng Phạm
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
Andreas Enbohm
 
Solid principles
Solid principlesSolid principles
Solid principles
Toan Nguyen
 
Refactoring for Software Design Smells
Refactoring for Software Design SmellsRefactoring for Software Design Smells
Refactoring for Software Design Smells
Ganesh Samarthyam
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed PrincipleSam Hennessy
 
Microservices
MicroservicesMicroservices
Microservices
Đức Giang Nguyễn
 

What's hot (11)

Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
The Solid Principles
The Solid PrinciplesThe Solid Principles
The Solid Principles
 
Object-oriented design principles
Object-oriented design principlesObject-oriented design principles
Object-oriented design principles
 
An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.An ultimate guide to SOLID Principles, developers must know.
An ultimate guide to SOLID Principles, developers must know.
 
Introduction to Unit Testing
Introduction to Unit TestingIntroduction to Unit Testing
Introduction to Unit Testing
 
IoC and Mapper in C#
IoC and Mapper in C#IoC and Mapper in C#
IoC and Mapper in C#
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Refactoring for Software Design Smells
Refactoring for Software Design SmellsRefactoring for Software Design Smells
Refactoring for Software Design Smells
 
Implementing The Open/Closed Principle
Implementing The Open/Closed PrincipleImplementing The Open/Closed Principle
Implementing The Open/Closed Principle
 
Microservices
MicroservicesMicroservices
Microservices
 

Similar to Solid principle

SOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupSOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User Group
Adnan Masood
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
Mahmoud Asadi
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
Shuhab Tariq
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
deonpmeyer
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
Basavaraj Patil
 
Ood and solid principles
Ood and solid principlesOod and solid principles
Ood and solid principles
Avinash Kadam
 
OO Design Principles
OO Design PrinciplesOO Design Principles
OO Design Principles
Anju Kanjirathingal
 
Solid principles
Solid principlesSolid principles
Solid principles
Kumaresh Chandra Baruri
 
Common design principles and design patterns in automation testing
Common design principles and design patterns in automation testingCommon design principles and design patterns in automation testing
Common design principles and design patterns in automation testing
KMS Technology
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-PresentQuang Nguyen
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
Knoldus Inc.
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
Daniel Fisher
 
Android architecture
Android architectureAndroid architecture
Android architecture
Vandana Srivastava
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
jinaldesailive
 
The Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionThe Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary Version
Philip Schwarz
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
Prabhakar Sharma
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
Steven Smith
 

Similar to Solid principle (20)

SOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User GroupSOLID Principles of Refactoring Presentation - Inland Empire User Group
SOLID Principles of Refactoring Presentation - Inland Empire User Group
 
Oop principles
Oop principlesOop principles
Oop principles
 
Solid design principles
Solid design principlesSolid design principles
Solid design principles
 
Inversion of Control
Inversion of ControlInversion of Control
Inversion of Control
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 
DesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatternsDesignPrinciples-and-DesignPatterns
DesignPrinciples-and-DesignPatterns
 
Solid
SolidSolid
Solid
 
Ood and solid principles
Ood and solid principlesOod and solid principles
Ood and solid principles
 
OO Design Principles
OO Design PrinciplesOO Design Principles
OO Design Principles
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Common design principles and design patterns in automation testing
Common design principles and design patterns in automation testingCommon design principles and design patterns in automation testing
Common design principles and design patterns in automation testing
 
SOLID principles-Present
SOLID principles-PresentSOLID principles-Present
SOLID principles-Present
 
Solid
SolidSolid
Solid
 
SOLID Design Principles for Test Automaion
SOLID Design Principles for Test AutomaionSOLID Design Principles for Test Automaion
SOLID Design Principles for Test Automaion
 
2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#2009 Dotnet Information Day: More effective c#
2009 Dotnet Information Day: More effective c#
 
Android architecture
Android architectureAndroid architecture
Android architecture
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
The Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary VersionThe Open-Closed Principle - the Original Version and the Contemporary Version
The Open-Closed Principle - the Original Version and the Contemporary Version
 
Design principle vs design patterns
Design principle vs design patternsDesign principle vs design patterns
Design principle vs design patterns
 
Improving the Quality of Existing Software
Improving the Quality of Existing SoftwareImproving the Quality of Existing Software
Improving the Quality of Existing Software
 

More from muhammadali0014

Formal Software Specification part 1
Formal Software Specification part 1Formal Software Specification part 1
Formal Software Specification part 1
muhammadali0014
 
Formal Software Specification part 4
Formal Software Specification part 4Formal Software Specification part 4
Formal Software Specification part 4
muhammadali0014
 
Formal Software Specification part 5
Formal Software Specification part 5Formal Software Specification part 5
Formal Software Specification part 5
muhammadali0014
 
Software Architecture 1
Software Architecture 1Software Architecture 1
Software Architecture 1
muhammadali0014
 
Software Architecture 2
Software Architecture 2Software Architecture 2
Software Architecture 2
muhammadali0014
 
Software Architecture 3
Software Architecture 3Software Architecture 3
Software Architecture 3
muhammadali0014
 
Role of agile in designing 2
Role of agile in designing 2Role of agile in designing 2
Role of agile in designing 2
muhammadali0014
 
Role of agile in designing 1
Role of agile in designing 1Role of agile in designing 1
Role of agile in designing 1
muhammadali0014
 
Process models
Process modelsProcess models
Process models
muhammadali0014
 
Role of agile in designing 3
Role of agile in designing 3Role of agile in designing 3
Role of agile in designing 3
muhammadali0014
 
unified modeling language diagrams
unified modeling language diagramsunified modeling language diagrams
unified modeling language diagrams
muhammadali0014
 
Programming paradigm
Programming paradigmProgramming paradigm
Programming paradigm
muhammadali0014
 
Modeling 4
Modeling 4Modeling 4
Modeling 4
muhammadali0014
 
Modeling 2
Modeling 2Modeling 2
Modeling 2
muhammadali0014
 
Modeling 3
Modeling 3Modeling 3
Modeling 3
muhammadali0014
 
Design principles 1
Design principles 1Design principles 1
Design principles 1
muhammadali0014
 
Modeling 1
Modeling 1Modeling 1
Modeling 1
muhammadali0014
 
Design principles 2
Design principles 2Design principles 2
Design principles 2
muhammadali0014
 
Topic sentence mind map
Topic sentence mind mapTopic sentence mind map
Topic sentence mind map
muhammadali0014
 
Sentence structure
Sentence structureSentence structure
Sentence structure
muhammadali0014
 

More from muhammadali0014 (20)

Formal Software Specification part 1
Formal Software Specification part 1Formal Software Specification part 1
Formal Software Specification part 1
 
Formal Software Specification part 4
Formal Software Specification part 4Formal Software Specification part 4
Formal Software Specification part 4
 
Formal Software Specification part 5
Formal Software Specification part 5Formal Software Specification part 5
Formal Software Specification part 5
 
Software Architecture 1
Software Architecture 1Software Architecture 1
Software Architecture 1
 
Software Architecture 2
Software Architecture 2Software Architecture 2
Software Architecture 2
 
Software Architecture 3
Software Architecture 3Software Architecture 3
Software Architecture 3
 
Role of agile in designing 2
Role of agile in designing 2Role of agile in designing 2
Role of agile in designing 2
 
Role of agile in designing 1
Role of agile in designing 1Role of agile in designing 1
Role of agile in designing 1
 
Process models
Process modelsProcess models
Process models
 
Role of agile in designing 3
Role of agile in designing 3Role of agile in designing 3
Role of agile in designing 3
 
unified modeling language diagrams
unified modeling language diagramsunified modeling language diagrams
unified modeling language diagrams
 
Programming paradigm
Programming paradigmProgramming paradigm
Programming paradigm
 
Modeling 4
Modeling 4Modeling 4
Modeling 4
 
Modeling 2
Modeling 2Modeling 2
Modeling 2
 
Modeling 3
Modeling 3Modeling 3
Modeling 3
 
Design principles 1
Design principles 1Design principles 1
Design principles 1
 
Modeling 1
Modeling 1Modeling 1
Modeling 1
 
Design principles 2
Design principles 2Design principles 2
Design principles 2
 
Topic sentence mind map
Topic sentence mind mapTopic sentence mind map
Topic sentence mind map
 
Sentence structure
Sentence structureSentence structure
Sentence structure
 

Recently uploaded

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 

Recently uploaded (20)

Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 

Solid principle

  • 1. REFACTORING CODE TO A SOLID FOUNDATION Adnan Masood http://blog.adnanmasood.com @adnanmasood adnanmasood@acm.org Presented at Inland Empire .NET UG – 12/11/2012
  • 2. EXAMPLE APP: BEFORE AND AFTER OR HOW TO GET FROM HERE TO THERE? Before After
  • 3. CREDITS & REFERENCES • Robert C Martin – Clean Coder https://sites.google.com/site/unclebobconsultingllc/ • Pablo’s SOLID Software Development | LosTechies.com Derick Bailey - McLane Advanced Technologies, LLC • PluralSight Training - SOLID Principles of Object Oriented Design http://pluralsight.com/training/Courses/TableOfContents/principles-oo-design • Marcin Zajączkowski - http://solidsoft.wordpress.com/ • Images Used Under License • http://www.lostechies.com/blogs/derickbailey/archive/2009/02/11/solid-development-principles-in-motivational-pictures.aspx • SRP Article • http://www.objectmentor.com/resources/articles/srp.pdf • Solid for your Language • http://stefanroock.files.wordpress.com/2011/09/solidforyourlanguage.pdf
  • 4. HELP :ABOUT Adnan Masood works as a system architect / technical lead for Green dot Corporation where he develops SOA based middle-tier architectures, distributed systems, and web-applications using Microsoft technologies. He is a Microsoft Certified Trainer holding several technical certifications, including MCPD (Enterprise Developer), MCSD .NET, and SCJP-II.Adnan is attributed and published in print media and on the Web; he also teaches Windows Communication Foundation (WCF) courses at the University of California at San Diego and regularly presents at local code camps and user groups. He is actively involved in the .NET community as cofounder and president of the of San GabrielValley .NET Developers group. Adnan holds a Master’s degree in Computer Science; he is currently a doctoral student working towards PhD in Machine Learning; specifically discovering interestingness measures in outliers using Bayesian Belief Networks. He also holds systems architecture certification from MIT and SOA Smarts certification from Carnegie Melon University.
  • 5.
  • 6.
  • 7.
  • 8. ADDITIONAL RESOURCES Uncle Bob’s Principle Of Object Oriented Development: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod Pablo’sTopic OfThe Month: SOLID http://www.lostechies.com/blogs/chad_myers/archive/2008/03/07/pablo- s-topic-of-the-month-march-solid-principles.aspx Agile Principles, Patterns,And Practices In C# by Robert (Uncle Bob) Martin and Micah Martin Pablo’s SOLID E-Book http://www.lostechies.com/content/pablo_ebook.aspx
  • 9. COHESION: “A measure of how strongly-related and focused the various responsibilities of a software module are” - Wikipedia OBJECT ORIENTED PRINCIPLES - QUIZ COUPLING: “The degree to which each program module relies on each one of the other modules” – Wikipedia ENCAPSULATION: “The hiding of design decisions in a computer program that are most likely to change” -Wikipedia
  • 10. SRP: SINGLE RESPONSIBILITY PRINCIPLE OCP: OPEN CLOSED PRINCIPLE LSP: LISKOV SUBSTITUTION PRINCIPLE ISP: INTERFACE SEGREGATION PRINCIPLE DIP: DEPENDENCY INVERSION PRINCIPLE S.O.L.I.D. PRINCIPLES
  • 12. SRP:THE SINGLE RESPONSIBILITY PRINCIPLE The Single Responsibility Principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. Wikipedia There should never be more than one reason for a class to change. Robert C.“Uncle Bob” Martin
  • 13.
  • 14. COHESION AND COUPLING  Cohesion: how strongly-related and focused are the various responsibilities of a module  Coupling: the degree to which each program module relies on each one of the other modules Strive for low coupling and high cohesion!
  • 15. RESPONSIBILITIES ARE AXES OF CHANGE  Requirements changes typically map to responsibilities  More responsibilities == More likelihood of change  Having multiple responsibilities within a class couples together these responsibilities  The more classes a change affects, the more likely the change will introduce errors.
  • 16. Demo The ProblemWith Too Many Responsibilities
  • 17. WHAT IS A RESPONSIBILITY? “a reason to change” A difference in usage scenarios from the client’s perspective Multiple small interfaces (follow ISP) can help to achieve SRP
  • 18. SRP: SINGLE RESPONSIBILITY EXAMPLE APP: READ A FLAT FILE AND SEND AN EMAIL
  • 19. EXAMPLE APP NEW REQUIREMENTS: SEND FROM NON-WINFORMS APP. READ XML OR FLAT FILE SRP: SINGLE RESPONSIBILITY
  • 20. EXAMPLE APP: A BETTER STRUCTURE
  • 21. SUMMARY  Following SRP leads to lower coupling and higher cohesion  Many small classes with distinct responsibilities result in a more flexible design  Related Fundamentals: o Open/Closed Principle o Interface Segregation Principle o Separation of Concerns  Recommended Reading: o Clean Code by Robert C. Martin [http://amzn.to/Clean-Code]
  • 22. THE OPEN / CLOSED PRINCIPLE
  • 23.
  • 24. OCP:THE OPEN/CLOSED PRINCIPLE The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification Wikipedia
  • 25. THE OPEN / CLOSED PRINCIPLE Open to Extension New behavior can be added in the future Closed to Modification Changes to source or binary code are not required Dr. Bertrand Meyer originated the OCP term in his 1988 book, Object Oriented Software Construction
  • 26. CHANGE BEHAVIORWITHOUT CHANGING CODE? Rely on abstractions No limit to variety of implementations of each abstraction In .NET, abstractions include:  Interfaces  Abstract Base Classes In procedural code, some level of OCP can be achieved via parameters
  • 27. THE PROBLEM  Adding new rules require changes to the calculator every time  Each change can introduce bugs and requires re-testing, etc.  We want to avoid introducing changes that cascade through many modules in our application  Writing new classes is less likely to introduce problems o Nothing depends on new classes (yet) o New classes have no legacy coupling to make them hard to design or test
  • 28. THREE APPROACHESTO ACHIEVE OCP  Parameters (Procedural Programming) o Allow client to control behavior specifics via a parameter o Combined with delegates/lambda, can be very powerful approach  Inheritance /Template Method Pattern o Child types override behavior of a base class (or interface)  Composition / Strategy Pattern o Client code depends on abstraction o Provides a “plug in” model o Implementations utilize Inheritance; Client utilizes Composition
  • 29. WHEN DO WE APPLY OCP?  Experience Tells You If you know from your own experience in the problem domain that a particular class of change is likely to recur, you can apply OCP up front in your design Otherwise – “Fool me once, shame on you; fool me twice, shame on me”  Don’t apply OCP at first  If the module changes once, accept it.  If it changes a second time, refactor to achieve OCP Remember TANSTAAFL  There Ain’t No Such Thing As A Free Lunch  OCP adds complexity to design  No design can be closed against all changes
  • 30. SUMMARY  Conformance to OCP yields flexibility, reusability, and maintainability  Know which changes to guard against, and resist premature abstraction  Related Fundamentals: o Single Responsibility Principle o Strategy Pattern o Template Method Pattern  Recommended Reading: o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp]
  • 32. LSP:THE LISKOV SUBSTITUTION PRINCIPLE The Liskov Substitution Principle states that Subtypes must be substitutable for their base types. Agile Principles, Patterns, and Practices in C# Named for Barbara Liskov, who first described the principle in 1988.
  • 33.
  • 34. SUBSTITUTABILITY Child classes must not: 1) Remove base class behavior 2) Violate base class invariants And in general must not require calling code to know they are different from their base type.
  • 35. INHERITANCE ANDTHE IS-A RELATIONSHIP Naïve OOP teaches use of IS-A to describe child classes’ relationship to base classes LSP suggests that IS-A should be replaced with IS-SUBSTITUTABLE-FOR
  • 36. LSP: LISKOV SUBSTITUTION PRINCIPLE EXAMPLE APP: VIOLATING LSP WITH DATABASE CONNECTION INFO
  • 37. LSP: LISKOV SUBSTITUTION PRINCIPLE EXAMPLE APP: CORRECTING FOR LSP – MOVE THE DATABASE READER
  • 38. INVARIANTS  Consist of reasonable assumptions of behavior by clients  Can be expressed as preconditions and postconditions for methods  Frequently, unit tests are used to specify expected behavior of a method or class  Design By Contract is a technique that makes defining these pre- and post- conditions explicit within code itself.  To follow LSP, derived classes must not violate any constraints defined (or assumed by clients) on the base classes
  • 39. THE PROBLEM  Non-substitutable code breaks polymorphism  Client code expects child classes to work in place of their base classes  “Fixing” substitutability problems by adding if-then or switch statements quickly becomes a maintenance nightmare (and violates OCP)
  • 40. LSPVIOLATION “SMELLS” foreach (var emp in Employees) { if( emp is Manager ) { _printer.PrintManager( emp as Manager ); } else { _printer.PrintEmployee( emp ); } }
  • 41. LSPVIOLATION “SMELLS” public abstract class Base { public abstract void Method1(); public abstract void Method2(); } public class Child : Base { public override void Method1() { throw new NotImplementedException(); } public override void Method2() { // do stuff } } Follow ISP! Use small interfaces so you don’t require classes to implement more than they need!
  • 42. WHEN DO WE FIX LSP? If you notice obvious smells like those shown If you find yourself being bitten by the OCP violations LSP invariably causes
  • 43. LSPTIPS  “Tell, Don’t Ask” o Don’t interrogate objects for their internals – move behavior to the object o Tell the object what you want it to do  Consider Refactoring to a new Base Class o Given two classes that share a lot of behavior but are not substitutable… o Create a third class that both can derive from o Ensure substitutability is retained between each class and the new base
  • 44. SUMMARY  Conformance to LSP allows for proper use of polymorphism and produces more maintainable code  Remember IS-SUBSTITUTABLE-FOR instead of IS-A  Related Fundamentals: o Polymorphism o Inheritance o Interface Segregation Principle o Open / Closed Principle  Recommended Reading: o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp]
  • 46. ISP:THE INTERFACE SEGREGATION PRINCIPLE The Interface Segregation Principle states that Clients should not be forced to depend on methods they do not use. Agile Principles, Patterns, and Practices in C# Corollary: Prefer small, cohesive interfaces to “fat” interfaces
  • 47.
  • 48. WHAT’S AN INTERFACE? Interface keyword/type public interface IDoSomething { … } Public interface of a class public class SomeClass { … } What does the client see and use?
  • 49. ISP: INTERFACE SEGREGATION PRINCIPLE “THIS PRINCIPLE DEALS WITH THE DISADVANTAGES OF ‘FAT’ INTERFACES. CLASSES THAT HAVE ‘FAT’ INTERFACES ARE CLASSES WHOSE INTERFACES ARE NOT COHESIVE. IN OTHER WORDS, THE INTERFACES OF THE CLASS CAN BE BROKEN UP INTO GROUPS OF MEMBER FUNCTIONS. EACH GROUP SERVES A DIFFERENT SET OF CLIENTS. THUS SOME CLIENTS USE ONE GROUP OF MEMBER FUNCTIONS, AND OTHER CLIENTS USE THE OTHER GROUPS.” - ROBERT MARTIN
  • 51. ISP: INTERFACE SEGREGATION PRINCIPLE EXAMPLE APP: CLARIFYING THE EMAIL SENDER AND MESSAGE INFO PARSING
  • 52. THE PROBLEM Client Class (Login Control) NeedsThis:
  • 53. THE PROBLEM  AboutPage simply needs ApplicationName and AuthorName  Forced to deal with huge ConfigurationSettings class  Forced to deal with actual configuration files Interface Segregation violations result in classes that depend on things they do not need, increasing coupling and reducing flexibility and maintainability
  • 54. ISP SMELLS  Unimplemented interface methods: public override string ResetPassword( string username, string answer) { throw new NotImplementedException(); } Remember these violate Liskov Substitution Principle!
  • 55. ISP SMELLS Client references a class but only uses small portion of it
  • 56. WHEN DO WE FIX ISP?  Once there is pain o If there is no pain, there’s no problem to address.  If you find yourself depending on a “fat” interface you own o Create a smaller interface with just what you need o Have the fat interface implement your new interface o Reference the new interface with your code  If you find “fat” interfaces are problematic but you do not own them o Create a smaller interface with just what you need o Implement this interface using an Adapter that implements the full interface
  • 57. ISPTIPS  Keep interfaces small, cohesive, and focused  Whenever possible, let the client define the interface Whenever possible, package the interface with the client o Alternately, package in a third assembly client and implementation both depend upon o Last resort: Package interfaces with their implementation
  • 58. SUMMARY  Don’t force client code to depend on things it doesn’t need  Keep interfaces lean and focused  Refactor large interfaces so they inherit smaller interfaces  Related Fundamentals: o Polymorphism o Inheritance o Liskov Substitution Principle o Façade Pattern  Recommended Reading: o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp]
  • 60. DIP: DEPENDENCY INVERSION PRINCIPLE “WHAT IS IT THAT MAKES A DESIGN RIGID, FRAGILE AND IMMOBILE? IT IS THE INTERDEPENDENCE OF THE MODULES WITHIN THAT DESIGN. A DESIGN IS RIGID IF IT CANNOT BE EASILY CHANGED. SUCH RIGIDITY IS DUE TO THE FACT THAT A SINGLE CHANGE TO HEAVILY INTERDEPENDENT SOFTWARE BEGINS A CASCADE OF CHANGES IN DEPENDENT MODULES.” - ROBERT MARTIN
  • 61. DIP:THE DEPENDENCY INVERSION PRINCIPLE High-level modules should not depend on low- level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions. o Agile Principles, Patterns, and Practices in C#
  • 62.
  • 67. EXAMPLE APP: CONSTRUCTOR DEPENDENCIES IN A PROCESSING SERVICE DIP: DEPENDENCY INVERSION PRINCIPLE
  • 68. WHAT ARE DEPENDENCIES?  Framework  Third Party Libraries  Database  File System  Email  Web Services  System Resources (Clock)  Configuration  The new Keyword  Static methods  Thread.Sleep  Random
  • 69. TRADITIONAL PROGRAMMING AND DEPENDENCIES  High Level Modules Call Low Level Modules  User Interface depends on o Business Logic depends on o Infrastructure o Utility o Data Access  Static methods are used for convenience or as Façade layers  Class instantiation / Call stack logic is scattered through all modules o Violation of Single Responsibility Principle
  • 70. CLASS DEPENDENCIES: BE HONEST!  Class constructors should require any dependencies the class needs  Classes whose constructors make this clear have explicit dependencies  Classes that do not have implicit, hidden dependencies public class HelloWorldHidden { public string Hello(string name) { if (DateTime.Now.Hour < 12) return "Good morning, " + name; if (DateTime.Now.Hour < 18) return "Good afternoon, " + name; return "Good evening, " + name; } }
  • 71. CLASSES SHOULD DECLARE WHAT THEY NEED public class HelloWorldExplicit { private readonly DateTime _timeOfGreeting; public HelloWorldExplicit(DateTime timeOfGreeting) { _timeOfGreeting = timeOfGreeting; } public string Hello(string name) { if (_timeOfGreeting.Hour < 12) return "Good morning, " + name; if (_timeOfGreeting.Hour < 18) return "Good afternoon, " + name; return "Good evening, " + name; } }
  • 72. THE PROBLEM  Order has hidden dependencies: o MailMessage o SmtpClient o InventorySystem o PaymentGateway o Logger o DateTime.Now  Result o Tight coupling o No way to change implementation details (OCP violation) o Difficult to test
  • 73. DEPENDENCY INJECTION  Dependency Injection is a technique that is used to allow calling code to inject the dependencies a class needs when it is instantiated.  The Hollywood Principle o “Don’t call us; we’ll call you”  Three PrimaryTechniques o Constructor Injection o Property Injection o Parameter Injection  Other methods exist as well
  • 74. CONSTRUCTOR INJECTION  Dependencies are passed in via constructor Strategy Pattern  Pros o Classes self-document what they need to perform their work o Works well with or without a container o Classes are always in a valid state once constructed  Cons o Constructors can have many parameters/dependencies (design smell) o Some features (e.g. Serialization) may require a default constructor o Some methods in the class may not require things other methods require (design smell)
  • 75. PROPERTY INJECTION  Dependencies are passed in via a property o Also known as “setter injection”  Pros o Dependency can be changed at any time during object lifetime o Very flexible  Cons o Objects may be in an invalid state between construction and setting of dependencies via setters o Less intuitive
  • 76. PARAMETER INJECTION  Dependencies are passed in via a method parameter  Pros o Most granular o Very flexible o Requires no change to rest of class  Cons o Breaks method signature o Can result in many parameters (design smell)  Consider if only one method has the dependency, otherwise prefer constructor injection
  • 77. REFACTORING Extract Dependencies into Interfaces Inject implementations of interfaces into Order Reduce Order’s responsibilities (apply SRP)
  • 78. DIP SMELLS  Use of new keyword foreach(var item in cart.Items) { try { var inventorySystem = new InventorySystem(); inventorySystem.Reserve(item.Sku, item.Quantity); } }
  • 79. DIP SMELLS Use of static methods/properties message.Subject = "Your order placed on " + DateTime.Now.ToString(); Or DataAccess.SaveCustomer(myCustomer);
  • 80. WHERE DOWE INSTANTIATE OBJECTS?  Applying Dependency Injection typically results in many interfaces that eventually need to be instantiated somewhere… but where?  Default Constructor o You can provide a default constructor that news up the instances you expect to typically need in your application o Referred to as “poor man’s dependency injection” or “poor man’s IoC”  Main o You can manually instantiate whatever is needed in your application’s startup routine or main() method  IoC Container o Use an “Inversion of Control” Container
  • 81. IOC CONTAINERS  Responsible for object graph instantiation  Initiated at application startup via code or configuration  Managed interfaces and the implementation to be used are Registered with the container  Dependencies on interfaces are Resolved at application startup or runtime  Examples of IoC Containers for .NET o Microsoft Unity o StructureMap o Ninject o Windsor o Funq / Munq
  • 82. SUMMARY  Depend on abstractions.  Don’t force high-level modules to depend on low-level modules through direct instantiation or static method calls  Declare class dependencies explicitly in their constructors  Inject dependencies via constructor, property, or parameter injection  Related Fundamentals: o Single Responsibility Principle o Interface Segregation Principle o Façade Pattern o Inversion of Control Containers  Recommended Reading: o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp] o http://www.martinfowler.com/articles/injection.html
  • 85. THE PROBLEM  Dependencies FlowToward Infrastructure  Core / Business / Domain Classes Depend on Implementation Details  Result o Tight coupling o No way to change implementation details without recompile (OCP violation) o Difficult to test
  • 86. DEPENDENCY INJECTION  Dependency is transitive o If UI depends on BLL depends on DAL depends on Database Then *everything* depends on the Database  Depend on abstractions (DIP)  Package interfaces (abstractions) with the client (ISP)  Structure Solutions and Projects so Core / BLL is at center, with fewest dependencies
  • 87. SUMMARY  Don’t Depend on Infrastructure Assemblies from Core  Apply DIP to reverse dependencies  Related Fundamentals: o Open Closed Principle o Interface Segregation Principle o Strategy Pattern  Recommended Reading: o Agile Principles, Patterns, and Practices by Robert C. Martin and Micah Martin [http://amzn.to/agilepppcsharp] o http://www.martinfowler.com/articles/injection.html
  • 88.
  • 89. DON’T REPEATYOURSELF “Every piece of knowledge must have a single, unambiguous representation in the system.” The Pragmatic Programmer “Repetition in logic calls for abstraction. Repetition in process calls for automation.” 97Things Every Programmer Should Know Variations include:  Once and Only Once  Duplication Is Evil (DIE)
  • 90. ANALYSIS Magic Strings/Values Duplicate logic in multiple locations Repeated if-then logic Conditionals instead of polymorphism Repeated Execution Patterns Lots of duplicate, probably copy-pasted, code Only manual tests Static methods everywhere
  • 92. DUPLICATE LOGIC IN MULTIPLE LOCATIONS
  • 94. CONDITIONAL INSTEAD OF POLYMORPHISM  Example of Flags Over Objects anti-pattern  Violates theTell, Don’t Ask principle (aka DIP)
  • 95. SUMMARY  Repetition breeds errors and waste  Refactor code to remove repetition  Recommended Reading: o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK o 97 Things Every Programmer Should Know http://amzn.to/cAse1Y
  • 96. STATIC METHODS  Tightly coupled  Difficult to test  Difficult to change behavior (violates OCP)  Cannot use object oriented design techniques o Inheritance o Polymorphism
  • 97. SUMMARY  Repetition breeds errors and waste  Abstract repetitive logic in code  Related Fundamentals: o Template Method Pattern o Command Pattern o Dependency Inversion Principle  Recommended Reading: o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK o 97Things Every Programmer Should Know http://amzn.to/cAse1Y
  • 99. DUPLICATE, COPY PASTED CODE Too Numerous to List CommercialTool (free version available) o Atomiq (http://GetAtomiq.com)
  • 100. CONSIDER CODE GENERATION  T4Templates o Custom code generation templates o Built intoVisual Studio 2010  ORMTools (LINQ to SQL, Entity Framework, nHibernate, LLBLGen) o Reduce repetitive data access code o Eliminate common data access errors  CommercialTools: o CodeSmith o CodeBreeze o CodeHayStack
  • 101. REPETITION IN PROCESS  Testing o Performing testing by hand is tedious and wasteful  Builds o Performing builds by hand is tedious and wasteful  Deployments o Performing deployments by hand is tedious and wasteful  Are you seeing a trend here? String.Format(“Performing {0} by hand is tedious and wasteful”,action);
  • 102. SUMMARY  Repetition breeds errors and waste  Use an ORM tool to eliminate repeated data access code  UseTools to Locate Repeated Code  Automate repetitive actions in your processes  Recommended Reading: o The Pragmatic Programmer: From Journeyman to Master http://amzn.to/b2gJdK o 97 Things Every Programmer Should Know http://amzn.to/cAse1Y  RelatedTool o Atomiq http://GetAtomiq.com
  • 103. EXAMPLE APP: BEFORE AND AFTER S.O.L.I.D. CONVERSION SUMMARY Before After
  • 104. LOW COUPLING: OCP, DIP, ISP S.O.L.I.D. -> OO PRINCIPLES
  • 105. HIGH COHESION: LOW COUPLING + SRP, LSPS.O.L.I.D. -> OO PRINCIPLES
  • 108. THANKYOU! Adnan Masood adnanmasood@acm.org @adnanmasood  Blog: blog.AdnanMasood.com  Pasadena .NET User Group: www.sgvdotnet.org