Variables of the Veracious Variety:

How to better name your variables

Β @AdrienneTacke

πŸ‘‹ Copenhagen Developers Festival

"There are only two hard things in Computer Science: cache invalidation and naming things"

- Phil Karlton

naming things.

naming things

Why is naming important?

One

Why is it difficult?

Two

The 3 Cs of great variables

Three

Variable variables

Four

Why is naming important?

🌳

Naming is a way of placing order on our world by helping us differentiate between things.

Why is naming important?

🌳

Onyx

Tux

Domino

Oreo

Why is naming important?

🌳

Onyx

Why is naming important?

🌳

Adrienne

Why is naming important?

🌳

Adrienne

Why is naming important?

🌳

The way scientists name things

scientific nomenclature

Why is naming important?

🌳

Scientists prevent confusion by being specific.

specific

Why is naming important?

🌳

?

Why is naming important?

🌳

Say what strange White Oak is this...

It was a Rounded Oak, I believe.

The Rough Oak still stands, day after day!

Quercus alba

AKA "White Oak"

Why is naming important?

🌳

And there stood before me, a White Oak.

NOT White Oak

Possibly Maple

Why is naming important?

🌳

Carl Linnaeus

Swedish Naturalist

Why is naming important?

🌳

developed unique naming system

Every organism in the world is assigned a unique binomial name.

binomial name

Why is naming important?

🌳

binomial name

Homo

sapiens

genus

species modifier

Why is naming important?

🌳

binomial name

Homo

sapiens

genus

species modifier

Humans

Why is naming important?

🌳

Why is it difficult?

πŸ˜‘

Why is it difficult?

πŸ˜‘

Guidance on naming is mostly syntactical.

syntactical

Why is it difficult?

πŸ˜‘

syntactical

camelCase

kebab-case

PascalCase

snake_case

i

ht

long

TotalInventoryCountForEndOfDay

casing

length

auth

usr

Why is it difficult?

πŸ˜‘

syntactical

camelCase

kebab-case

PascalCase

snake_case

i

ht

long

TotalInventoryCountForEndOfDay

casing

length

auth

usr

"Use camelCase for variable names..."

"Avoid kebab-case since it clashes with subtraction syntax..."

"Use short identifier names for short scopes"

"Keep name length to a twenty five character maximum"

Why is it difficult?

πŸ˜‘

semantics

The meaning of names

Why is it difficult?

πŸ˜‘

semantics

The meaning of names

Why is it difficult?

πŸ˜‘

semantics

"Use meaningful, descriptive names..."

"Names should be nonambiguous."

"Try making identifiers self-documenting."

meaningful

descriptive

self-documenting

nonambiguous

Why is it difficult?

πŸ˜‘

semantics

meaningful

descriptive

self-documenting

nonambiguous

"meaningful"

"descriptive"

"self-documenting"

"nonambiguous"

Why is it difficult?

πŸ˜‘

meaningful

descriptive

self-documenting

nonambiguous

Names that are

are not enough...

Why is it difficult?

πŸ˜‘

correct

Names have to be

highly debatable

"Β  Β  Β  Β  "

.

Why is it difficult?

πŸ˜‘

homonyms

same words, different meaning

Why is it difficult?

πŸ˜‘

homonyms

homophones

homographs

"same writing"

same spelling, different meaning

"same sound"

same pronunciation, different meaning

Why is it difficult?

πŸ˜‘

homonyms

homophones

homographs

English

duck

"I took my pet duck to the Gucci store."

"Duck your head going while down the stairs!"

rock

"There was a great rockΒ band last night."

"Almost four tons of solid rock had to be removed."

Why is it difficult?

πŸ˜‘

homonyms

homophones

homographs

English

duck

"I took my pet duck to the Gucci store."

"Duck your head going while down the stairs!"

rock

"There was a great rockΒ band last night."

"Almost four tons of solid rock had to be removed."

write

"Turn right at the next intersection."

"WriteΒ a book, they said."

hour

"You are with me for the hour."

"What do you mean 'our' cake?"

right

our

English

Why is it difficult?

πŸ˜‘

homonyms

homophones

homographs

English

duck

"I took my pet duck to the Gucci store."

