15 letters that will change your code

Slides:



Advertisements
Similar presentations
Object Oriented Design Principles Arnon Rotem-Gal-Oz Product Line Architect.
Advertisements

Design Principles & Patterns
Since 1995, we’ve been building innovative custom solutions specifically designed to meet the unique needs of America’s most recognized companies. If you.
General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
1 Software Maintenance and Evolution CSSE 575: Session 6, Part 1 The “SEAM” Model Steve Chenoweth Office Phone: (812) Cell: (937)
Liskov Substitution Principle
Common mistakes Basic Design Principles Amit Shabtay.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Software Engineering. Administrivia This is me: Cyndi Rader You can reach me: Or find me here: BB 280D Class notes here:
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SOLID Principles in Software Design
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Principles iwongu at gmail dot com.
The benefits of SOLID in software development Ruben Agudo Santos (GS-AIS-HR)
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
Software Design Principles
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
SOLID Principles in Software Design
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
SOLID PHP & Code Smell Wrap-Up
Liskov Substitution Principle Jon McBee CLA, CLED, CTD, CPI, LabVIEW Champion.
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Mantas Radzevičius ifm-2/2
What is Agile Design? SRP OCP LSP DIP ISP
OCP and Liskov Principles
Course information Old exam Resit Report Result and walkthrough
Software Design Principles
Software Architecture & Difference from Design
Copyright © by Curt Hill
Software Engineering: A Practitioner’s Approach, 6/e Chapter 11 Component-Level Design copyright © 1996, 2001, 2005 R.S. Pressman & Associates, Inc.
Adaptive Code Via C#
Factory pattern Unit of Work
Agile Software Development
Adaptive Code Umamaheswaran Senior Software Engineer
Object Oriented Practices
Component-Level Design
Software Design Principles
null, true, and false are also reserved.
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Improving the structure of existing code
CS 350 – Software Design Principles and Strategies – Chapter 14
The SOLID Principles.
A (partial) blueprint for dealing with change
European conference.
Software Development An overview of careers in Business Application Development Sources: Dice.com, money.CNN.com, InfoWorld.com, money.USNews.com,
Object Oriented Design & Analysis
Dependency Inversion principle
Some principles for object oriented design
Liskov Substitution Principle (LSP)
Chapter 10 – Component-Level Design
Presentation transcript:

15 letters that will change your code SOLID 15 letters that will change your code

software developer About me: Jarosław Stadnicki jstadnicki@pgs-soft.com jaroslaw.stadnicki@gmail.com @j_stadnicki jstadnicki.blogspot.com

SOLID - is a mnemonic acronym introduced by Michael Feathers for the "first five principles" named by Robert C. Martin in the early 2000s that stands for five basic principles of object-oriented programming and design. The principles, when applied together, intend to make it more likely that a programmer will create a system that is easy to maintain and extend over time. The principles of SOLID are guidelines that can be applied while working on software to remove code smells by causing the programmer to refactor the software's source code until it is both legible and extensible. It is part of an overall strategy of agile and adaptive programming - wikipedia

SOLID - is a mnemonic acronym introduced by Michael Feathers for the "first five principles" named by Robert C. Martin in the early 2000s that stands for five basic principles of object-oriented programming and design. The principles, when applied together, intend to make it more likely that a programmer will create a system that is easy to maintain and extend over time. The principles of SOLID are guidelines that can be applied while working on software to remove code smells by causing the programmer to refactor the software's source code until it is both legible and extensible. It is part of an overall strategy of agile and adaptive programming.

S O L I D S R P O C L I D

S 2019-01-14

