Clean Code: Motivations, Challenges, and a Book Review

23
Yet attentiveness to even more critical professionalism than vision detail is an foundation of is any grand Clean Code: A Quick Review linkedin.com/in/elgeish

Transcript of Clean Code: Motivations, Challenges, and a Book Review

Page 1: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

Clean Code:A Quick Review

linkedin.com/in/elgeish

Page 2: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

Things Covered Here

1 About the Book

2 Why Clean Code?

3 Things We Liked

4 Things We Disliked

2

Page 3: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Coding for Machines

Quick Reminder

IN AX, DX

MOV [ES:DI], AX

INT 3

MOV AX, [DS:SI]

OUT DX, AX

3

Page 4: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

About the Book

Page 5: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 It’s all about craftsmanshipA Handbook of Agile Software Craftsmanship

5

• Amazon best seller in software testing

• Uncle Bob is also known for SOLID

• He also wrote The Clean Coder (2011)

Page 6: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

Why Clean Code?

Page 7: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Because we write code for humans, and not just machinesCode Is Speech:

7Source: xkcd.com/1421

Page 8: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 “We are honest about the state of our code because code is never perfect”, and that’s – actually – desirable!

Code Is Never Perfect:

8Source: xkcd.com/844

Page 9: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 “[Coding] stops as soon as minimally-accepted results are found” - Guillaume Ferrero (1894)

Principle of Least Effort:

9

Source: xkcd.com/221

Page 10: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Seeking delayed gratification (clean code) => a more successful lifeThe Stanford Marshmallow Experiment:

10

Acceptable result?

Demand excellence

Have a cookie!

No

Yes

Have two cookies!

Page 11: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 The secret is dopamineA Neuroscientific Look at Meh Code

11

• Coding is iterative• A failure causes low dopamine levels• Task completion => a spike in dopamine levels• Moving on to a new task => dopamine levels rise

“[Yet] newness is no virtue and oldness is no vice. Truth and beauty and goodness are not determined by when they exist.”

- Pastor John Piper

Page 12: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

Things We Liked

Page 13: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Can save your lifeCoding Standards

13

• Consistent indentation style => fewer bugs • Reduce attention deficit (Thinking, Fast and Slow)• Pick them, stick to them, and automate if possible

“Code formatting is about communication,and communication is the professional

developer's first order of business.”

Page 14: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 As if you’re naming a first-born childMeaningful Names

14

• We spend disproportionately more time reading coding (than writing it)

• Use the shortest most expressive name that you can find easily

“[An entity's name] should tell you why it exists, what it does, and how it is used.”

Page 15: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Keep everything smallMinimalism

15

• Files, classes, functions, etc. should be small• SRP: Do one thing; do it well; and do it only!

Page 16: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 // Do you really need this comment?Comments

16

• When Bugs Become Features: Norman Doors• Can’t test a comment (TODO vs. @Expiration)• Comments are spells of magic; they charm the

reader into thinking they must be truer than code

“The proper use of comments is to compensate for our failure to express

[ourselves] in code.”

Page 17: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

Things We Disliked

Page 18: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Sometimes renaming might introduce bugsMeaningful Names

18

• Tangled code reviews might hide an issue• Binary serialization might use private field names;

renaming them may cause compatibility issues!

Page 19: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Not the cleanest code

 Uncle Bob showed the use of comments to explain intent in this example:

 public int compareTo(Object o) {  if (o instanceof WikiPagePath) {  WikiPagePath p = (WikiPagePath) o;  String compressedName = StringUtil.join(names, “”);  String compressedArgumentName = StringUtil.join(p.names, “”);  return compressedName.compareTo(compressedArgumentName);  }  return 1; // we are greater because we are the right type. }

Comments

19

Page 20: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 How would you review his code?

 Unnecessary Objects: There are better ways to compare iterables without creating throwaway objects (and especially not strings that are expensive to create – see Item 5 in Effective Java).

 Variable Names: they’re not meaningful.

 Choice of Words: "we" is not a known reference in programming; "this" is though, and it would have been more apt here.

 Eliminate the Comment Altogether: Implement Comparable{T} instead, or skip the type check; compareTo should throw ClassCastException if the types are different. Comparing two objects of different types violates many contracts.

Comments

20

Page 21: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 SRPFunctions

21

• “Functions should either do something or answer something, but not both”

• Yet, the Command-Query pattern is sometimes pragmatic in concurrent programming:• test-and-set: writes to a memory location, and

returns the old value• compare-and-swap: compares the value x at a

given memory location to a given value y, and sets the former to a given new value z only if x is equal to y; the return value indicates whether or not the swap occurred

Page 22: Clean Code: Motivations, Challenges, and a Book Review

Yet attentiveness toeven more critical

professionalism than vision

detail is an foundation of is any grand

 Missing sectionsFunctions

22

• Generics• Overloading• Default Parameters• Scope (visibility)• Testability