"Duck your head going while down the stairs!"

rock

"There was a great rockΒ band last night."

"Almost four tons of solid rock had to be removed."

write

"Turn right at the next intersection."

"WriteΒ a book, they said."

hour

"You are with me for the hour."

"What do you mean 'our' cake?"

right

our

English

same spelling, different meaning

same pronunciation, different meaning

Why is it difficult?

πŸ˜‘

homonyms

skΓ₯l

(cheers!)

Danish

skΓ₯l

(bowl)

jul

(christmas)

hjul

(wheel)

tayo

(we)

Tagalog

tayo

(to stand)

basa

(to read)

basa

(wet)

Why is it difficult?

πŸ˜‘

synonyms

different words, same meaning

Why is it difficult?

πŸ˜‘

synonyms

Why is it difficult?

πŸ˜‘

synonyms

count

tally

enumeration of numbers

summation

keep count

to mark

Names

Concepts

Words chosen as identifiers

Associated meanings and perceptions tied to a name

Why is it difficult?

πŸ˜‘

Florian Deissenboeck Β· Markus Pizka

Concise and consistent naming

Published in Software Quality Journal (2006)

Why is it difficult?

πŸ˜‘

Source: Deissenboeck, F., & Pizka, M. (2006). Concise and consistent naming. Software Quality Journal, 14, 261-282.

Why is it difficult?

πŸ˜‘

Source: Deissenboeck, F., & Pizka, M. (2006). Concise and consistent naming. Software Quality Journal, 14, 261-282.

"In computer programs, homonyms pose an obstacle for program comprehension since the developer has to take all elements of the set Cn into account when spotting an identifier named n."

Why is it difficult?

πŸ˜‘

Source: Deissenboeck, F., & Pizka, M. (2006). Concise and consistent naming. Software Quality Journal, 14, 261-282.

"Synonyms unnecessarily increase the domain of N and the relation R...raising the learning effort of the language used."

Why is it difficult?

πŸ˜‘

Source: Deissenboeck, F., & Pizka, M. (2006). Concise and consistent naming. Software Quality Journal, 14, 261-282.

"On encountering name n1, which is synonym to n2, the reader must take concepts c1 and c2 into account."

Why is it difficult?

πŸ˜‘

Source: Deissenboeck, F., & Pizka, M. (2006). Concise and consistent naming. Software Quality Journal, 14, 261-282.

Ideally, we get to this

Why is it difficult?

πŸ˜‘

"When 334 developers were asked to choose 47 variable names, the median probability that two would choose the same name was just 6.9%."

Source: Feitelson, D. G., Mizrahi, A., Noy, N., Shabat, A. B., Eliyahu, O., & Sheffer, R. (2020). How developers choose names

How do we choose better variable names then?

The 3 Cs of Great Naming

🀩

The 3 Cs of Great Naming

🀩

Consistent

Concise

Correct

The 3 Cs of Great Naming

🀩

Concise

Correct

Consistent

Identifier needs to be unique enough to convey the concept.

The 3 Cs of Great Naming

🀩

Correct

Identifier needs to accurately convey the concept and minimize related concept space as much as possible.

Consistent

Concise

Identifier needs to be unique enough to convey the concept.

The 3 Cs of Great Naming

🀩

Consistent

Concise

Correct

Identifier needs to convey what is actually being assigned.

Identifier needs to accurately convey the concept and minimize related concept space as much as possible.

Identifier needs to be unique enough to convey the concept.

Variable Variables

πŸ€“

Booleans

Can only be one of two values: true or false

Booleans

Can only be one of two values: true or false

isSomething

doesSomething

hasSomething

Examples:

isUpgrade, isApproval, isProMember

Examples:

allowsWhitespace, willUpdate, didRestart

Examples:

hasMoreThanOneWarning, hasMultipleDiscounts, hasException

condition: return True if at least one user is active

isUsersActive

const {something} = users.some(user => user.isActive);

condition: return True if at least one user is active

isUsersActive

const isUsersActive = users.some(user => user.isActive);
isUsersActive

isUsersActive

condition: return True if at least one user is active

isAtLeastOneUserActive

isAtLeastOneUserActive

