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?

コード設計学習14  結果を返すために引数を使わないこと

Posted at

このブログについて

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

結果を返すために引数を使わないことについて

メソッドが引数を通じて結果を書き換えると、データの流れが不透明になり、理解が困難になります。
呼び出し側から見ると、何が変更されるのかが明確ではなく、副作用により意図しない変更が起こる危険もあります。

悪いコード例

void GetUserInfo(User user)
{
    user.Name = "Taro";
    user.Email = "taro@example.com";
}

良いコード例

User GetUserInfo()
{
    return new User("Taro", "taro@example.com");
}
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?