SOLID and Other Principles

Slides:



Advertisements
Similar presentations
Creating Vector Graphics in the Web Learning & Development Telerik Software Academy.
Advertisements

Redundant Array of Independent Disks Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Writing HTML made easier Telerik School Academy HTML, CSS and JavaScript.
Inside Windows Boot Process Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Guidelines to Preparing and Delivering an Elevator Pitch Presentation Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training.
Two basic algorithms for path searching in a graph Telerik Algo Academy Graph Algorithms.
About the Course Telerik Software Academy CSS Styling.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Eclipse + Android SDK, VS + Windows Phone SDK Telerik Software Academy Hybrid Mobile Applications.
Welcome to the JSON-stores world Learning & Development Telerik Software Academy.
Course Content, Evaluation, Exams Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Eclipse + Android SDK, VS + Windows Phone SDK Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Twitter Bootstrap Telerik Software Academy
Things you need to know George Georgiev Technical Trainer GeorgeAtanasov George Atanasov Front-End Developer.
Hamiltonian Cycle Penka Borukova Student at Telerik Academy.
Group Policy Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Reverse polish notation, Operations with big numbers Ivelin Rachev Telerik Corporation “Baba Tonka” High School of Mathematics.
April, Sofia, Telerik Software Academy Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET MVC.
Sales Processes, Sales Cycle and the Sales Force Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik.
Design Patterns that ease the design by identifying a simple way to realize relationships between entities. Antony Jekov Telerik Software Academy academy.telerik.com.
Using KendoUI for SPA Applications Learning & Development Telerik School Academy.
The way to create cross-platform apps Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
PEST Analysis, Porter’s 5 Forces and Sustainability Strategies Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System.
Searching for a Job, Writing CV and Cover Letter, Passing a Job Interview Telerik Academy for Software Engineers October 6 th 2012 – Sofia academy.telerik.com.
Drive Partitioning Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Telerik Software Academy Mobile apps for iPhone & iPad.
Google APIs and Facebook API Ivaylo Kenov Penka Borukova Telerik Corporation Telerik Academy Students.
The magic of virtual machines Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Web development with ExpressJS Learning & Development Telerik School Academy.
Device APIs with Xamarin
Telerik Software Academy Mobile apps for iPhone & iPad.
Academy.telerik.com Class of , Fall 2013.
Telerik Software Academy Databases.
Telerik Software Academy ASP.NET Web Forms.
What are WinJS and WinRT, Using the APIs in JavaScript George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
JavaScript Modules and Patterns Telerik Software Academy
The True power of dynamic web pages Learning & Development Team Telerik Software Academy.
Approximate string matching Evlogi Hristov Telerik Corporation Student at Telerik Academy.
Xamarin with SQLite Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
Know your Tools! Telerik Software Academy JavaScript Fundamentals.
Describe ways to assemble objects to implement a new functionality Telerik Software Academy High-Quality Code.
Ivaylo Kenov Telerik Software Academy Technical Assistant.
Developing "evil" JavaScript applications Learning & Development Telerik School Academy.
The PhoneGap History Doncho Minkov Telerik Software Academy Technical Trainer
Building Rock-Solid Software Svetlin Nakov Telerik Software Academy Manager Technical Training
Cultivating Professionals for Your Company The Success Story of Telerik Svetlin Nakov, PhD Manager Technical Training Telerik Corp.
Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
The way of the ViewModel and the Binding Learning & Development Telerik School Academy.
AJAX with ASP.NET MVC Telerik Software Academy
Nencho Nenchev Doroteya Agayna Telerik Software Academy Telerik Support Leads.
Installation, Sample Usage, Strings and OOP Telerik Software Academy Software Quality Assurance.
In JavaScript Learning & Development Telerik Software Academy.
Services in AngularJS Telerik Software Academy
Svetlin Nakov Telerik Software Academy Manager Technical Training
What is Roslyn and how can we use it? Telerik Academy Plus C# 6.0 and Roslyn Seminar.
Splitting JavaScript into Dependent Modules Learning & Development Telerik Software Academy.
Changing the default visualization of views in Xamarin.Forms Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
Telerik Software Academy HTML5.
What to expect from the new IDE Telerik Academy Plus Visual Studio 2015 and ASP.NET 5.
Hristian Hristov Telerik Corporation
I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development.
Monitoring Server Performance Organizing Your Support Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Automating Windows Deployment Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Server Roles and Features Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Binary, Decimal and Hexadecimal Numbers Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Adding UI and Audio element Telerik Academy Plus Unity 2D Game Development.
Dimo Dimov Support Lead, Kendo UI Telerik Telerik Software Academy
Presentation transcript:

SOLID and Other Principles SOLID, DRY, YAGNI, KISS Telerik Software Academy Learning & Development http://academy.telerik.com

Table of Contents SOLID Principles DRY – Don't Repeat Yourself SRP – Single Responsibility Principle OCP – Open/Closed Principle LSP – Liskov Substitution Principle ISP – Interface Segregation Principle DIP – Dependency Inversion Principle DRY – Don't Repeat Yourself YAGNI – You Aren't Gonna Need It KISS – Keep It Simple, Stupid

Single Responsibility

SRP "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

SRP Cohesion Coupling Ideal - low coupling / strong cohesion Relation of responsibilities Focused on one task Coupling Dependency on other modules Relationship between modules Ideal - low coupling / strong cohesion

SRP Responsibilities "A reason to change" Mapped to project requirements More requirements – more possible changes More responsibilities – more changes in code Multiple responsibilities in one class – coupling More coupling – more errors on change

SRP Classic violations Classic solution Objects that can print/draw themselves Objects that can save/restore themselves Classic solution Separate printer Separate saver (or memento)

SRP Solution Result Multiple small interfaces (ISP) Many small classes Distinct responsibilities Result Flexible design Lower coupling Higher cohesion

