giving code a good name

72
Giving Code a Good Name @ KevlinHenney

Upload: kevlin-henney

Post on 22-Jan-2018

659 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Giving Code a Good Name

Giving Code a

Good Name

@KevlinHenney

Page 2: Giving Code a Good Name
Page 3: Giving Code a Good Name
Page 4: Giving Code a Good Name

It's just naming.

Page 5: Giving Code a Good Name

It's just semantics.

Page 6: Giving Code a Good Name

It's just meaning.

Page 7: Giving Code a Good Name

/ WordFriday

Page 8: Giving Code a Good Name

code, noun

▪ a set of instructions for a computer

▪ a computer program, or a portion thereof

▪ a system of words, figures or symbols used to represent others,

especially for the purposes of secrecy

▪ a set of conventions or principles governing behaviour or activity in

a particular domain

Concise Oxford English Dictionary ∙ Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary

Page 9: Giving Code a Good Name

As a

I want

So that

$Role

$Feature

$Benefit

Page 10: Giving Code a Good Name

As a

I want

So that

programmer

$Feature

$Benefit

Page 11: Giving Code a Good Name

As a

I want

So that

programmer

good identifier names

$Benefit

Page 12: Giving Code a Good Name

As a

I want

So that

programmer

good identifier names

I can read code

Page 13: Giving Code a Good Name

As a

I want

So that

programmer

good identifier names

I can read and understand code

Page 14: Giving Code a Good Name

As a

I want

So that

programmer

good identifier names

I can spend less time determining the meaning of code and more time coding meaningfully

Page 15: Giving Code a Good Name

As a

I want

So that

programmer

???

I can spend less time determining the meaning of code and more time coding meaningfully

Page 16: Giving Code a Good Name

Signal-to-noise ratio (often abbreviated SNR or S/N) is a

measure used in science and engineering that compares the

level of a desired signal to the level of background noise.

Signal-to-noise ratio is sometimes used informally to refer

to the ratio of useful information to false or irrelevant data

in a conversation or exchange.

http://en.wikipedia.org/wiki/Signal_to_noise_ratio

Page 17: Giving Code a Good Name
Page 18: Giving Code a Good Name

To be, or not to be: that is the question:

Whether 'tis nobler in the mind to suffer

The slings and arrows of outrageous fortune,

Or to take arms against a sea of troubles,

And by opposing end them?

William Shakespeare

Hamlet

Page 19: Giving Code a Good Name

Continuing existence or cessation of existence:

those are the scenarios. Is it more empowering

mentally to work towards an accommodation of the

downsizings and negative outcomes of adversarial

circumstance, or would it be a greater enhancement

of the bottom line to move forwards to a challenge

to our current difficulties, and, by making a

commitment to opposition, to effect their demise?

Tom Burton

Long Words Bother Me

Page 20: Giving Code a Good Name
Page 21: Giving Code a Good Name

People will be using the words you

choose in their conversation for the

next 20 years. You want to be sure

you do it right.

Unfortunately, many people get all

formal [...]. Just calling it what it is

isn't enough.

Page 22: Giving Code a Good Name

public class ConfigurationManager{

...}

Page 23: Giving Code a Good Name

public class Configuration{

...}

Page 24: Giving Code a Good Name

They have to tack on a flowery,

computer science-y, impressive

sounding, but ultimately

meaningless word, like Object,

Thing, Component, Part, Manager,

Entity, or Item.

Page 25: Giving Code a Good Name

Comments

A delicate matter, requiring taste and judgement. I tend to err on the side of eliminating comments, for several reasons. First, if the code is clear, and uses good type names and variable names, it should explain itself. Second, comments aren't checked by the compiler, so there is no guarantee they're right, especially after the code is modified. A misleading comment can be very confusing. Third, the issue of typography: comments clutter code.

Rob Pike, "Notes on Programming in C"

Page 26: Giving Code a Good Name

There is a famously bad comment style:

