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.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. 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: p says nothing, price_excl_tax says 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_ttc
    • nombre, valeur, calcul
    • TauxTva, PrixHt, PrixTtc
    • float1, float2, float3
    Show the answer

    taux_tva, prix_ht, prix_ttc

    The first three names state each value's role and follow Python's snake_case. nombre, valeur and float1 would fit anything; TauxTva is 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_moyenne in Python, calculerMoyenne in Java.

  • A function tests whether a number is even and returns a boolean. Which name makes it clearest in use inside if ... (n):?

    • pair
    • est_pair
    • verifier
    • fonction1
    Show the answer

    est_pair

    if est_pair(n): reads like a sentence. verifier does not say what is checked, fonction1 says nothing, and pair looks more like a value than a question.