1
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.

setterを書くな高校校歌

Last updated at Posted at 2021-04-25

#setterを書くな高校校歌
##作詞作曲:わたくし
迷光が巡る丘の上
値を代入するためにsetterを書こうとするな
若きアプリだとあなどらず
ロジックを持つクラスはコンストラクタで値を与えなさい

SetterDame.java
public class SetterDame {

    String value;
    
    //好き
    public SetterDame(String input) {
        this.value = input;
    }

    //よくない
    public setValue(String input) {
        this.value = input;
    }
}

大空へと今飛び立とう
Listの場合はreturn new コンストラクタで命を与えてやれ

SetterDame.java
public class SetterDame {

    List<String> values;
    
    //可視性好き
    private SetterDame(List<String> input) {
        this.values = input;
    }

    //好き(staticでも良い)
    public create(List<String> input) {
        return new SetterDame(input);
    }
}

setterはアプリがでかくなるとマジで追えなくなるぞ
おぉ 我らがsetterを書くな高校
setterを書くな高校

#参考書籍
現場で役立つシステム設計の原則 ~変更を楽で安全にするオブジェクト指向の実践技法~ 増田亨 著(技術評論社 2017年)

1
1
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
1
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?