clean code - polito.itelite.polito.it/.../lucidi/moduloa/cleancode.pdf · license clean code...

Post on 17-Jun-2020

5 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Clean Code

Best practice Luigi De Russis

Measuring code quality

07/03/2013 Clean Code

2

The only valid measurement: WTFs/min

07/03/2013 Clean Code

Good code Bad code

CODE REVIEW

CODE REVIEW

WTF

WTF

WTF

WTF

WTF

Dude, WTF

WTF, this is shit!

WTF

3

Goals

07/03/2013 Clean Code

4

Code MUST be…

07/03/2013 Clean Code

Readable Maintainable

Extendable

5

Testable

How?

07/03/2013 Clean Code

6

07/03/2013 Clean Code 7

Meaningful names

07/03/2013

8

Clean Code

An example…

07/03/2013 Clean Code

public List<int[]> getThem()

{

List<int[]> list = new ArrayList<int[]>();

for (int[] x : theList)

if (x[0] == 4)

list.add(x);

return list;

}

What does this code do?

9

An example…

07/03/2013 Clean Code

public List<int[]> getFlaggedCells()

{

List<int[]> flaggedCells = new ArrayList<int[]>();

for (int[] cell : gameBoard)

if (cell[STATUS_VALUE] == FLAGGED)

flaggedCell.add(cell);

return flaggedCells;

}

Better?

10

An example…

07/03/2013 Clean Code

public List<Cell> getFlaggedCells()

{

List<Cell> flaggedCells = new ArrayList<Cell>();

for (Cell cell : gameBoard)

if (cell.isFlagged())

flaggedCell.add(cell);

return flaggedCells;

}

What about this?

11

What we have done

cell[STATUS_VALUE]

rather than x[0]

07/03/2013 Clean Code

Used intention

revealing names

Replaced magic

numbers with constants

flaggedCells rather

than list

Created an appropriate

abstract data type

Cell cell rather than int[] cell

12

Another example…

07/03/2013 Clean Code

class DtaRcrd102

{

private Date genymdhms;

private Date modymdhms;

private final String pszqint = “102”;

/* ... */

}

Use pronounceable and searchable names

(please!)

13

Another example…

07/03/2013 Clean Code

class Customer

{

private Date generationTimeStamp;

private Date modificationTimeStamp;

private final String recordId = “102”;

/* ... */

}

Better?

14

Functions

07/03/2013

15

Clean Code

Functions

07/03/2013 Clean Code

Small

Do One Thing

16

Do One Thing

public bool isEdible()

{

if(this.ExpirationDate > Date.Now &&

this.ApprovedForConsumption == true &&

this.InspectorId != null) {

return true;

else return false;

}

How many things is the function doing?

07/03/2013 Clean Code

17

Do One Thing

public bool isEdible()

{

return isFresh() &&

isApproved() &&

isInspected();

}

Now the function is doing one thing!

A change in the specification turns into a single

change in the code.

07/03/2013 Clean Code

18

Functions

07/03/2013 Clean Code

Handle errors (and use exceptions)

Don’t Repeat Yourself

(avoid copy-and-paste code)

19

Comments

07/03/2013

20

Clean Code

Explain yourself in the code

Which one is cleaner?

//check to see if the employee is eligible for full benefits

if((employee.flags & HOURLY_FLAG) && (employee.age > 65))

if(employee.isEligibleForFullBenefits())

07/03/2013 Clean Code

21

Explain yourself in the code

Which one is cleaner?

//check to see if the employee is eligible for full benefits

if((employee.flags & HOURLY_FLAG) && (employee.age > 65))

if(employee.isEligibleForFullBenefits())

07/03/2013 Clean Code

22

07/03/2013 Clean Code

NO Orphan comments

Obsolete comments

Noise comments

Code commented-out

YES API Documentation

Explanation of intent

Clarification

Warning of consequences

23

Formatting

07/03/2013

24

Clean Code

Formatting

07/03/2013 Clean Code

Communication is the purpose of formatting

Vertical openness between concepts

25

Each blank line is a visual cue that identifies a new and separate concept

Breaking Indentation

public class CommentWidget extends TextWidget {

public static final String REGEXP =

“^#[\r\n]*(?:(?:\r\n)|\n|\r)?”;

public CommentWidget(String text) { super(text); }

public String render() throws Exception { return “”; }

}

Eh?

07/03/2013 Clean Code

26

Breaking Indentation

public class CommentWidget extends TextWidget

{

public static final String REGEXP =

“^#[\r\n]*(?:(?:\r\n)|\n|\r)?”;

public CommentWidget(String text)

{

super(text);

}

public String render() throws Exception

{

return “”;

}

}

07/03/2013 Clean Code

27

Better?

Conventions

07/03/2013

28

Clean Code

Conventions

07/03/2013 Clean Code

Stick to the language-specific conventions

Respect team-level conventions

29

Still complying with the language-specific conventions…

Conventions enable common understanding

References

07/03/2013 Clean Code

30

Robert C. Martin Series, “Clean Code - A Handbook

of Agile Software Craftsmanship”, Prentice Hall

Geek and Poke,

http://geekandpoke.typepad.com/

OSNews Comics, http://www.osnews.com/comics

License

07/03/2013 Clean Code

31

This work is licensed under the Creative Commons “Attribution-NonCommercial-ShareAlike Unported (CC BY-NC-SA 3,0)” License.

You are free:

to Share - to copy, distribute and transmit the work

to Remix - to adapt the work

Under the following conditions:

Attribution - You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

Noncommercial - You may not use this work for commercial purposes.

Share Alike - If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

top related