SlideShare a Scribd company logo
How to build SOLID code
Sebastiano (eTr) Merlino
ZenConf
04/Jun/2014 - Catania
Almost entirely copied from inspired by the presentation “From
STUPID to SOLID” by William Durand.
#zenetr
DISCLAIMER:
I give you an opinion not rules.
Please, consider these just as principles, not laws!
#zenetr
Only 2 types of code do exist:
Your code: Other people code:
It’s REASSURING and you
understand it
It INTIMIDATES you and it’s ~99.
999% of world’s code
#zenetr
Why should my code be
clear?
Because we READ much more than we WRITE
#zenetr
STUPID code, seriously?
#zenetr
What makes code STUPID?
● Singleton
● Tight Coupling
● Untestability
● Premature Optimization
● Indescriptive Naming (yeah! It’s not misspelled)
● Duplication
#zenetr
Singleton
#zenetr
Singleton
● It represents a GLOBAL STATE in your code
● Programs using global state are VERY difficult to test and debug
● Programs relying on global state HIDE their dependencies
Links:
- Why singletons are controversial?
- Why is singleton an antipattern?
- So Singletons are bad, then what?
#zenetr
Tight Coupling
#zenetr
Tight Coupling
● Generalization of the singleton ISSUE
● If changing something in a module FORCES you to change also
another module
● It makes code difficult to REUSE and also difficult to TEST
● To avoid it, favor COMPOSITION over inheritance and try to use
DEPENDENCY INJECTION where possible.
Links:
- Reducing Coupling (Martin Fowler)
#zenetr
Untestability
#zenetr
Untestability
● Testing should not be HARD
● Whenever you don’t write UNIT TESTS because you DON’T HAVE
TIME, the real issue is that your code is BAD
● Most of the time untestability is caused by TIGHT COUPLING
#zenetr
Premature Optimization
#zenetr
Premature Optimization
PREMATURE OPTIMIZATION is the root of all evil - DONALD KNUTH
● Do not OVER-COMPLICATE your system in order to optimize it
● There is only COST and no BENEFIT
● MEASURE the performances before you start to optimize
DON’T DO IT.
For experts only: DON’T DO IT NOW!
Links:
- Premature Optimization Anti-Pattern
#zenetr
Indescriptive Naming
#zenetr
Indescriptive Naming
● Name your classes, methods, attributes and variables properly
● Don’t abbreviate, NEVER!
● If you are not able to give your class a short name, maybe its
responsibilities are not WELL DEFINED
● Programming languages are for HUMANS
#zenetr
Duplication
#zenetr
Duplication
● If you do it, you will find yourself changing your code in multiple
places.
● Don’t Repeat Yourself (DRY)
● Keep It Simple, Stupid! (KISS)
● Be DRY not WET (We Enjoy Typing)
Links:
- No, seriously. Don’t repeat yourself
- DRY principle
- KISS principle
#zenetr
#zenetr
SOLID
Term describing a collection of design principles for GOOD CODE
that was coined by ROBERT C. MARTIN aka UNCLE BOB
#zenetr
What makes code SOLID?
● Single Responsibility Principle
● Open/Closed Principle
● Liskov Substitution Principle
● Interface Segregation Principle
● Dependency Inversion Principle
#zenetr
Single Responsibility Principle
#zenetr
Single Responsibility Principle
● There should NEVER be more than ONE reason for a class to
change
● Split big classes
● Use LAYERS
● Avoid GOD classes
● Write STRAIGHTFORWARD comments
Links:
- Single Responsibility Principle
#zenetr
Open/Closed Principle
#zenetr
Open/Closed Principle
● Software entities should be OPEN for extension but CLOSED for
manipulation
● Make ALL member variables private
● NO global variables, EVER
● Avoid SETTERS (as much as possible)
● Builder Pattern + Immutable Objects
Links:
- Open/Closed Principle
#zenetr
Liskov Substitution Principle
#zenetr
Liskov Substitution Principle
#zenetr
public class Rectangle {
protected int width;
protected int height;
public int getArea() { return width * height; }
public void setWidth(int width) { this.width = width; }
public void setHeight(int height) { this.height = height; }
}
public class Square extends Rectangle {
public void setWidth(int width) { this.width = width; this.height = width; }
public void setHeight(int height) { this.width = height; this.height = height; }
}
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Square();
rectangle.setWidth(10); rectangle.setHeight(20);
assert rectangle.getArea() == 200; // this will fail!
}
}
Liskov Substitution Principle
#zenetr
● Objects in a program should be replaceable with instances of
their subtypes WITHOUT ALTERING THE CORRECTNESS of the
program
Links:
● Liskov Substitution Principle
● Circle-ellipse problem
● Should sets inherit from bags?
Interface Segregation Principle
#zenetr
Interface Segregation Principle
#zenetr
● MANY client-specific interfaces are BETTER THAN ONE general-
purpose interface.
● You should not have to implement methods you don’t use
● Enforcing ISP gives you LOW COUPLING and HIGH COHESION
● KEEP COMPONENTS FOCUSED and MINIMIZE DEPENDENCIES BETWEEN
THEM
Links:
● Interface Segregation Principle
● Coupling and Cohesion: Principles of Orthogonal OOP
Dependency Inversion Principle
#zenetr
Dependency Inversion Principle
#zenetr
● High level modules SHOULD NOT DEPEND upon low level modules.
Both SHOULD DEPEND upon abstractions
● Abstractions should not depend upon details. Details should
depend upon abstractions
● Use the same level of abstraction at a given level
● It REDUCES DEPENDENCY on implementation specifics and makes
code MORE REUSABLE
Links:
● Dependency Inversion Principle
● Programming to the interface
Rule of thumb:
USE YOUR BRAIN!
#zenetr
Thank you!
https://github.com/etr
https://twitter.com/electrictwister
http://www.slideshare.net/electrictwister
#zenetr
Credits:
http://williamdurand.fr/2013/07/30/from-stupid-to-solid-
code/
http://lostechies.com/derickbailey/2009/02/11/solid-
development-principles-in-motivational-pictures/
#zenetr
License:
Creative Commons Attribution-ShareAlike 3.0 Unported License
#zenetr