i=i+1; /* Add one to i */

and there are worse ways to do it:

/**********************************

* *

* Add one to i *

* *

**********************************/

i=i+1;

Don't laugh now, wait until you see it in real life.

Rob Pike, "Notes on Programming in C"

Page 27: Giving Code a Good Name

A common fallacy is to assume authors of incomprehensible code will somehow be able to express themselves lucidly and clearly in comments.

Kevlin Henneyhttps://twitter.com/KevlinHenney/status/381021802941906944

Page 28: Giving Code a Good Name

http://eightyeightgames.com/2011/03/14/rollrover-source-analysis/

Page 29: Giving Code a Good Name

if (portfolioIdsByTraderId.get(trader.getId()).containsKey(portfolio.getId()))

{...

}

Dan North"Code in the Language of the Domain"

Page 30: Giving Code a Good Name

if (trader.canView(portfolio)){

...}

Dan North"Code in the Language of the Domain"

Page 31: Giving Code a Good Name
Page 32: Giving Code a Good Name

Agglutination is a process in linguistic morphology

derivation in which complex words are formed by

stringing together morphemes, each with a single

grammatical or semantic meaning.

Languages that use agglutination widely are called

agglutinative languages.

http://en.wikipedia.org/wiki/Agglutination

Page 33: Giving Code a Good Name

pneumonoultramicroscopicsilicovolcanoconiosis

Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz

fylkestrafikksikkerhetsutvalgssekretariatslederfunksjonene

muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine

hippopotomonstrosesquipedaliophobia

Page 34: Giving Code a Good Name

http://www.bonkersworld.net/object-world/

Page 35: Giving Code a Good Name

http://www.bonkersworld.net/object-world/

OBJECT-ORIENTED

VenetianBlind Door

Television

PictureGlass

SofaTelevisionRemoteControl

Peephole

Page 36: Giving Code a Good Name

AccessViolationException

ArgumentOutOfRangeException

ArrayTypeMismatchException

BadImageFormatException

CannotUnloadAppDomainException

EntryPointNotFoundException

IndexOutOfRangeException

InvalidOperationException

OverflowException

Page 37: Giving Code a Good Name

AccessViolation

ArgumentOutOfRange

ArrayTypeMismatch

BadImageFormat

CannotUnloadAppDomain

EntryPointNotFound

IndexOutOfRange

InvalidOperation

Overflow

Page 38: Giving Code a Good Name

ArgumentException

ArithmeticException

ContextMarshalException

FieldAccessException

FormatException

NullReferenceException

ObjectDisposedException

RankException

TypeAccessException

Page 39: Giving Code a Good Name

Argument

Arithmetic

ContextMarshal

FieldAccess

Format

NullReference

ObjectDisposed

Rank

TypeAccess

Page 40: Giving Code a Good Name

InvalidArgument

InvalidArithmeticOperation

FailedContextMarshal

InvalidFieldAccess

InvalidFormat

NullDereferenced

OperationOnDisposedObject

ArrayRankMismatch

InvalidTypeAccess

Page 41: Giving Code a Good Name

Omit needless words.

William Strunk and E B White

The Elements of Style

Page 42: Giving Code a Good Name
Page 43: Giving Code a Good Name
Page 44: Giving Code a Good Name
Page 45: Giving Code a Good Name
Page 46: Giving Code a Good Name

Naomi EpelThe Observation Deck

Page 47: Giving Code a Good Name

LoanMember BookCopy

Page 48: Giving Code a Good Name

LoanIMember IBookCopy

Member BookCopy

Page 49: Giving Code a Good Name

Loan

Member BookCopy

Page 50: Giving Code a Good Name

role, noun

▪ an actor's part in a play, film, etc.

▪ a person or thing's function in a particular

situation

▪ a function, part or expected behaviour

performed in a particular operation or process

Concise Oxford English Dictionary ∙ Merriam-Webster's Collegiate Dictionary