Single Responsibility Live Demo

Open/Closed Principle

OCP Open to Extension Closed to Modification "The Open / Closed Principle states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification." Wikipedia Open to Extension New behavior can be added in the future Closed to Modification Changes to source or binary code are not required

OCP Change behavior without changing code?! In .NET In procedural code Rely on abstractions, not implementations Do not limit the variety of implementations In .NET Interfaces Abstract Classes In procedural code Use parameters

OCP Classic violations Classic solution Each change requires re-testing (possible bugs) Cascading changes through modules Logic depends on conditional statements Classic solution New classes (nothing depends on them yet) New classes (no legacy coupling)

OCP Three approaches to achieve OCP Parameters Pass delegates / callbacks Inheritance / Template Method pattern Child types override behavior of a base class Composition / Strategy pattern Client code depends on abstraction "Plug in" model

OCP When to apply OCP? OCP add complexity to design (TANSTAAFL) Experience tell you "Fool me once, shame on you" Don't apply OCP at first If module changes once, accept it If it changes a second time, refactor for OCP OCP add complexity to design (TANSTAAFL) No design can be closed against all changes

Open / Closed Live Demo

Liskov Substitution

LSP Substitutability – child classes must not "The Liskov Substitution Principle states that Subtypes must be substitutable for their base types." Agile Principles, Patterns, and Practices in C# Substitutability – child classes must not Remove base class behavior Violate base class invariants

LSP Normal OOP inheritance Liskov Substitution inheritance IS-A relationship Liskov Substitution inheritance IS-SUBSTITUTABLE-FOR

LSP The problem Classic violations Polymorphism break Client code expectations "Fixing" by adding if-then – nightmare (OCP) Classic violations Type checking for different methods Not implemented overridden methods Virtual methods in constructor

LSP Solutions "Tell, Don't Ask" Refactoring to base class Don’t ask for types Tell the object what to do Refactoring to base class Common functionality Introduce third class

Liskov Substitution Live Demo

Interface Segregation

ISP Prefer small, cohesive interfaces "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# Prefer small, cohesive interfaces Divide "fat" interfaces into smaller ones

ISP Interface is: Having "fat" interfaces leads to: The interface type All public members of a class Having "fat" interfaces leads to: Classes having methods they do not need Increasing coupling Reduced flexibility Reduced maintainability

ISP Classic violations When to fix? Unimplemented methods (also in LSP) Use of only small portion of a class When to fix? Once there is pain! Do not fix, if is not broken! If the "fat" interface is yours, separate it to smaller ones If the "fat" interface is not yours, use "Adapter" pattern

ISP Solutions Small interfaces Cohesive interfaces Focused interfaces Let the client define interfaces Package interfaces with their implementation

Interface Segregation Live Demo

Dependency Inversion

DIP "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." Agile Principles, Patterns, and Practices in C#

DIP Dependency is: Framework System Resources (Clock) Third Party Libraries Configuration Database The new Keyword File System Static methods Email Thread.Sleep Web Services Random

DIP Traditional Programming High level modules use lower lever modules UI depends on Business Layer Business layer depends on Infrastructure Database Utilities Static methods (Façade for example) Classes instantiated everywhere

DIP How it should be How to do it Classes should declare what they need Constructors should require dependencies Hidden dependencies should be shown Dependencies should be abstractions How to do it Dependency Injection The Hollywood principle "Don't call us, we'll call you!"

DIP Constructor injection Dependencies – through constructors Pros Classes self document requirements Works well without container Always valid state Cons Many parameters Some methods may not need everything

DIP Property injection Dependencies – through setters Pros Cons Can be changed anytime Very flexible Cons Possible invalid state Less intuitive

DIP Parameter injection Dependencies – through method parameter Pros No change in rest of the class Very flexible Cons Many parameters Breaks method signature

DIP Classic violations How to fix? Using of the new keyword Using static methods/properties How to fix? Default constructor Main method/starting point Inversion of Control container

DIP IoC containers Responsible for object instantiation Initiated at application start-up Interfaces are registered into the container Dependencies on interfaces are resolved Examples – StructureMap, Ninject and more

Dependency Inversion Live Demo

Other Principles

Don't Repeat Yourself

DRY Variations include: Once and Only Once Duplication Is Evil (DIE) "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." 97 Things Every Programmer Should Know Variations include: Once and Only Once Duplication Is Evil (DIE)

DRY Classic violations 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

Don't Repeat Yourself Live Demo

You Ain't Gonna Need It

YAGNI "A programmer should not add functionality until deemed necessary." Wikipedia "Always implement things when you actually need them, never when you just foresee that you need them." Ron Jeffries, XP co-founder

YAGNI Disadvantages Time for adding, testing, improving Debugging, documented, supported Difficult for requirements Larger and complicate software May lead to adding even more features May be not know to clients

You Ain't Gonna Need It Live Demo

Keep It Simple, Stupid

KISS "Most systems work best if they are kept simple." U.S. Navy "Simplicity should be a key goal in design and unnecessary complexity should be avoided." Wikipedia

Keep It Simple Stupid Live Demo

Even More Principles

Golden Hammer

Feature Creep

Death March

Duck Tape Coder

Iceberg Class

Spaghetti Code

Calendar Coder

Reinventing The Wheel

Copy Paste Programming

Boy Scout Rule

Design By Committee

Copy Folder Versioning

SOLID And Other Principles http://academy.telerik.com

Free Trainings @ Telerik Academy C# Programming @ Telerik Academy csharpfundamentals.telerik.com Telerik Software Academy academy.telerik.com Telerik Academy @ Facebook facebook.com/TelerikAcademy Telerik Software Academy Forums forums.academy.telerik.com