Clean Code

31
Clean Code Best practice Luigi De Russis

description

A brief overview about writing clean code. Presentation made for the Multimedia Languages and Environments course at Politecnico di Torino (academic year 2012/2013).

Transcript of Clean Code

Page 1: Clean Code

Clean Code

Best practice Luigi De Russis

Page 2: Clean Code

Measuring code quality

07/03/2013 Clean Code

2

Page 3: Clean Code

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

Page 4: Clean Code

Goals

07/03/2013 Clean Code

4

Page 5: Clean Code

Code MUST be…

07/03/2013 Clean Code

Readable Maintainable

Extendable

5

Testable

Page 6: Clean Code

How?

07/03/2013 Clean Code

6

Page 7: Clean Code

07/03/2013 Clean Code 7

Page 8: Clean Code

Meaningful names

07/03/2013

8

Clean Code

Page 9: 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

Page 10: Clean Code

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

Page 11: Clean Code

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

Page 12: Clean Code

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

Page 13: Clean Code

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

Page 14: Clean Code

Another example…

07/03/2013 Clean Code

class Customer

{

private Date generationTimeStamp;

private Date modificationTimeStamp;

private final String recordId = “102”;

/* ... */

}

Better?

14

Page 15: Clean Code

Functions

07/03/2013

15

Clean Code

Page 16: Clean Code

Functions

07/03/2013 Clean Code

Small

Do One Thing

16

Page 17: Clean Code

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

Page 18: Clean Code

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

Page 19: Clean Code

Functions

07/03/2013 Clean Code

Handle errors (and use exceptions)

Don’t Repeat Yourself

(avoid copy-and-paste code)

19

Page 20: Clean Code

Comments

07/03/2013

20

Clean Code

Page 21: 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

Page 22: 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

22

Page 23: Clean Code

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

Page 24: Clean Code

Formatting

07/03/2013

24

Clean Code

Page 25: 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

Page 26: Clean Code

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

Page 27: Clean Code

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?

Page 28: Clean Code

Conventions

07/03/2013

28

Clean Code

Page 29: 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

Page 30: Clean Code

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

Page 31: Clean Code

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/