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

前端 未结 27 777
抹茶落季
抹茶落季 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 10:06

    On first sight: "Ghastly!".

    On further thought

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

    is actually less error prone than

    Employee anEmployee = new Employee();
    anEmployee.setName("xxx");
    ...
    list.add(anEmployee);
    

    So quite interesting. Adding idea to toolbag ...

    0 讨论(0)
  • 2020-11-27 10:06

    Long ago answer, but my two cents ... Its fine. I wish this fluent interface were used more often.

    Repeating the 'factory' variable does not add more info below:

    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(Foo.class);
    factory.setFilter(new MethodFilter() { ...
    

    This is cleaner, imho:

    ProxyFactory factory = new ProxyFactory()
    .setSuperclass(Properties.class);
    .setFilter(new MethodFilter() { ...
    

    Of course, as one of the answers already mentioned, the Java API would have to be tweaked to do this correctly for some situations, like inheritance and tools.

    0 讨论(0)
  • 2020-11-27 10:07

    Because it doesn't return void, it's no longer a valid JavaBean property setter. That might matter if you're one of the seven people in the world using visual "Bean Builder" tools, or one of the 17 using JSP-bean-setProperty elements.

    0 讨论(0)
  • 2020-11-27 10:07

    I agree with all posters claiming this breaks the JavaBeans spec. There are reasons to preserve that, but I also feel that the use of this Builder Pattern (that was alluded to) has its place; as long as it is not used everywhere, it should be acceptable. "It's Place", to me, is where the end point is a call to a "build()" method.

    There are other ways of setting all these things of course, but the advantage here is that it avoids 1) many-parameter public constructors and 2) partially-specified objects. Here, you have the builder collect what's needed and then call its "build()" at the end, which can then ensure that a partially-specified object is not constructed, since that operation can be given less-than-public visibility. The alternative would be "parameter objects", but that IMHO just pushes the problem back one level.

    I dislike many-parameter constructors because they make it more likely that a lot of same-type arguments are passed in, which can make it easier to pass the wrong arguments to parameters. I dislike using lots of setters because the object could be used before it is fully configured. Further, the notion of having default values based on previous choices is better served with a "build()" method.

    In short, I think it is a good practice, if used properly.

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

    At least in theory, it can damage the optimization mechanisms of the JVM by setting false dependencies between calls.

    It is supposed to be syntactic sugar, but in fact can create side effects in the super-intelligent Java 43's virtual machine.

    That's why I vote no, don't use it.

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

    It is better to use other language constructs if available. For example, in Kotlin, you would use with, apply, or let. If using this approach, you won't really need to return an instance from your setter.

    This approach allows your client code to be:

    • Indifferent to the return type
    • Easier to maintain
    • Avoid compiler side effects

    Here are some examples.

    val employee = Employee().apply {
       name = "Jack Sparrow"
       id = 1
       foo = "bacon"
    }
    
    
    val employee = Employee()
    with(employee) {
       name = "Jack Sparrow"
       id = 1
       foo = "bacon"
    }
    
    
    val employee = Employee()
    employee.let {
       it.name = "Jack Sparrow"
       it.id = 1
       it.foo = "bacon"
    }
    
    0 讨论(0)
提交回复
热议问题