SlideShare una empresa de Scribd logo
1 de 38
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a How to build SOLID code

Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
 
Writing Readable Code
Writing Readable CodeWriting Readable Code
Writing Readable Codeeddiehaber
 
Object Design - Part 1
Object Design - Part 1Object Design - Part 1
Object Design - Part 1Dhaval Dalal
 
Clean Code - Part 2
Clean Code - Part 2Clean Code - Part 2
Clean Code - Part 2Knoldus Inc.
 
Create first android app with MVVM Architecture
Create first android app with MVVM ArchitectureCreate first android app with MVVM Architecture
Create first android app with MVVM Architecturekhushbu thakker
 
From good to solid: How to become a better developer
From good to solid: How to become a better developerFrom good to solid: How to become a better developer
From good to solid: How to become a better developerKaterina Trajchevska
 
Things Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowThings Every Professional Programmer Should Know
Things Every Professional Programmer Should KnowDaniel Sawano
 
How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....Mike Harris
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Geecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesGeecon09: SOLID Design Principles
Geecon09: SOLID Design PrinciplesBruno Bossola
 
Behavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning RORBehavior Driven Education: A Story of Learning ROR
Behavior Driven Education: A Story of Learning RORSmartLogic
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next ChapterVictor Rentea
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll buildMark Stoodley
 
Ruby for .NET developers
Ruby for .NET developersRuby for .NET developers
Ruby for .NET developersMax Titov
 

Similar a How to build SOLID code (20)

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

Más de Sebastiano Merlino (eTr) (20)

Multithreading, multiprocessing e Asincronia
Multithreading, multiprocessing e AsincroniaMultithreading, multiprocessing e Asincronia
Multithreading, multiprocessing e Asincronia
 
Asterisk
AsteriskAsterisk
Asterisk
 
Biomeccatronica
BiomeccatronicaBiomeccatronica
Biomeccatronica
 
Openid+Opensocial
Openid+OpensocialOpenid+Opensocial
Openid+Opensocial
 
Bash programming
Bash programmingBash programming
Bash programming
 
Lezione Uno Pratica
Lezione Uno PraticaLezione Uno Pratica
Lezione Uno Pratica
 
Lezione Tre Pratica
Lezione Tre PraticaLezione Tre Pratica
Lezione Tre Pratica
 
Lezione tre
Lezione treLezione tre
Lezione tre
 
Lezione Quattro
Lezione QuattroLezione Quattro
Lezione Quattro
 
Lezione Due Pratica
Lezione Due PraticaLezione Due Pratica
Lezione Due Pratica
 
Lezione Cinque
Lezione CinqueLezione Cinque
Lezione Cinque
 
Lezione uno
Lezione unoLezione uno
Lezione uno
 
Lezione due
Lezione dueLezione due
Lezione due
 
Wsmo Restricted
Wsmo RestrictedWsmo Restricted
Wsmo Restricted
 
Sawsdl Restriced
Sawsdl RestricedSawsdl Restriced
Sawsdl Restriced
 
Owl Guide Resticted
Owl Guide RestictedOwl Guide Resticted
Owl Guide Resticted
 
Owl S Restricted
Owl S RestrictedOwl S Restricted
Owl S Restricted
 
Fast Wsdl Tutorial
Fast Wsdl TutorialFast Wsdl Tutorial
Fast Wsdl Tutorial
 
Lezione Tre
Lezione TreLezione Tre
Lezione Tre
 
Linux & Open Source - Alternative Software
Linux & Open Source - Alternative SoftwareLinux & Open Source - Alternative Software
Linux & Open Source - Alternative Software
 

Último

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 

Último (20)

Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 

How to build SOLID code