[RUBY] Don't use magic numbers ...

What is a magic number?

The code I actually wrote ...

 prefectures = Prefecture.where(area_id: 3)
 cities = City.where(prefecture_id: 13)

Such a code. It's a number that you don't know what the number is when someone other than yourself sees it.

In my head ,,, 3 in area_id is Kanto prefecture_id 13 is Tokyo I was writing the code as a matter of course.

Code after change

  kantou = Area.find_by(name: 'Kanto')
  prefectures = Prefecture.where(area_id: kantou)
  tokyo = Prefecture.find_by(name: 'Tokyo')
  cities = City.where(prefecture_id: tokyo)

Instead of using numbers, I stored them in variables that anyone can easily see.

Recommended Posts

Don't use magic numbers ...
Don't use `get ()` ~ Defeated Optional