SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante 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
 

Semelhante 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
 

Mais 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

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Último (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

How to build SOLID code