Setter conventions in Java (return void or this) -


i have been writing java year now, , have seen 2 different conventions how people implement setters.

to illustrate this, here examples of both conventions. (i love know concise names of these 2 patters)

classes using first convention, return nothing 'set' methods. so:

public class classic{     private double _x;     private double _y;     public classic(){         x = 0;         y = 0;     }     public void setx(double d){//or boolean type check on input         x = d;     }     public void sety(double d){         y = d;     } } 

classes using alternative convention return setter methods. so:

public class alternative{     private double _x;     private double _y;     public alternative(){         x = 0;         y = 0;     }     public alternative setx(double d){         x = d;         return(this);     }     public alternative sety(double d){         y = d;         return(this);     } } 

the difference being alternative approach syntax such as

alternative newalt(double x,double y){      return(new alternative()                 .setx(x)                 .sety(y)); } 

is possible while classic set-up same factory method this.

classic newalt(double x,double y){      classic temp = new classic();      temp.setx(x);      temp.sety(x);      return(temp); } 

it debatable of these more readable/usable.

my question performance difference between these 2 patterns. exist? if how big difference, , arise from?

if there no performance difference, 1 considered 'better practice'?

method chaining may nice in situations, not overuse it. used lot in builder pattern, mentioned in comment. degree it's matter of personal preference.

one disadvantage of method chaining in opinion debugging , breakpoints. may tricky step through code filled chained methods - may depend on ide. find ability debug absolutely crucial, avoid patterns , snippets make life harder when debugging.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -