117
Clean Code: A Reader-Centered Approach @matthewrenze #sddconf

Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Clean Code:A Reader-Centered Approach

@matthewrenze

#sddconf

Page 2: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 3: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 4: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 5: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 6: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 7: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

About Me

Independent consultant

EducationB.S. in Computer Science (ISU)

B.A. in Philosophy (ISU)

CommunityPublic Speaker

Pluralsight Author

Microsoft MVP

ASPInsider

Open-Source Software

Page 8: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Overview

Clean Code

Names

Functions

Classes

Comments

Process

Page 9: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Source: http://www.bernie-eng.com/blog/wp-content/uploads/2011/03/code.jpg

Page 10: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Bad Code?

Difficult to read

Difficult to understand

Difficult to maintain

Contains bugs

Contains surprises

Page 11: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

The Total Cost of Owning a Mess

Source: Clean Code

Page 12: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Source: http://talkingincode.com/wp-content/uploads/2014/12/crapcode.png

Page 13: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight
Page 14: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

The way we avoid a mess is by keeping our code clean.

Page 15: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Page 16: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Simple and direct

Reads like well-written prose

Never obscures the designer’s intent

Full of crisp abstractions

Contains straight-forward lines of controlGrady Booch

Co-inventor of UML

Page 17: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Runs all the tests

Expresses all the design ideas in the system

Minimizes the number of entities

Minimizes duplication

Expresses ideas clearlyRon Jeffries

Co-inventor of XP

Page 18: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Readable by others

Has unit tests

Has meaningful names

Has minimal dependencies

Do one thingDave Thomas

Co-Author of The Pragmatic Programmer

Page 19: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

“You know you are working on clean code when each routine you read turns out to be pretty much what you expected.”

Ward CunninghamInventor of the Wiki

Co-inventor of XP

Page 20: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Simple

Readable

Understandable

Maintainable

Testable Matthew RenzeGenius, Billionaire, Playboy,

Philanthropist : )

Page 21: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

What is Clean Code?

Code that is written for the reader of the code… not for the author… or the machine

Page 22: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Why Should We Invest in Clean Code?

Sources: • Barry Boehm - Software Engineering Economics, Prentice Hall • Schach, R., Software Engineering, Fourth Edition, McGraw-Hill• Glass, Robert, Frequently Forgotten Fundamental Facts about Software Engineering

Page 23: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Why Should We Invest in Clean Code?

Source: Clean Code

Page 24: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Clean Code is an Investment

Clean code makes it easier to:Write new code

Maintain old code

Invest in code readability

Page 25: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

How Do You Write Clean Code?

Write code for the reader

Not for the author

Not for a machine

Page 26: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

How Do You Write Clean Code?

“Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live!”

- Author Unknown

Page 27: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Names

Page 28: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Choose Names Thoughtfully

Inigo Montoya

Page 29: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Intention-Revealing Names

// Bad - Terse variable nameint d; // days in queue

// Goodint daysInQueue;

Page 30: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Intention-Revealing Names

// Bad – Unclear method nameprivate int Process();

// Goodprivate int ParseCustomerIdFromFile();

Page 31: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Names from Problem Domain

// Problem domainpublic class Customer {}

public void AddAccount();

Page 32: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Names from Solution Domain

// Solution domainpublic class Factory {}

public void AddToJobQueue();

Page 33: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Names from Both Domains

// Both domainspublic class CustomerFactory {}

public void AddAccountToJobQueue();

Page 34: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Disinformation

// Bad - misleadingISet<Customer> customerList;

Page 35: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Pronounceable Names

// Bad – Not pronounceable namespublic class DtaRcrd102{

private DateTime genymdhms;

private DateTime modymdhms;

private string pszqint = "102";}

Page 36: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Pronounceable Names

// Bad – Not pronounceable namespublic class DtaRcrd102{

private DateTime genymdhms;

private DateTime modymdhms;

private string pszqint = "102";}

// Good – Pronounceable namespublic class Customer{

private DateTime generationTimestamp;

private DateTime modificationTimestamp;

private string recordId = "102";}

Page 37: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Encodings

// Bad – Hungarian Notationprivate int intSomeValue = 123;

Page 38: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Encodings

// Bad - Module prefixesprivate int m_SomeField = 0;

Page 39: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Encodings

// OK... Maybe?private int _someField = 0;

Page 40: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Class Names

// Good - Noun or noun phrasepublic class Customer

public class AddressParser

public class AddAccountCommand

Page 41: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Class Names

// Bad - Fuzzy namespublic class ObjectManager

public class EntityProcessor

public class Stuff

// Good - Noun or noun phrasepublic class Customer

public class AddressParser

public class AddAccountCommand

Page 42: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Method Names

// Good - Verb or verb phrasepublic void AddCustomer()

public void DeleteAccount()

public string ParseAddress()

Page 43: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Method Names

// Bad - Fuzzy namespublic string Process()

public void DoWork()

// Good - Verb or verb phrasepublic void AddCustomer()

public void DeleteAccount()

public string ParseAddress()

Page 44: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Method Names

// Good - Boolean predicatespublic bool IsValid()

public bool HasAccount()

Page 45: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Variable Names Should Increase with Scope

// Good - Very short range variable namesfor (int i = 0; i < 10; i++) {}

list.Sum(p => p.GetAmount());

Page 46: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Variable Names Should Increase with Scope

// Good - Very short range variable namesfor (int i = 0; i < 10; i++) {}

list.Sum(p => p.GetAmount());

// Good - Short method variable namesvar balance = GetAccountBalance();

Page 47: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Variable Names Should Increase with Scope

// Good - Very short range variable namesfor (int i = 0; i < 10; i++) {}

list.Sum(p => p.GetAmount());

// Good - Short method variable namesvar balance = GetAccountBalance();

// Good - Longer field variable namesprivate int totalAccountBalance = 0;

Page 48: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Variable Names Should Increase with Scope

// Good - Very short range variable namesfor (int i = 0; i < 10; i++) {}

list.Sum(p => p.GetAmount());

// Good - Short method variable namesvar balance = GetAccountBalance();

// Good - Longer field variable namesprivate int totalAccountBalance = 0;

// Good - Even longer global variable namespublic int totalBalanceInAllBankAccounts;

Page 49: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Method Names Should Decrease with Scope

// Good - Short public method namespublic void GetCustomers();

public void Save();

Page 50: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Method Names Should Decrease with Scope

// Good - Short public method namespublic void GetCustomers();

public void Save();

// Good - Longer private method namesprivate string ParseHtmlFromFile()

private int GetIdFromAccountHolder()

Page 51: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Class Names Should Decrease with Scope

// Good - Short public class namepublic class Account

Page 52: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Class Names Should Decrease with Scope

// Good - Short public class namepublic class Account

// Good - Longer private class nameprivate class AccountNumberGenerator

Page 53: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Length of Class Names Should Decrease with Scope

// Good - Short public class namepublic class Account

// Good - Longer private class nameprivate class AccountNumberGenerator

// Good - Longer derived class namepublic abstract class Account

public class SavingsAccount : Account

Page 54: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Functions

Page 55: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Functions Should Be Small

Simpler

Easier to read

Easier to understand

Easier to test

Contain less bugs

Page 56: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

How Small?

Most evidence says:Less than 20 lines

Uncle Bob says:Less than 10 lines

Average 3 to 6 lines

Page 57: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Large Functions are Where Classes Go to Hide

Page 58: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Functions Should Do One Thing

Source: http://www.wengerna.com/giant-knife-16999

Page 59: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

One Level of Abstraction per Function

// Good - Separate levels of abstractionpublic File CreateFile()

public Html RenderHtml()

private string RenderHtmlBody()

private string RenderHtmlElement()

private char RenderHtmlElementClosingTag()

Page 60: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Minimize the Number of Parameters

// Try to minimize the # of argumentspublic void SetNone() {}

public void SetOne(int arg1)

public void SetTwo(int arg1, int arg2)

public void SetThree(int arg1, int arg2, int arg3)

public void SetMany(Args args)

Page 61: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Flag Arguments

// Bad – Flag argumentspublic void Render(bool useColor)

Page 62: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Flag Arguments

