giving code a good name
TRANSCRIPT
![Page 1: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/1.jpg)
Giving Code a
Good Name
@KevlinHenney
![Page 2: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/2.jpg)
![Page 3: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/3.jpg)
![Page 4: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/4.jpg)
It's just naming.
![Page 5: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/5.jpg)
It's just semantics.
![Page 6: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/6.jpg)
It's just meaning.
![Page 7: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/7.jpg)
/ WordFriday
![Page 8: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/9.jpg)
As a
I want
So that
$Role
$Feature
$Benefit
![Page 10: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/10.jpg)
As a
I want
So that
programmer
$Feature
$Benefit
![Page 11: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/11.jpg)
As a
I want
So that
programmer
good identifier names
$Benefit
![Page 12: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/12.jpg)
As a
I want
So that
programmer
good identifier names
I can read code
![Page 13: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/13.jpg)
As a
I want
So that
programmer
good identifier names
I can read and understand code
![Page 14: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/17.jpg)
![Page 18: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/19.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/20.jpg)
![Page 21: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/22.jpg)
public class ConfigurationManager{
...}
![Page 23: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/23.jpg)
public class Configuration{
...}
![Page 24: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/27.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/28.jpg)
http://eightyeightgames.com/2011/03/14/rollrover-source-analysis/
![Page 29: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/29.jpg)
if (portfolioIdsByTraderId.get(trader.getId()).containsKey(portfolio.getId()))
{...
}
Dan North"Code in the Language of the Domain"
![Page 30: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/30.jpg)
if (trader.canView(portfolio)){
...}
Dan North"Code in the Language of the Domain"
![Page 31: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/31.jpg)
![Page 32: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/33.jpg)
pneumonoultramicroscopicsilicovolcanoconiosis
Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz
fylkestrafikksikkerhetsutvalgssekretariatslederfunksjonene
muvaffakiyetsizleştiricileştiriveremeyebileceklerimizdenmişsinizcesine
hippopotomonstrosesquipedaliophobia
![Page 34: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/34.jpg)
http://www.bonkersworld.net/object-world/
![Page 35: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/35.jpg)
http://www.bonkersworld.net/object-world/
OBJECT-ORIENTED
VenetianBlind Door
Television
PictureGlass
SofaTelevisionRemoteControl
Peephole
![Page 36: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/36.jpg)
AccessViolationException
ArgumentOutOfRangeException
ArrayTypeMismatchException
BadImageFormatException
CannotUnloadAppDomainException
EntryPointNotFoundException
IndexOutOfRangeException
InvalidOperationException
OverflowException
![Page 37: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/37.jpg)
AccessViolation
ArgumentOutOfRange
ArrayTypeMismatch
BadImageFormat
CannotUnloadAppDomain
EntryPointNotFound
IndexOutOfRange
InvalidOperation
Overflow
![Page 38: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/38.jpg)
ArgumentException
ArithmeticException
ContextMarshalException
FieldAccessException
FormatException
NullReferenceException
ObjectDisposedException
RankException
TypeAccessException
![Page 39: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/39.jpg)
Argument
Arithmetic
ContextMarshal
FieldAccess
Format
NullReference
ObjectDisposed
Rank
TypeAccess
![Page 40: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/40.jpg)
InvalidArgument
InvalidArithmeticOperation
FailedContextMarshal
InvalidFieldAccess
InvalidFormat
NullDereferenced
OperationOnDisposedObject
ArrayRankMismatch
InvalidTypeAccess
![Page 41: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/41.jpg)
Omit needless words.
William Strunk and E B White
The Elements of Style
![Page 42: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/42.jpg)
![Page 43: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/43.jpg)
![Page 44: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/44.jpg)
![Page 45: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/45.jpg)
![Page 46: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/46.jpg)
Naomi EpelThe Observation Deck
![Page 47: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/47.jpg)
LoanMember BookCopy
![Page 48: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/48.jpg)
LoanIMember IBookCopy
Member BookCopy
![Page 49: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/49.jpg)
Loan
Member BookCopy
![Page 50: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/50.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/51.jpg)
LoanBorrower LoanItem
Member BookCopy
![Page 52: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/52.jpg)
interface
![Page 53: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/53.jpg)
TreeBuilderParser
![Page 54: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/54.jpg)
TreeBuilderParser
BuilderImpl
![Page 55: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/55.jpg)
TreeParserListenerParser
Builder
![Page 56: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/56.jpg)
Tree
ParserListenerParser
Builder
![Page 57: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/57.jpg)
ConnectionConnection
Factory
ConnectionImpl
![Page 58: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/58.jpg)
ConnectionConnection
Pool
PooledConnection
![Page 59: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/59.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/60.jpg)
![Page 61: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/61.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/62.jpg)
[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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/63.jpg)
[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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/64.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/65.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/66.jpg)
[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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/67.jpg)
methodtest
test
test
method
method
test
test
![Page 68: Giving Code a Good Name](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/68.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/69.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/70.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/71.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022050806/5a64ddb27f8b9a2c5d8b4bf3/html5/thumbnails/72.jpg)
At some level
the style
becomes the
substance.