S. Single Responsibility Principle O. L. I. D.

Slides:



Advertisements
Similar presentations
Object Oriented Analysis And Design-IT0207 iiI Semester
Advertisements

A practical guide John E. Boal TestDrivenDeveloper.com.
Structured Design. 2 Design Quality – Simplicity “There are two ways of constructing a software design: One is to make it so simple that there are obviously.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Classes and Object- Oriented... tMyn1 Classes and Object-Oriented Programming The essence of object-oriented programming is that you write programs in.
Object-Oriented Metrics. Characteristics of OO ● Localization ● Encapsulation ● Information hiding ● Inheritence ● Object abstraction.
Fundamentals of Information Systems, Second Edition
By for Test Driven Development: Industry practice and teaching tool Robert Vanderwall, Ph.D. 1 WISTPC-15.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
Chapter 7 Software Engineering Introduction to CS 1 st Semester, 2015 Sanghyun Park.
GRASP: Designing Objects with Responsibilities
Fundamentals of Information Systems, Second Edition 1 Systems Development.
An Automatic Software Quality Measurement System.
Self-Documenting Code Chapter 32. Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Introduction to Software Architecture.
Robustness Initiative Jeff Kern NRAO. CASA Robustness and Reliability Number one priority from the CASA User Survey was reliability. – Survey did not.
PC204 Lecture 5 Programming Methodologies Copyright 2000 by Conrad Huang and the Regents of the University of California. All rights reserved.
Design and Planning Or: What’s the next thing we should do for our project?
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Class Design. Class Design The analysis phase determines what the implementation must do, and the system design.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Further Modularization, Cohesion, and Coupling. Simple Program Design, Fourth Edition Chapter 9 2 Objectives In this chapter you will be able to: Further.
Object Oriented Programming Some Interesting Genes.
Prototype 3 Prototype 2 Prototype What is prototyping? Types of prototyping: – Evolutionary – Throw-away Good and Bad points to prototyping.
Static Software Metrics Tool
Principles and examples
Mantas Radzevičius ifm-2/2
Steve Chenoweth Office Phone: (812) Cell: (937)
Software Testing.
“When quality is critical"
Metrics of Software Quality
Coupling and Cohesion Rajni Bhalla.
Michael J. Salé, Seidenberg School of CSIS, Westchester DPS 2016
Software Metrics 1.
LEARNING NEW SKILLS.
Chapter 18 Maintaining Information Systems
Conception OBJET GRASP Patterns
Chapter 11 Object-Oriented Design
Software Testing An Introduction.
Data Abstraction: The Walls
Copyright © by Curt Hill
Systems Analysis and Design
BASICS OF SOFTWARE TESTING Chapter 1. Topics to be covered 1. Humans and errors, 2. Testing and Debugging, 3. Software Quality- Correctness Reliability.
Abstract Data Types and Encapsulation Concepts
Component-Level Design
Objects First with Java
Introduction to Software Testing
The Object-Oriented Thought Process Chapter 05
Improving the Design “Can the design be better?”
Introduction to Design Patterns Part 1
Code Smells 1.
Building a Custom Gadget in OU Campus
CSCE 489- Problem Solving Programming Strategies Spring 2018
Programming Logic and Design Fourth Edition, Comprehensive
Refactoring Types Blake Duncan.
COP 3330 Object-oriented Programming in C++
Communication between modules, cohesion and coupling
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Use Case Analysis – continued
European conference.
Cohesion and Coupling.
Dependency Inversion principle
Interface Segregation Principle
Refactoring.
DESIGN CONCEPTS AND PRINCIPLES
Presentation transcript:

S. Single Responsibility Principle O. L. I. D. 1

1 2 3 4 5 Content Definition Advantages Example How to recognize a break of S.R.P? 5 How to make the design compliant with the S.R.P? www.seavus.com 2

01 Definition www.seavus.com

The single responsibility principle - definition Single Responsibility Principle is one of the five principles of SOLID Design Principles. From Wikipedia: …In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility…. From Clean Code: A class or module should have one, and only one, reason to change. www.seavus.com

What the definition means When we design our classes, we should take care that one class at the most is responsible for doing one task or functionality among the whole set of responsibilities that it has. And only when there is a change needed in that specific task or functionality should this class be changed. How to recognize it? SRP looks simple on the first glance, but it is, probably, the most difficult SOLID principle to follow in practice. Following SRP is difficult because “reasons to change” become certain only in the future, when requirements actually change. At design time, all we can do is estimate which requirements are likely to change and which are stable. One effective way of identifying “reasons to change” is to learn about the business domain that is targeted by the application. Since most of the requirements come from business domain, there is a high probability that any instability in that domain will propagate into the requirements. www.seavus.com

02 Advantages www.seavus.com

Advantages of S.R.P Organize the code Less fragile When a class has more than one reason to be changed, it is more fragile. A change in one location might lead to some unexpected behavior in totally other places. Low Coupling/High Cohesion More responsibilities lead to higher coupling. Higher coupling leads to more dependencies, which is harder to maintain. Cohesion refers to the degree to which the elements inside a class belong together. Code Changes Refactoring is much easier for a single responsibility module. Maintainability It’s obvious that it is much easier to maintain a small single purpose class, then a big monolithic one. Testability A test class for a ‘one purpose class’ will have less test cases (branches). If a class has one purpose it will usually have less dependencies, thus less mocking and test preparing. The “self documentation by tests” becomes much clearer. Easier Debugging In a single responsibility class, finding the bug or the cause of the problem, becomes a much easier task. www.seavus.com

03 Example www.seavus.com

What needs to have single responsibility? Each part of the system: The methods The classes The packages The modules www.seavus.com

04 How to recognize a break of Single Responsibility Principle? www.seavus.com

How to recognize a break of S. R. P? Class Has Too Many Dependencies A constructor with too many input parameters implies many dependencies (hopefully you do inject dependencies). Another way too see many dependencies is by the test class. If you need to mock too many objects, it usually means breaking the SRP. Method Has Too Many Parameters Same as the class’s smell. Think of the method’s parameters as dependencies. The Test Class Becomes Too Complicated If the test has too many variants, it might suggest that the class has too many responsibilities. It might suggest that some methods do too much. Class / Method is Long If a method is long, it might suggest it does too much. Same goes for a class. Descriptive Naming If you need to describe what your class / method / package is using with the AND world, it probably breaks the SRP. Change In One Place Breaks Another If a change in the code to add a new feature or simply refactor broke a test which seems unrelated, it might suggest a breaking the SRP. Shotgun effect If a small change makes a big ripple in your code. If you need to change many locations it might suggest, among other smells, that the SRP is broken. www.seavus.com 11

05 How to make the design compliant with the S.R.P? www.seavus.com

How to make the design compliant with the S.R.P? Awareness This is a general suggestion for clean code. We need to be aware of our code. We need to take care. As for SRP, we need to try and catch as early as we can a class that is responsible for too much. We need to always look for a ‘too big method’. Testable Code Write your code in a way that everything can be tested. Then, you will surly want that your tests be simple and descriptive. Code Coverage Metrics Sometimes, when a class does too much, it won’t have 100% coverage at first shot. Check the code quality metrics. www.seavus.com 13

Thank you for your attention Copyright: © 2019 Seavus. All rights reserved. | www.seavus.com | info@seavus.com | sales@seavus.com