const isAtLeastOneUserActive = users.some(user => user.isActive);
isAtLeastOneUserActive

condition: return True if at least one user is active

isOneUserActive

isOneUserActive

const isOneUserActive = users.some(user => user.isActive);
isOneUserActive

condition: return True if at least one user is active

isSomeUserActive

isSomeUserActive

const isSomeUserActive = users.some(user => user.isActive);
isSomeUserActive

condition: return True if at least one user is active

isAnyUserActive

const isAnyUserActive = users.some(user => user.isActive);

🀩

Integers

Things understood to be numbers; counts/number of something

Integers

Things understood to be numbers; counts/number of something

{integer itself}

numberOfSomething

somethingCount

Examples:

age, year, MAXIMUM_ALLOWED_LOGIN_ATTEMPTS, tooltipDelayInMilliseconds

Examples:

numberOfRetries, numberOfDescendants, numberOfAccounts, numberOfDaysWithoutRain

Examples:

failureCount, retryCount, currentCakeInventoryCount

condition: return number of eligible discount items that are part of a customer's order.

numberOfEligibleItemsWithinTransaction

condition: return number of eligible discount items that are part of a customer's order.

numberOfEligibleItemsWithinTransaction

numberOfEligibleItemsInTransaction

condition: return number of eligible discount items that are part of a customer's order.

numberOfEligibleItemsWithinTransaction

numberOfEligibleItemsInTransaction

numberOfEligibleItems

condition: return number of eligible discount items that are part of a customer's order.

numberOfEligibleItemsWithinTransaction

numberOfEligibleItemsInTransaction

numberOfEligibleItems

numberOfEligibleDiscountItems

🀩

Floating-Point Numbers

Unrounded measures;

numbers that are not integers

Floating-Point Numbers

Unrounded measures; numbers that are not integers

{floating-point itself}

somethingAmount

Examples:

height, weight, priceInDanishKrone, angleInDegrees, highTemperatureInFahrenheit

Examples:

discountAmount, transferAmount, refundAmount

Strings

Labels; typically a sequence of characters

Strings

Labels; typically a sequence of characters

{entity itself}

somethingAsString

Examples:

fullGivenName, city, shortSpeakerBiography, playerSelectedClass, definition

Examples:

monthAsString, timeZoneAsString

Collections

Arrays, lists, or sets; groups of things

Collections

Arrays, lists, or sets; groups of things

{plural form of thing}

implementationOfThings

Examples:

robots, sentientRobots, discountedProducts, customers, newlyReleasedBooks

Examples:

unorderedListOfCustomers, queueOfFirstPriorityTasks, orderedSetOfTimeStamps

Maps

Accessing values by keys

Maps

Accessing values by keys

keyToValueMap

Examples:

bookIdToAuthorMap, customerToOrderTotalMap, productIdToSuppliersMap

Pairs & Tuples

Typically stored together; items of the same type

Pairs & Tuples

Typically stored together and items of the same type

firstPairAndSecondPair

Examples:

lengthAndWidth, setsAndRepitions, currentAndLifetimeXP, genusAndSpecies

firstSecondAndThirdThing

Examples:

heightWidthAndDepthInCentimeters, saturatedTransAndTotalFatInGrams, redGreenBlueAndAlpha

"Great code is understood; rarely is it interpreted."

- Adrienne Braganza Tacke

Salamat/Tak #CPHDevFest!

Adrienne Braganza Tacke

Β @AdrienneTacke

Currently writing this!

πŸ€”πŸ° Know a good pΓ’tisserie/bakery?

Please come find me and let's chat!

Variables of the Veracious Variety

By Adrienne Tacke

Variables of the Veracious Variety

First presented at Copenhagen Developers Festival (August 2023) As the famous Phil Karlton quote goes: "There are only two hard things in Computer Science: cache invalidation and naming things". This talk focuses on one of those things: naming! Why is naming so difficult? How do we craft concise, clear, and consistent variable names? What makes a variable name concise, clear, and consistent? In this talk, I'd like to discuss the importance of naming, walk through examples of variable names and improve them, and set some clear guidelines and tips on how to name things. By the end of this talk, you'll leave with the indispensable skill of effectively naming things!

  • 421