constructor style?  
Author Message
Duane Evenson





PostPosted: 2006-5-15 22:00:00 Top

java-programmer, constructor style? A quick question about constructor style:
Which do you prefer, independently built constructors or nested
constructors?

eg.
// Eclipse automatically generated style
AClass() {
}
AClass(String str) {
this.str = str;
}
AClass(String str, int num) {
this.str = str;
this.num = num;
}

or
// "Elements of Java Style" recommended style
AClass(String str, int num) {
this.string = string;
this.num = num;
}
AClass(String str) {
this(str, DEFAULT_NUM);
}
AClass() {
this("", DEFAULT_NUM);
}



 
Domagoj Klepac





PostPosted: 2006-5-15 22:06:00 Top

java-programmer >> constructor style? On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
<email***@***.com> wrote:
>A quick question about constructor style:
>Which do you prefer, independently built constructors or nested
>constructors?

It really depends on the amount of logic in the constructors, but, as
a general preference, nested constructors - no code duplication.

Domchi

--
Ouroboros ltd. - http://www.ouroboros.hr
Antispam: to reply, remove extra monkey from reply-to address.
 
Jeffrey Schwab





PostPosted: 2006-5-15 22:13:00 Top

java-programmer >> constructor style? Domagoj Klepac wrote:
> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
> <email***@***.com> wrote:
>> A quick question about constructor style:
>> Which do you prefer, independently built constructors or nested
>> constructors?
>
> It really depends on the amount of logic in the constructors, but, as
> a general preference, nested constructors - no code duplication.

Ditto.
 
 
Tobias Schr鰁r





PostPosted: 2006-5-15 22:21:00 Top

java-programmer >> constructor style? Duane Evenson schrieb:
> A quick question about constructor style:
> Which do you prefer, independently built constructors or nested
> constructors?
>
> eg.
> // Eclipse automatically generated style
> AClass() {
> }
> AClass(String str) {
> this.str = str;
> }
> AClass(String str, int num) {
> this.str = str;
> this.num = num;
> }
>
> or
> // "Elements of Java Style" recommended style
> AClass(String str, int num) {
> this.string = string;
> this.num = num;
> }
> AClass(String str) {
> this(str, DEFAULT_NUM);
> }
> AClass() {
> this("", DEFAULT_NUM);
> }

I'd prefer the latter one. Every constructor finally leads to the "most
flexible" one. If you have to change anything, you have to do it only
once and not - as in this example - thrice.

It's the same with methods: normally you would implement
List#add(Object) as

<code>
public void add(Object obj) {
this.add(this.size(), obj);
}
</code>

and not do the implementation twice for List#add(Object) and
List#add(int, Object), which are technically the same.

Tobi
 
 
Robert Klemme





PostPosted: 2006-5-15 22:26:00 Top

java-programmer >> constructor style? Jeffrey Schwab wrote:
> Domagoj Klepac wrote:
>> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
>> <email***@***.com> wrote:
>>> A quick question about constructor style:
>>> Which do you prefer, independently built constructors or nested
>>> constructors?
>>
>> It really depends on the amount of logic in the constructors, but, as
>> a general preference, nested constructors - no code duplication.
>
> Ditto.

+1

robert
 
 
Duane Evenson





PostPosted: 2006-5-15 22:26:00 Top

java-programmer >> constructor style? On Mon, 15 May 2006 14:12:55 +0000, Jeffrey Schwab wrote:

> Domagoj Klepac wrote:
>> On Mon, 15 May 2006 13:59:59 GMT, Duane Evenson
>> <email***@***.com> wrote:
>>> A quick question about constructor style:
>>> Which do you prefer, independently built constructors or nested
>>> constructors?
>>
>> It really depends on the amount of logic in the constructors, but, as
>> a general preference, nested constructors - no code duplication.
>
> Ditto.

thanks