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?

コード設計学習9 再代入

Posted at

このブログについて

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

再代入について

一見シンプルなコードでも、再代入を多用すると値の変化が追えなくなります。
特に規模が大きくなると、どこで値が変わったのかが分からなくなり、
修正やデバッグに時間を奪われます。

悪いコード例

int total = 100;
total = total + 50; // 再代入
total = total - 30; // 再代入
Console.WriteLine(total);

このような「上書き型のロジック」は、短いスクリプトでは問題なくても、大きなプログラムでは混乱の元です。
変数totalがどの時点でどんな意味を持つのか、読み手にとっても自分にとっても分かりづらくなります。

良いコード例

int baseAmount = 100;
int added = baseAmount + 50;
int discounted = added - 30;

Console.WriteLine(discounted);

それぞれの変数がどの段階の値なのかが一目で分かるようになり、後から読む人も理解しやすくなります。

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?