More Related Content

PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
PDF
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
PDF
Clean code and code smells
PDF
Pavlo Zhdanov "Mastering solid and base principles for software design"
PDF
TDD and Simple Design Workshop - Session 1 - March 2019
PDF
Tdd is not about testing
PPT
The OO Design Principles
PDF
API Design
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE
AN EXERCISE IN CLEANER CODE - FROM LEGACY TO MAINTAINABLE - CFObjective() 2017
Clean code and code smells
Pavlo Zhdanov "Mastering solid and base principles for software design"
TDD and Simple Design Workshop - Session 1 - March 2019
Tdd is not about testing
The OO Design Principles
API Design

Similar to How to build SOLID code (20)

PPTX
Writing clean code in C# and .NET
PDF
Writing Readable Code
PDF
Object Design - Part 1
PPTX
Design Principles
ODP
Clean Code - Part 2
PPTX
Simple is the best
PDF
Create first android app with MVVM Architecture
PDF
L05 Design Patterns
PPTX
From good to solid: How to become a better developer
PDF
Things Every Professional Programmer Should Know
PPTX
How I Learned to Stop Worrying and Love Legacy Code.....
PDF
Keeping code clean
PDF
Clean Code 2
ODP
Geecon09: SOLID Design Principles
PDF
Behavior Driven Education: A Story of Learning ROR
PDF
TDD in Python With Pytest
PPTX
Clean Code - The Next Chapter
PDF
Clean Code V2
PDF
Oh the compilers you'll build
PPTX
Ruby for .NET developers
Writing clean code in C# and .NET
Writing Readable Code
Object Design - Part 1
Design Principles
Clean Code - Part 2
Simple is the best
Create first android app with MVVM Architecture
L05 Design Patterns
From good to solid: How to become a better developer
Things Every Professional Programmer Should Know
How I Learned to Stop Worrying and Love Legacy Code.....
Keeping code clean
Clean Code 2
Geecon09: SOLID Design Principles
Behavior Driven Education: A Story of Learning ROR
TDD in Python With Pytest
Clean Code - The Next Chapter
Clean Code V2
Oh the compilers you'll build
Ruby for .NET developers
Ad

More from Sebastiano Merlino (eTr) (20)

PDF
Multithreading, multiprocessing e Asincronia
PPTX
Biomeccatronica
PDF
Openid+Opensocial
PDF
Bash programming
PDF
Lezione Uno Pratica
PDF
Lezione Tre Pratica
PDF
PDF
Lezione Quattro
PDF
Lezione Due Pratica
PDF
Lezione Cinque
PDF
PDF
PDF
Wsmo Restricted
PDF
Sawsdl Restriced
PDF
Owl Guide Resticted
PDF
Owl S Restricted
PDF
Fast Wsdl Tutorial
PDF
PDF
Linux & Open Source - Alternative Software
Multithreading, multiprocessing e Asincronia
Biomeccatronica
Openid+Opensocial
Bash programming
Lezione Uno Pratica
Lezione Tre Pratica
Lezione Quattro
Lezione Due Pratica
Lezione Cinque
Wsmo Restricted
Sawsdl Restriced
Owl Guide Resticted
Owl S Restricted
Fast Wsdl Tutorial
Linux & Open Source - Alternative Software
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PPTX
Essential Infomation Tech presentation.pptx
PDF
top salesforce developer skills in 2025.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
System and Network Administration Chapter 2
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
System and Network Administraation Chapter 3
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Nekopoi APK 2025 free lastest update
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Introduction to Artificial Intelligence
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
medical staffing services at VALiNTRY
Essential Infomation Tech presentation.pptx
top salesforce developer skills in 2025.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Understanding Forklifts - TECH EHS Solution
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
VVF-Customer-Presentation2025-Ver1.9.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 41
CHAPTER 2 - PM Management and IT Context
System and Network Administration Chapter 2
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
System and Network Administraation Chapter 3
How to Choose the Right IT Partner for Your Business in Malaysia
Nekopoi APK 2025 free lastest update
PTS Company Brochure 2025 (1).pdf.......
How to Migrate SBCGlobal Email to Yahoo Easily
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers

How to build SOLID code