Page 51: Giving Code a Good Name

LoanBorrower LoanItem

Member BookCopy

Page 52: Giving Code a Good Name

interface

Page 53: Giving Code a Good Name

TreeBuilderParser

Page 54: Giving Code a Good Name

TreeBuilderParser

BuilderImpl

Page 55: Giving Code a Good Name

TreeParserListenerParser

Builder

Page 56: Giving Code a Good Name

Tree

ParserListenerParser

Builder

Page 57: Giving Code a Good Name

ConnectionConnection

Factory

ConnectionImpl

Page 58: Giving Code a Good Name

ConnectionConnection

Pool

PooledConnection

Page 59: Giving Code a Good Name

Everybody knows that TDD stands for Test Driven Development. However, people too often concentrate on the words "Test" and "Development" and don't consider what the word "Driven" really implies.

For tests to drive development they must do more than just test that code performs its required functionality: they must clearly express that required functionality to the reader.

That is, they must be clear specifications of the required functionality. Tests that are not written with their role as specifications in mind can be very confusing to read.

Nat Pryce and Steve Freeman"Are Your Tests Really Driving Your Development?"

Page 60: Giving Code a Good Name
Page 61: Giving Code a Good Name

public class Stack<T>{

private public Stack() public int Depth public T Top public void Push(T newTop) public void Pop()

}

Page 62: Giving Code a Good Name

[TestFixture]public class StackTests{

[Test]public void TestConstructor() [Test]public void TestDepth() [Test]public void TestTop() [Test]public void TestPush() [Test]public void TestPop()

}

Page 63: Giving Code a Good Name

[TestFixture]public class StackTests{

[Test]public void Constructor() [Test]public void Depth() [Test]public void Top() [Test]public void Push() [Test]public void Pop()

}

Page 64: Giving Code a Good Name

public class Stack<T>{

private public Stack() public int Depth public T Top public void Push(T newTop) public void Pop()

}

Page 65: Giving Code a Good Name

public class Stack<T>{

private

public int Depth public T Top public void Push(T newTop) public void Pop()

}

Page 66: Giving Code a Good Name

[TestFixture]public class StackTests{

[Test]public void Depth() [Test]public void Top() [Test]public void Push() [Test]public void Pop()

}

Page 67: Giving Code a Good Name

methodtest

test

test

method

method

test

test

Page 68: Giving Code a Good Name

namespace Stack_spec{

[TestFixture]public class A_new_stack{

[Test]public void has_no_depth()

}[TestFixture]public class An_empty_stack{

[Test]public void throws_when_queried_for_its_top_item() [Test]public void throws_when_popped() [Test]public void acquires_depth_by_retaining_a_pushed_item_as_its_top()

}[TestFixture]public class A_non_empty_stack{

[Test]public void becomes_deeper_by_retaining_a_pushed_item_as_its_top() [Test]public void on_popping_reveals_tops_in_reverse_order_of_pushing()

}}

Page 69: Giving Code a Good Name

namespace Stack_spec{

[TestFixture]public class A_new_stack{

[Test]public void has_no_depth()

}[TestFixture]public class An_empty_stack{

[Test]public void throws_when_queried_for_its_top_item() [Test]public void throws_when_popped() [Test]public void acquires_depth_by_retaining_a_pushed_item_as_its_top()

}[TestFixture]public class A_non_empty_stack{

[Test]public void becomes_deeper_by_retaining_a_pushed_item_as_its_top() [Test]public void on_popping_reveals_tops_in_reverse_order_of_pushing()

}}

Page 70: Giving Code a Good Name

As a

I want

So that

programmer

???

I can spend less time determining the meaning of code and more time coding meaningfully

Page 71: Giving Code a Good Name

As a

I want

So that

programmer

identifiers to communicate directly and with intent

I can spend less time determining the meaning of code and more time coding meaningfully

Page 72: Giving Code a Good Name

At some level

the style

becomes the

substance.