Adaptive Code Via C# 06.04.2017 Umamaheswaran @UMW1990.

Slides:



Advertisements
Similar presentations
Design by Contract.
Advertisements

1 Refactoring with Contracts Shmuel Tyszberowicz School of Computer Science The Academic College of Tel Aviv Yaffo Maayan Goldstein School of Computer.
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
The Substitution Principle SWE 332 – Fall Liskov Substitution Principle In any client code, if subtype object is substituted for supertype object,
1 Computer Science 340 Software Design & Testing Inheritance & Design By Contract.
CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
CSE-321 Programming Languages Subtyping POSTECH May 14, 2007 박성우.
5/17/2015 OO Design: Liskov Substitution Principle 1.
Error Management with Design Contracts Karlstad University Computer Science Error Management with Design Contracts Eivind J. Nordby, Martin Blom, Anna.
1 Design by Contract Building Reliable Software. 2 Software Correctness Correctness is a relative notion  A program is correct with respect to its specification.
Object Oriented Design An object combines data and operations on that data (object is an instance of class) data: class variables operations: methods Three.
On the Relation between Design Contracts and Errors Karlstad University Computer Science On the Relation Between Design Contracts and Errors A Software.
Liskov Substitution Principle
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Contract based programming Using pre- and post-conditions, and object invariants Contract based programming1.
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 12: Subtyping Rules What’s the.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
OO as a language for acm l OO phrase l Mental model of key concepts.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 17: Inheritance & Behavioral.
Cs205: engineering software university of virginia fall 2006 David Evans Substitution Principle.
Type Abstraction Liskov, Chapter 7. 2 Liskov Substitution Principle In any client code, if the supertype object is substituted by a subtype object, the.
Type Abstraction SWE Spring October 05Kaushik, Ammann Substitution Principle “In any client code, if supertype object is substituted.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Five design principles
Computer Science 209 Software Development Handing Errors and Creating Documentation.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
Defensive Programming CNS 3370 Copyright 2003, Fresh Sources, Inc.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
SOLID Design Principles
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
SOLID PHP & Code Smell Wrap-Up
Inheritance (Part 3) 1. Preconditions and Inheritance  precondition  what the method assumes to be true about the arguments passed to it  inheritance.
CSCI 383 Object-Oriented Programming & Design Lecture 22 Martin van Bommel.
2.4 Exceptions n Detects try { //code that may raise an exception and/or set some condition if (condition) throw exceptionName; //Freq. A string } n Handles.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Component Based Software Engineering
CSC 205 Java Programming II
CSE 331 Subtyping slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
EECE 310: Software Engineering
Type Abstraction SWE Spring 2009.
Adaptive Code Umamaheswaran Senior Software Engineer
Subtyping Rules David Evans cs205: engineering software BlackBear
CSC 480 Software Engineering
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
Type Abstraction Liskov, Chapter 7.
Subtype Polymorphism, Subtyping vs
Verified Subtyping with Traits and Mixins
EECE 310: Software Engineering
619 Final Review Last updated Spring 2010 © : Paul Ammann.
619 Final Review Fall 2017 Professor Ammann.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Lecture 13: Subtyping Rules Killer Bear Climber
1.
Type Abstraction SWE Spring 2013.
Some principles for object oriented design
Liskov Substitution Principle (LSP)
Generics, Lambdas and Reflection
Presentation transcript:

Adaptive Code Via C# 06.04.2017 Umamaheswaran @UMW1990

Agenda Recap (Solid Principles) Liskov Substitution Principle Interface Segregation Dependency Injection

Recap Agile overview What goes wrong with Agile Bad Code In Experience Not for all Projects

Problems with Inheritance

Liskov Substitution Principle If S is a subtype of T, then objects of type T may be replaced with objects of type S - Barbara Liskov Class T Object S Object T Class S: T

Why LSP? Collection of guidelines for creating inheritance hierarchies Client can reliable use any class or subclass without compromising the expected behaviour Client can remain unaware of changes to the client hierarchy As long as there are no changes to the interface there should no reason to change any existing code

LSP Ingredients Base Type The type (T) that clients have reference to. Clients call various methods, any of which can be over ridden or partially specialized by the subtype. Subtype Any one of a possible family of classes(S) that inherit from the base type (T). Clients should not know which specific subtype they are calling, nor should they need to. The client should behave the same regardless of the subtype instance that it is given. Context The way in which the client interacts with the subtype. If the client doesn’t interact with a subtype, the LSP can neither be honoured nor contravened

LSP Rules Contract Rules Preconditions cannot be strengthened in a subtype Postconditions cannot be weakened in a subtype Invariants conditions that must remain true of the supertype must be preserved in a subtype Variance Rules There must be contravariance of the method arguments in the subtype There must be covariance of the return types in the subtype No new exceptions can be thrown by the subtype unless they are part of the existing exception hierarchy

Contracts Return Type decimal CalculateShippingCost( float packageWeightInKilograms, Size<float> packageDimensionsInInches, RegionInfo destination) Parameter names Parameter Types

Pre conditions Defined as all of the conditions necessary for a method to run reliably and without fault. public decimal CalculateShippingCost( float packageWeightInKilograms, Size<float> packageDimensionsInInches, RegionInfo destination) { if(packageWeightInKilograms <= 0f) throw new Exception(); return -1; }

Post conditions Post conditions check whether an object is being left in a valid state as a method is exited. public decimal CalculateShippingCost( float packageWeightInKilograms, Size<float> packageDimensionsInInches, RegionInfo destination) { // pre conditions var shippingCost = (decimal) (packageDimensionsInInches.Length * packageDimensionsInInches.Width * packageDimensionsInInches.Height); if (shippingCost <= decimal.Zero) throw new Exception("The return value is our of range"); return shippingCost; }

Data Variant Predicate that remains true for the lifetime of an object public class ShippingStrategy { public ShippingStrategy(decimal flatRate) if(flatRate <=decimal.Zero) throw new ArgumentOutOfRangeException(nameof(flatRate),"Flat rate must be positive and non-zero"); FlatRate = flatRate; } protected decimal FlatRate { get; set; }

Variance Covariance: Supertype defines some fields and methods that are inherited by subtype. Subtype enhances the supertype by defining its own fields and methods Contravariance: relates to treatment of types that are used as return values Invariant: If a type is not variant at all, no arrangement of types will yield a class hirarchy

Thank you!!