4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【新人注目】マジックナンバーは使わないで・・・。

Last updated at Posted at 2020-09-30

マジックナンバーとは

実際に僕が書いたコード・・・

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

こんなコード。
ようするに自分以外の人が見たら何の数字のことかわからない番号のこと。

自分の頭の中では,,,
area_idの3は関東
prefecture_idの13は東京都
という感じで当たり前のようにコードを書いていた。

変更後のコード

  kantou = Area.find_by(name: '関東')
  prefectures = Prefecture.where(area_id: kantou)
  tokyo = Prefecture.find_by(name: '東京都')
  cities = City.where(prefecture_id: tokyo)

番号を使うのではなく、誰が見てもすぐにわかる変数に格納した。

4
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?