Is it bad practice to make a setter return “this”?

前端 未结 27 779
抹茶落季
抹茶落季 2020-11-27 09:39

Is it a good or bad idea to make setters in java return \"this\"?

public Employee setName(String name){
   this.name = name;
   return this;
}
相关标签:
27条回答
  • 2020-11-27 09:53

    I used to prefer this approach but I have decided against it.

    Reasons:

    • Readability. It makes the code more readable to have each setFoo() on a separate line. You usually read the code many, many more times than the single time you write it.
    • Side effect: setFoo() should only set field foo, nothing else. Returning this is an extra "WHAT was that".

    The Builder pattern I saw do not use the setFoo(foo).setBar(bar) convention but more foo(foo).bar(bar). Perhaps for exactly those reasons.

    It is, as always a matter of taste. I just like the "least surprises" approach.

    0 讨论(0)
  • 2020-11-27 09:54

    It's not a bad practice at all. But it's not compatiable with JavaBeans Spec.

    And there is a lot of specification depends on those standard accessors.

    You can always make them co-exist to each other.

    public class Some {
        public String getValue() { // JavaBeans
            return value;
        }
        public void setValue(final String value) { // JavaBeans
            this.value = value;
        }
        public String value() { // simple
            return getValue();
        }
        public Some value(final String value) { // fluent/chaining
            setValue(value);
            return this;
        }
        private String value;
    }
    

    Now we can use them together.

    new Some().value("some").getValue();
    

    Here comes another version for immutable object.

    public class Some {
    
        public static class Builder {
    
            public Some build() { return new Some(value); }
    
            public Builder value(final String value) {
                this.value = value;
                return this;
            }
    
            private String value;
        }
    
        private Some(final String value) {
            super();
            this.value = value;
        }
    
        public String getValue() { return value; }
    
        public String value() { return getValue();}
    
        private final String value;
    }
    

    Now we can do this.

    new Some.Builder().value("value").build().getValue();
    
    0 讨论(0)
  • 2020-11-27 09:55

    I'm in favor of setters having "this" returns. I don't care if it's not beans compliant. To me, if it's okay to have the "=" expression/statement, then setters that return values is fine.

    0 讨论(0)
  • 2020-11-27 09:55

    This may be less readable

    list.add(new Employee().setName("Jack Sparrow").setId(1).setFoo("bacon!")); 
    

    or this

    list.add(new Employee()
              .setName("Jack Sparrow")
              .setId(1)
              .setFoo("bacon!")); 
    

    This is way more readable than:

    Employee employee = new Employee();
    employee.setName("Jack Sparrow")
    employee.setId(1)
    employee.setFoo("bacon!")); 
    list.add(employee); 
    
    0 讨论(0)
  • 2020-11-27 09:57

    If I'm writing an API, I use "return this" to set values that will only be set once. If I have any other values that the user should be able to change, I use a standard void setter instead.

    However, it's really a matter of preference and chaining setters does look quite cool, in my opinion.

    0 讨论(0)
  • 2020-11-27 09:58

    In general it’s a good practice, but you may need for set-type functions use Boolean type to determine if operation was completed successfully or not, that is one way too. In general, there is no dogma to say that this is good or bed, it comes from the situation, of course.

    0 讨论(0)
提交回复
热议问题