// Bad – Flag argumentspublic void Render(bool useColor)

// Good – No flag argumentspublic void RenderInColor()

public void RenderInGrayScale()

Page 63: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Output Arguments

// Bad - Uses ‘out’ argumentpublic void AppendFooter(out Report report) {…

}

AppendFooter(out report);

Page 64: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Output Arguments

// Bad - Uses ‘out’ argumentpublic void AppendFooter(out Report report) {…

}

AppendFooter(out report);

// Good - No ‘out’ argumentpublic ReportBuilder AppendFooter(){…

}

reportBuilder.AppendFooter();

Page 65: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Command-Query Separation

CommandDoes something

Should modify state

Should not return a value

Page 66: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Command-Query Separation

CommandDoes something

Should modify state

Should not return a value

QueryAnswers a question

Should not modify state

Always returns a value

Page 67: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Command-Query Separation

CommandDoes something

Should modify state

Should not return a value

QueryAnswers a question

Should not modify state

Always returns a value

Avoid mixing the two!

Page 68: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Side Effects

Page 69: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Duplication

Source: Sony Pictures Home Entertainment

Page 70: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Functions to Enhance Readability

// Bad – One giant chunk of codepublic void CreateReport(){... Giant block of code ...

}

Page 71: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Use Functions to Enhance Readability

// Bad – One giant chunk of codepublic void CreateReport(){... Giant block of code ...

}

// Good – Uses small named functionspublic void CreateReport(){

CreateHeader();

CreateBody();

CreateFooter();}

Page 72: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Classes

Page 73: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Classes Should Be Small

Similar benefits as small functions

Single-Responsibility Principle

Page 74: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

How Small?

Source: Clean Code

Page 75: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Classes Should Be Narrow

Source: Clean Code

Page 76: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Follow the Law of Demeter

// Bad - Law of Demeter violationvar rent = customer.Pocket.Wallet

.Money.GetRentMoney();

Page 77: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Follow the Law of Demeter

// Bad - Law of Demeter violationvar rent = customer.Pocket.Wallet

.Money.GetRentMoney();

// Good - No violationvar rent = customer.GetRentMoney();

Page 78: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Follow the Law of Demeter

// Bad - Law of Demeter violationvar rent = customer

.Pocket.Wallet

.Money.GetRentMoney();

// Good - No violationvar rent = customer.GetRentMoney();

Source: Athens Banner-Herald

Page 79: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Object vs. Data Structure

public class Rectangle{

private double x;...public double GetX(){

return x;}...public double GetArea(){

return width * height;}

}

Page 80: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Object vs. Data Structure

public class Rectangle{

private double x;...public double GetX(){

return x;}...public double GetArea(){

return width * height;}

}

public struct Rectangle{

public double X;

public double Y;

public double Width;

public double Height;}

Page 81: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Avoid Hybrid Object/Structures

Source: http://www.layoutsparks.com/1/147428/alien-resurrection-scary-dreadful-31000.html

Page 82: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Have a Consistent Order

public class SomeClass{

private const int SomeConst = 123;

private int _someField;

private int SomeProperty {...}

public SomeClass() {...}

public void DoSomethingPublic() {...}

private void DoSomethingPrivate() {...}}

Page 83: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Choose the Right Abstractions

Model

View

Controller

Repository

Factory

Builder

Adapter

Page 84: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Other Practices for Classes

DRY Principle

High Cohesion

Low Coupling

Dependency Injection

Testability

Page 85: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Comments

Page 86: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Comments Represent a Failure

Source: http://a.tgcdn.net/images/products/zoom/no_comment.jpg

Page 87: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Obsolete Comments Lie

Page 88: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Explain Yourself in Code// Bad - Code explained in comment

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

if ((employee.FullTime || SalaryFlag)

&& (employee.Age > 65))

Page 89: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Explain Yourself in Code// Bad - Code explained in comment

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

if ((employee.FullTime || SalaryFlag)

&& (employee.Age > 65))

// Good - Code explains itself

private bool IsEligibleForFullBenefits(Employee employee)

{

return ((employee.FullTime || SalaryFlag)

&& employee.Age > 65))

}

