Naming
Choosing the names of variables, functions and constants: a name that states the intent, the writing conventions of Python and Java, a function's verb, a boolean's prefix and hunting down magic numbers.
- 24 cards
- 5 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
- Understand
A few cards from the deck
Identifier: what is it?
Show the answer
A name chosen by the programmer to refer to a variable, a function, a class or a constant.
The program does not care which name is chosen, but the reader relies on it to understand:
psays nothing,price_excl_taxsays what the value stands for.Which names make this calculation readable?
```python
t = 0.2
p = 50
r = p * (1 + t)`taux_tva,prix_ht,prix_ttcnombre,valeur,calculTauxTva,PrixHt,PrixTtcfloat1,float2,float3
Show the answer
taux_tva,prix_ht,prix_ttcThe first three names state each value's role and follow Python's
snake_case.nombre,valeurandfloat1would fit anything;TauxTvais the shape of a class.A value written directly in the code with no explicit name is a …; it is replaced by a …, defined once.
Show the answer
A value written directly in the code with no explicit name is a magic number; it is replaced by a named constant, defined once.
The name makes the code readable and the value is changed in one place only, with no risk of missing an occurrence.
Naming convention: what is it?
Show the answer
A writing rule shared by a team or a language, which sets the shape of names according to their role: lowercase, uppercase, separators.
It lets you guess a name's role from its shape alone. It depends on the language:
calculer_moyennein Python,calculerMoyennein Java.A function tests whether a number is even and returns a boolean. Which name makes it clearest in use inside
if ... (n):?pairest_pairverifierfonction1
Show the answer
est_pairif est_pair(n):reads like a sentence.verifierdoes not say what is checked,fonction1says nothing, andpairlooks more like a value than a question.