Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Engineering and Architecture

Similar presentations


Presentation on theme: "Software Engineering and Architecture"— Presentation transcript:

1 Software Engineering and Architecture
Writing Clean Code

2 Henrik Bærbak Christensen
Motivation Code is written to Be compiled, deployed, and executed by users in order to serve their need So we make money, get salaries and can buy presents for our woman (or man, or children, or cats, or whatever…) Be maintained – that is read by humans and modified Henrik Bærbak Christensen

3 Henrik Bærbak Christensen
Motivation Functionally correct code can be next to impossible to maintain… Morale: Write Clean Code Henrik Bærbak Christensen

4 Henrik Bærbak Christensen
Analyzability Basically: can I understand the code fast? Is my understanding correct? Henrik Bærbak Christensen

5 Henrik Bærbak Christensen
How to ? Clean Code? Not an exact science !!! Kent Beck: ”Code smell” WTF measure Our Take at it Uncle Bob ”Clean Code” On Functions and Comments Uncle Henrik My own prejudices and tastes  Henrik Bærbak Christensen

6 Henrik Bærbak Christensen
Exercise We will be looking at Game.moveUnit(from, to) For AlphaCiv With a bit of combat Testcases based on AlphaCiv world layout Source: Group N in 2016, final prod. Code + some other uncleaniness I wrote my own test code Henrik Bærbak Christensen

7 Henrik Bærbak Christensen
Context moveUnit(Position from, Position to) Choice of data structures in GameImpl [Meta: Git rep ‘frsproject’, sub project ‘clean-code-lecture’ on master branch] Henrik Bærbak Christensen

8 TDD TestList => Test Cases
My own work I created a set of test cases for testing the group’s codebase. Generally, their code was functionally correct. Etc… Henrik Bærbak Christensen

9 The Method 5 minutes study
[Meta: Version 4527 of code base / my-solutions/dsoftark/tdd-own-solution] Functionally correct AlphaCiv+ (except details in combat) Henrik Bærbak Christensen

10 A Classification Template
Clean Code Properties A Classification Template

11 Classification Scheme
An attempt at systematics… Method Name: Wanted property is OK Example/argument Small Do One Thing One Level of Abstraction Use Descriptive Names Keep Number of Arguments Low Avoid Flag Arguments Have No Side Effects Command Query Separation Prefer Exceptions to Error Codes Don't Repeat Yourself Do the Same Thing, the Same Way Name Boolean Expressions Bail out Fast Arguments in Argument Lists Henrik Bærbak Christensen

12 Henrik Bærbak Christensen
Bob’s Properties Small Make it do 1-5 logical steps / ‘functional nuggets’ Do One Thing Do one thing, do it well, do it only! Keep focus! One Level of Abstraction The dean does not edit spelling errors in my slides! Use Descriptive Names Tell the one thing it does! Do not tell anything else Keep Number of Arguments low 0-1-2 maybe three arguments. No more. If more, your method does more than one thing! Eksempel: POS systems’ streg kode scanner kalder: ‘processItem(barcode)’ item = barcodeMap.get(barcode); addToReceipt(item); updateTotal(item.cost()); decrementInventory(item); Henrik Bærbak Christensen

13 Henrik Bærbak Christensen
Bob’s Properties Avoid Flag Arguments produceWebPage(true, true, false); Huh??? Boolean arguments says ”do more than one thing!” Right? Have No Side Effects Do not do hidden things / hidden state changes It will fool the client; and will hide weird bugs Ex: init a session; modify the object passed as argument If it does, the descriptive name, should reflect it! Henrik Bærbak Christensen

14 Henrik Bærbak Christensen
Bob’s Properties Command Query Separation Setters and Getters Accessors and Mutators Query: no state change!!! Return a value. Command: no return value! Change the state Prefer Exceptions to Error Codes Not ‘int addPayment(int amount)’ that returns error code 17 in case it is an illegal coin Don’t Repeat Yourself Avoid Duplicated Code To avoid multiple maintenance problem Henrik Bærbak Christensen

