0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

コード設計学習4 変数の使い分け

Posted at

このブログについて

最近システム設計に興味を持ち、特にコード設計について学んだことをまとめます。
自分の今後の戒めも込めて。

変数の使い分けについて

同じ変数に別の意味を持たせると、後で読む人は「今この時点での値は何?」と混乱してしまいます。
変数には一つの目的だけを持たせることで、バグの混入を防ぐことができます。

悪いコード例

int value = GetUserCount();
value = value + GetAdminCount();
Console.WriteLine(value);

同じ変数valueに別の意味を持たせているため、後から読むと意図がわかりにくくなります。

良いコード例

int userCount = GetUserCount();
int adminCount = GetAdminCount();
int totalCount = userCount + adminCount;
Console.WriteLine(totalCount);

目的ごとに変数を用意するだけで、ロジックの意図が明確になり、読みやすく安全なコードになります。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?