Page 90: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Explain Yourself in Code// Bad - Code explained in comment

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

if ((employee.FullTime || SalaryFlag)

&& (employee.Age > 65))

// Good - Code explains itself

private bool IsEligibleForFullBenefits(Employee employee)

{

return ((employee.FullTime || SalaryFlag)

&& employee.Age > 65))

}

if (IsEligibleForFullBenefits(employee))

Page 91: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Bad Comments

// All of these comments are bad

// Opens the filevar file = File.Open();

// Returns day of monthprivate int GetDayOfWeek()

// 08-07-2013 – Fixed Bug (MLR)

Main(){…

} // end main

Page 92: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Zombie Code

// Zombie Code// if (a == 1)// b = c + 1

Source: The Walking Dead

Page 93: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Zombie Code

// Zombie Code// if (a == 1)// b = c + 1

Source: The Walking Dead

Kill it with fire!

Page 94: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Necessary Comments

// Copyright © 2017 Matthew Renze

// Trim is necessary to prevent a// search term mismatch

// Warning: Slow running test

// TODO: Refactor to factory pattern

/// <summary>/// Opens the file for reading/// </summary>

Page 95: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

The Best Comment is No Comment at All

(but only if our code clearly explains itself)

Page 96: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

The Process

Page 97: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

The Principles

Test-Driven Development (TDD)

Simplicity (KISS)

Continuous Refactoring

Red

GreenRefactor

Page 98: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Test-Driven Development Process

1. Create a failing unit test

2. Code the simplest thing

3. Refactor until the code is clean

Red

GreenRefactor

Page 99: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Test-Driven Development

Starts with a test

Tests drive the design

Code evolves over time

Red

GreenRefactor

Page 100: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

TDD Code is:

Testable

Maintainable

Reliable

Self-documenting

Clean

Easy to keep clean

Red

GreenRefactor

Page 101: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Simplicity

Keep it Simple (KISS)

Unnecessary complexity

You Ain’t Gonna Need It (YAGNI)

Incremental algorithmics

Red

GreenRefactor

Page 102: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Continuous Refactoring

Working code is not the last step

Refactor until clean

Continuous process

Red

GreenRefactor

Page 103: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Continuous Refactoring

All creative endeavors are iterative processes

Red

GreenRefactor

Page 104: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Follow the Boy Scout Rule

“Leave the campground just a little bit cleaner than you found it.”

– adapted from Robert Stephenson Smyth Baden-Powell’s farewell message to the scouts: “Try and leave this world a little better than you found it.”

Page 105: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

“Leave the campground just a little bit cleaner than you found it.”

Page 106: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Conclusion

Page 107: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Conclusion

Clean code is:Simple

Readable

Understandable

Maintainable

Testable

Clean code is a philosophy of writing code for the reader

Page 108: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Conclusion

Use intention revealing names

Classes and functions should be small

Use comments to express a failure

The process is:1. Test First (TDD)

2. Simplest solution

3. Continuously refactor

Page 109: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Where to Go Next

Page 110: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Where to Go Next

Page 111: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Where to Go Next

http://pluralsight.com/training/Courses/TableOfContents/writing-clean-code-humans

Page 112: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Where to Go Next

Articles

Courses

Presentations

Source Code

Videos

www.matthewrenze.com

Page 113: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Feedback

Feedback is very important to me!

One thing you liked?

One thing I could improve?

Page 114: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

“Programming is not about telling the computer what to do.

Programming is the art of telling another human what the computer should do.”

- Donald Knuth

Page 115: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

“Any fool can write code that a computer can understand.

Good programmers write code that humans can understand.”

- Martin Fowler

Page 116: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

“To leave the campground just a little bit cleaner than you found it.”

Uncle Bob Wants You:

Page 117: Clean Code - SDD Conference · 2017. 5. 5. · About Me Independent consultant Education B.S. in Computer Science (ISU) B.A. in Philosophy (ISU) Community Public Speaker Pluralsight

Contact Info

Matthew Renze

Data Science Consultant

Renze Consulting

Twitter: @matthewrenze

Email: [email protected]

Website: www.matthewrenze.com

Thank You! : )