15 Henrik Bærbak Christensen
Don’t Repeat Yourself That is Why? Multiple maintenance problem !!! Root of all Evil Avoid Duplication Henrik Bærbak Christensen

16 Additions by Uncle Henrik
Clean Code Additions by Uncle Henrik Henrik Bærbak Christensen

17 Arguments in Argument Lists
One symptom on duplicated code is the use of ‘arguments’ in the method/class names addOneToX(int x); addTwoToX(int x); addThreeToX(int x); ??? An argument appears as part of the method name Henrik Bærbak Christensen

18 Do the Same Thing, the Same Way
Akin to Uncle Bob’s Do One Thing but not quite… Why? Analyzability WarStory DSE ‘string copy’ Do the same thing, the same way In the C library all functions that operate on a strings (copy, compare, …) have parameters (destination, source) in that order. My collegue had reversed it in his special copy function. Henrik Bærbak Christensen

19 Name Boolean Expressions
Boolean expressions are difficult to read! One big ball of mud of && between boolean computations  Henrik Bærbak Christensen

20 Name Boolean Expressions
Name each subexpression so we can read what it is! Give each boolean expression a name Yeah, should have been ‘LessThanOrEqualOne’  Henrik Bærbak Christensen

21 And Help from My Friends
Henrik Bærbak Christensen

22 Bail out fast When lots of conditions must be checked, I often see deep nesting of if Empirical studies show that humans cannot cope well with nesting levels deeper than 1-2 ! You can write it, but cannot maintain it Henrik Bærbak Christensen

23 Henrik Bærbak Christensen
Bail out fast Flatten it, by bailing out as soon as an answer can be computed Bail out fast Henrik Bærbak Christensen

24 Agile Method Development
Like any other creative process, you do not do it in one brilliant step! Kent Beck: Clean code that works but not in that order Make it work, then make it clean Commit horrible sins, and then clean up the mess Henrik Bærbak Christensen

25 Analysis

26 Henrik Bærbak Christensen
Method Name: moveUnit() Wanted property Is OK Small No Do One Thing One Level of Abstraction Use Descriptive Names Keep Number of Arguments Low Yes Avoid Flag Arguments Have No Side Effects Command Query Separation Prefer Exceptions to Error Codes n/a Don't Repeat Yourself Do SameThing, the Same Way Simple Boolean Expressions Bail out Fast Arguments in Argument Lists It is not small. It does several things (validation, actual movement in datastructures, combat resolving). It has many levels of abstraction (direct data manipulation and up). Ats+dfs are not descriptive names. It repeats itself in the actual unit movement code. A lot of boolean expressions are expressed directly and concatenated using AND. It does not bail out fast (it should say ‘if these conditions are not met, return false’). It has the player color argument encoded in the moveRedUnit method name. On the positive side, argument count is low, no flag arguments, no side effects (except the required one), it is a method that is a mutator (command separation), and there is no need for exceptions. Henrik Bærbak Christensen

27 Refactoring Session Online Coding Session

28 Henrik Bærbak Christensen
Let us clean up… Henrik Bærbak Christensen

29 45 Minutes Later Disclaimer: Issue with lost unit if attack fails 
Henrik Bærbak Christensen

30 Henrik Bærbak Christensen
Next level methods Henrik Bærbak Christensen

31 (Videos) 45 Minutes Later
Henrik Bærbak Christensen

32 Next Level Methods Note. I can still spot some clean up potential !
( ! notXXX is hard to read) Henrik Bærbak Christensen

33 Afterthoughts

34 Henrik Bærbak Christensen
The Two Codebases TDD produce two large codebases The production code The test code The properties of clean code apply to both You want to be able to read test code also! (Analyzability) Except one property: Arguments in Argument Lists In test code, you often ‘hardwire parameters’ / no abstraction Example: ”private void moveRedArcherToPosition45()” Encapsulates the moves of particular unit to particular position in order to do testing! Henrik Bærbak Christensen


Download ppt "Software Engineering and Architecture"

Similar presentations


Ads by Google