Single responsibility principle a class should have only a single responsibility (i.e. only one potential change in the software's specification should be able to affect the specification of the class)

public void SaveNewProjectToDatabase(NewProjectModel newProjectModel) { var authorization = new AuthorizationService(); if (authorization.CurrentUserIsAuthenticated == false) { var logger = new Logger(); logger.Log("Unauthorized access"); throw new NotAuthorizedAccess(); } var database = new Database(); try { database.Add(newProjectModel); database.SaveChanges(); } catch (Exception e) { var logger = new Logger(); logger.Log("Fail to add or save changes on adding new project model") } finally { logger.Log("success, new project model added"); /* ooops, actually that’s not true */ } }

Single responsibility principle public void SaveNewProjectToDatabase(NewProjectModel newProject) { database.Add(newProject); database.SaveChanges(); }

srp => focus Welcome to PGS 2019-01-14

O Welcome to PGS 2019-01-14

Open close principle “software entities … should be open for extension, but closed for modification.”

Open close principle public decimal GetTotalArea() { var totalSize = 0m; foreach (var shape in shapes) { if (shape is Rectangle) { /* calculate area of rectangle */ } if (shape is Circle) { /* calculate area of circle */ } } return totalSize;

Open close principle public class SadDemoBasket { public decimal CalculateRegularPrice() { /* calculate regular price of products */ } public decimal CalculateDiscountPrice() { /* calculate price of products with discount */ } public decimal CalculateWholesalePrice() { /* calculate price of products using wholesale prices */ } }

Open close principle public decimal CalculatePrice(PriceCalculatorType calculatorToUse) { switch (calculatorToUse) { case PriceCalculatorType.Regular: return priceCalculator.GetPriceForProducts(products); case PriceCalculatorType.Discount: return discountPriceCalculator.GetPriceForProducts(products); case PriceCalculatorType.Wholesale: return wholesalePriceCalculator.GetPriceForProducts(products); } }

Open close principle public class HappyAreaCounter { private List<IShape> shapes; public decimal GetTotalArea() { var totalSize = 0m; shapes.ForEach(shape => totalSize += shape.Area()); return totalSize; } }

Open close principle public class HappyDemoBasket { private List<Product> products; public decimal CalculatePrice(IPriceCalculator calculator) { return calculator.GetPriceForProducts(this.products); } }

ocp => plugins Welcome to PGS 2019-01-14

L Welcome to PGS 2019-01-14

Liskov substitution principle “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program”

Liskov substitution principle interface IFile { void Write(byte[] data); byte[] Read(); }

Liskov substitution principle class File : IFile { public void Write(byte[] data){ /* writes stuff */ } public byte[] Read() { /* reads stuff */ } } class ReadOnlyFile : IFile public void Write(byte[] data) { throw new NotImplementedException();} public byte[] Read(){ /* reads stuff*/ }

Liskov substitution principle public void SadTestMethod() { var files = new List<IFile>(); files.Add(new File()); files.Add(new ReadOnlyFile()); files.ForEach(file=>file.Read()); files.ForEach(file=>file.Write(new byte[1])); } Welcome to PGS 2019-01-14

Liskov substitution principle public interface IDuck { void Swim() }

Liskov substitution principle public class OrganicDuck : IDuck { public void Swim(){ /* do the swim */ } } public class ElectricDuck : IDuck private bool isTurnedOn; public void Swim() if (!isTurnedOn) throw new Exception("Toy must be turn on in order to swim"); /* do the swim */ public void TurnOn() {isTurnedOn = true; } public void TurnOff(){isTurnedOn = false;}

Liskov substitution principle public interface IWritable { void Write(byte[] bytes); } public interface IReadable byte[] Read();

Liskov substitution principle public void HappyTestMethod() { var canRead = new List<IReadable>(); var canWrite = new List<IWritable>(); canRead.Add(new File()); canRead.Add(new ReadOnlyFile()); canWrite.Add(new File()); canRead.ForEach(file => file.Read()); canWrite.ForEach(file => file.Write(new byte[1])); }

lsp => contracts Welcome to PGS 2019-01-14

I Welcome to PGS 2019-01-14

Interface segregation principle “many client-specific interfaces are better than one general-purpose interface.”

Interface segregation principle public interface IPerson { string Name { get; set; } string HomeAddress { get; set; } string Nickname { get; set; } List<string> SuperSkills { get; set; } bool IsSuperHero { get; } }

Interface segregation principle public void SadTestMethod() { var persons = new List<IPerson>(); persons.Add(CreateBruceWayne()); persons.Add(CreateJohmSmith()); persons.ForEach(PrintPersonDetails); } Welcome to PGS 2019-01-14

Interface segregation principle return new Person { Name = "Bruce Wayne", HomeAddress = "Gotham City", IsSuperHero = true, Nickname = "Batman", SuperSkills = new List<string> { "Kick ass", "Do the right thing", “Being deadly serious" } }; return new Person { Name = "John Smith", HomeAddress = "Gotham City", IsSuperHero = false, Nickname = string.Empty, SuperSkills = null };

Interface segregation principle public void PrintPersonDetails(IPerson person) { Console.WriteLine(person.Name); Console.WriteLine(person.HomeAddress); Console.WriteLine(person.IsSuperHero); Console.WriteLine(person.Nickname); Console.WriteLine(person.SuperSkills); }

Interface segregation principle public interface IPersonPublicInformation { string Name { get; set; } } public interface IPersonPrivateInformation string HomeAddress { get; set; } public interface IPersonAlterEgo string Nickname { get; set; } List<string> SuperSkills { get; set; } bool IsSuperHero { get; }

Interface segregation principle public interface Person: IPersonPublicInformation, IPersonPrivateInformation { string Name { get; set; } string HomeAddress { get; set; } } public interface PersonWithPowers: IPersonPublicInformation, IPersonPrivateInformation, IPersonAlterEgo { string Name { get; set; } string HomeAddress { get; set; } string Nickname { get; set; } List<string> SuperSkills { get; set; } bool IsSuperHero { get; } } Welcome to PGS 2019-01-14

Interface segregation principle public void PrintPersonPublicDetails(IPersonPublicInformation person) { Console.WriteLine(person.Name); } private void PrintPrivateDetails(IPersonPrivateInformation person) Console.WriteLine(person.HomeAddress); public void PrintPersonAlterEgoDetails(IPersonAlterEgo person) Console.WriteLine(person.IsSuperHero); Console.WriteLine(person.Nickname); Console.WriteLine(person.SuperSkills);

isp => secrets Welcome to PGS 2019-01-14

D Welcome to PGS 2019-01-14

Dependency inversion principle one should “Depend upon Abstractions. Do not depend upon concretions.”

Dependency inversion principle public void SadDemoMethod() { var service = new SadDemoService(); var database = new SadDemoDatabase(); var data = database.LoadData(); service.Handle(data); var loger = new SadFileLoger(); loger.Write("Im sad!"); }

Dependency inversion principle public void HappyDemoMethod(IDatabase database, IService service, ILoger loger) { var data = database.LoadData(); service.Handle(data); loger.Write("Im happy!"); /* SRP warning here */ }

Dependency inversion principle public class HappyDemoClass { private readonly IDatabase database; private readonly IService service; private readonly ILoger loger; public HappyDemoClass(IDatabase database, IService service, ILoger loger) { this.database = database; this.service = service; this.loger = loger; } public void HappyDemoMethod() /* code goes here */

dip => demand Welcome to PGS 2019-01-14

S O L I D focus plugins contract secrets demand

What else KISS OOP TDD GIT DRY POCO BDD MAGIC NUMBERS YAGNI BLAME DDD XP WTF FLAME WAR AOP KANBAN RTFM REVIEW JIRA more!

That’s all folks Thank you Questions? Feedback