Clean code. Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing.

Slides:



Advertisements
Similar presentations
Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
Advertisements

A practical guide John E. Boal TestDrivenDeveloper.com.
Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Software Construction and Evolution - CSSE 375 Bad Smells in Code Shawn Bohner & Steve Chenoweth.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 4.0.
Objects First With Java A Practical Introduction Using BlueJ Designing object-oriented programs How to write code in a way that is easily understandable,
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
REFACTORING Improving the Design of Existing Code Atakan Şimşek e
Architectural Design Principles. Outline  Architectural level of design The design of the system in terms of components and connectors and their arrangements.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Test Driven Development Derived from Dr. Fawcett’s notes Phil Pratt-Szeliga Fall 2009.
Pattern Abstract Factory
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
LAYING OUT THE FOUNDATIONS. OUTLINE Analyze the project from a technical point of view Analyze and choose the architecture for your application Decide.
Development in hardware – Why? Option: array of custom processing nodes Step 1: analyze the application and extract the component tasks Step 2: design.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
1 CSE 403 Software Lifecycle Models Reading: Rapid Development Ch. 7, 25 (further reading: Ch. 21, 35, 36, 20) These lecture slides are copyright (C) Marty.
CSE 303 – Software Design and Architecture
CS 360 Lecture 3.  The software process is a structured set of activities required to develop a software system.  Fundamental Assumption:  Good software.
OHTO -99 SOFTWARE ENGINEERING LECTURE 5 Today: - An overview to OO Analysis and OO Design - Introduction of Assignment 2.
Chapter 4: Overview of Preventive Maintenance
Software Refactoring Part I: Introduction Bartosz Walter Advanced Object-Oriented Design Lecture 5.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 3.0.
Design and Programming Chapter 7 Applied Software Project Management, Stellman & Greene See also:
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Refactoring Improving the structure of existing code Refactoring1.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Cohesion and Coupling CS 4311
Refactoring1 Improving the structure of existing code.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 5.0.
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.
Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
The Software Development Process
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
Designing Classes. Software changes Software is not like a novel that is written once and then remains unchanged. Software is extended, corrected, maintained,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
A. Aimar - EP/SFT LCG - Software Process & Infrastructure1 SPI Software Process & Infrastructure for LCG Project Overview LCG Application Area Internal.
Integration Testing Beyond unit testing. 2 Testing in the V-Model Requirements Detailed Design Module implementation Unit test Integration test System.
Refactoring1 Improving the structure of existing code.
Pertemuan 12 Refactoring Mata kuliah: T0144 – Advanced Topics in Software Engineering Tahun: 2010.
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
CSSE 375 Organizing Data – Part 1 Shawn and Steve Q1.
Integrating the Code during the Development Alexander Vakrilov Telerik Corporation
Software Development Process CS 360 Lecture 3. Software Process The software process is a structured set of activities required to develop a software.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Module 9. Dealing with Generalization Course: Refactoring.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
© 2008 Cisco Systems, Inc. All rights reserved.Cisco ConfidentialPresentation_ID 1 Chapter 4: Overview of Preventive Maintenance IT Essentials 5.0.
Multi-cellular paradigm The molecular level can support self- replication (and self- repair). But we also need cells that can be designed to fit the specific.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Coupling and Cohesion Pfleeger, S., Software Engineering Theory and Practice. Prentice Hall, 2001.
Principles and examples
Coupling and Cohesion 1.
Behavioral Design Patterns
Refactoring and Code Smells
Objects First with Java
Code Smells 1.
Improving the structure of existing code
Refactoring and Code Smells
Refactoring Types Blake Duncan.
Refactoring and Code Smells
Refactoring.
Refactoring and Code Smells
Presentation transcript:

Clean code

Motivation Total cost = the cost of developing + maintenance cost Maintenance cost = cost of understanding + cost of changes + cost of testing + cost of delivery One of the strategy for the reduction of total cost is to increase investment in the initial development in the hope of reducing the cost of support or even get rid of its necessity. Our strategy of reducing the total cost is to reduce the cost of understanding of the code in the phase of support. Correlation of time of reading and writing code is 10:1. 2

Reasons of bad code Changes of requirements The tight schedule Stupid bosses Intolerant clients Fools marketers Nerds-colleagues Our own incompetence! 3

Basic features of clear code Elegance Efficiency Simplicity Purposefulness Readability Literacy Predictability Symmetry Optimal use of programming language 4

Code duplication Definition: one and the same logic is implemented in two or more places in your program Signs: – Syntactic duplication. – Semantic duplication. The results of the work of the two syntactically different pieces of code are the same. 5

Code duplication Problems: – Complicated support of the application. The introduction of new functionality requires fixes in two or more places. – Increases the appearance of errors. Sooner or later a moment comes when you forget one of the places where the duplicated code exist. – Complicated understanding of the code. Duplication increases the size of the code that leads for a complication. 6

Code duplication Solutions: – Selection method. In the simplest cases, you must select a method and replace the duplicate code to the call of a new method. If the code is duplicated in subclasses of the same level, you must make “Extraction of a method” and then apply the “Rising of a method”. – Selection class. If the code is duplicated in different classes, you must apply “Extraction of a class” in one of the classes and then use a new component in other classes. – Form template method. If the code is similar, but not identical, you can select matching fragments into a separate method and form the template method. 7

Long method Definition: method that is becoming clearer after its reduction. Signs: – A lot of strings. The method should always do one thing. Methods with a large number of string often do more, then it is required of them. – «Separating» comments. Comments that explain what is being done in a separate fragment of the method are the direct indication of the need to split method. 8

Long method Problems: – Long methods are difficult to understand and support. – Most often long methods are difficult or impossible to re-use. – Long method is hardly exposed to unit testing. 9

Long method Solutions: – “Extraction of a method”. You must find the agreed part and define a new method. – Decompose of conditional operator. When writing code, which checks some conditions, we can quickly come to the long method. You can try to define new methods for the part “then” and for the part “else”. 10

Big class Definition: class that does too many tasks. Signs: – A large number of attributes. – Dependence on a large number of heterogeneous modules (long list using/uses/import/include/etc). 11

Big class Problems: – Difficult to understand. – The presence of the attributes used only under certain conditions. – Difficult to use in unit testing. – Must be modified to add different functionality. 12

Big class Solutions: – Extraction of a class or extraction of a subclass. – Extraction of an interface. Find out how clients use the class and apply the refactoring. This will help to establish splitting the class further. 13

Long list of parameters Definition: method contains more than one or two parameters. Signs: – Parameters form groups of data. – The use (or misuse) of one or several parameters depends on the value of another parameter. 14

Long list of parameters Problems: – Long list of parameters is very difficult to understand. – They become contradictory and difficult to use in a time. – They have to constantly change in case of new data. – Long parameter lists make it difficult to test the method. 15

Long list of parameters Solutions: – Replace Parameter With Method. Parameter value can be obtained by calling the method that is already known. This object can be a field or the other option. – Preserve Whole Object. Instead of group of data obtained with a single object, it is better to pass the object itself. – Introduce Parameter Object. Parameters which are related to each other in a natural way can be gathered in one object. There is a chance of behavior that can be transferred to a new class (Range, Date, Version). 16