Download presentation
Presentation is loading. Please wait.
Published byPaul Kinnett Modified over 9 years ago
1
Clean code
2
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
3
Reasons of bad code Changes of requirements The tight schedule Stupid bosses Intolerant clients Fools marketers Nerds-colleagues Our own incompetence! 3
4
Basic features of clear code Elegance Efficiency Simplicity Purposefulness Readability Literacy Predictability Symmetry Optimal use of programming language 4
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.