Naming variables

Waarvoor doen we het?

  • Tijdbesparing
    • Leesbare code
    • Minder bugs door verkeerd interpreteren
    • Bonus: easy search and replace

1. De basics

  • Gebruik bestaande conventions
  • Gebruik dezelfde termen als in documentatie
  • Vermijd reserved keywords at all costs
    • Rails: https://riptutorial.com/ruby-on-rails/example/32446/reserved-word-list
    • JS: https://www.w3schools.com/js/js_reserved.asp

2. Resolve ambiguity

cars = 3
car_num = 3
car_count = 3
car_total = 3

3. Haal soortgelijke termen uit elkaar

home = "Huisje boompje beestje"
home = "Meidoornstraat 1105"
home = <Building>
home = <Appartment>
home = "25.1356° N, 55.2206° E"

home_name = "Huisje boompje beestje"
home_address = "Meidoornstraat 1105"
home_building = <Building>
home_appartment = <Appartment>
home_location = "25.1356° N, 55.2206° E"

4. Gebruik singular/plural consistent

document = <Document>
documents = [<Document>, <Document>]
document_list = [<Document>, <Document>]
documents_list = [
  [<Document>, <Document>],
  [<Document>, <Document>]
]

5. Gebruik prefix/postfix en wees consistent

max_score = 300
score_avg = 100
users_average = 150
employee_mean = 170

points_total = 120
points_max = 300
points_min = 100
points_mean = 130

6. Gebruik tegenpolen

small_image = <Image>
large_image = <Image>

fraudulent_users = [<User>, <User>]
honest_users = [<User>, <User>]

affected_windows = [<Window>, <Window>]
non_affected_windows = [<Window>, <Window>]

7. Zoek synoniemen op

right_cases = [<Case>, <Case>]

correct_cases = [<Case>, <Case>]

8. Gebruik meer specifieke namen wanneer nodig

# only one kind of medicine
medicines = [<Medicine>, <Medicine>]

# multiple kinds of medicines
deprecated_medicines = [<Medicine>, <Medicine>]
in_use_medicines = [<Medicine>, <Medicine>]

9. Gebruik geen afkortingen (tenzij iedereen die kent)

bab_avg = 420.69
broker_ab_avg = 420.69

broker_account_balances_avg = 420.69

broker_account_balances.average do |broker_account_balance|
  broker_account_balance.value
end

10. Gebruik tussenvariabelen

years = milliseconds / 31536000000
years = milliseconds / 1000 / 60 / 60 / 24 / 365

MILLISECONDS_IN_SECOND = 1000
SECONDS_IN_MINUTE = 60
MINUTES_IN_HOUR = 60
HOURS_IN_DAY = 24
DAYS_IN_YEAR = 365

years = milliseconds / MILLISECONDS_IN_SECOND
	/ SECONDS_IN_MINUTE
	/ MINUTES_IN_HOUR
	/ HOURS_IN_DAY
	/ DAYS_IN_YEAR

11. Vermijd nietszeggende namen

document.process
value = 24

document.check_and_send
default_temperature_value = 24

12. Bedenk nieuwe term om verwarring te voorkomen

document_id = document.id

document.each_with_index do |document, document_id|
  ...
end

document.each_with_index do |document, document_index|
  ...
end

13. Booleans: geen verwarring met acties

file.open = true

file.is_open = true
file.opened = true

14. Schrijf formules voluit (of link naar uitleg)

f = m * a

force = mass * acceleration

Title Text





questions_from_audience.present?





Naming variables

By mjorden

Naming variables

  • 29