Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Style of coding

Reply
Thread Tools

Style of coding

 
 
howachen@gmail.com
Guest
Posts: n/a
 
      07-04-2006
Consider the following styles, which one is preferred....

1.
public class Person {

private String name;

public void setName(String n) {
this.name = n;
}
}

2.
public class Person {

private String name;

public void setName(String n) {
name = n;
}
}


and...if in case of static ....

3.
public class Person {

private static String name;

public static void setName(String n) {
Person.name = n;
}
}

4.
public class Person {

private static String name;

public static void setName(String n) {
name = n;
}
}


1 or 2

3 or 4 ?

 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      07-04-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>Consider the following styles, which one is preferred....
>public class Person {
> private String name;


public class Person
{ private java.lang.String name;
public void setName( final java.lang.String name )
{ this.name = name; }}
 
Reply With Quote
 
 
 
 
howachen@gmail.com
Guest
Posts: n/a
 
      07-04-2006

Stefan Ram 寫道:

> (E-Mail Removed) writes:
> >Consider the following styles, which one is preferred....
> >public class Person {
> > private String name;

>
> public class Person
> { private java.lang.String name;
> public void setName( final java.lang.String name )
> { this.name = name; }}


hi again

it is quite rare to see people to use the "final" keyword, what is the
exact advantage of doing so?

thanks again!

 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      07-04-2006
(E-Mail Removed) wrote:
> Consider the following styles, which one is preferred....
>
> 1.
> public class Person {
>
> private String name;
>
> public void setName(String n) {
> this.name = n;
> }
> }
>
> 2.
> public class Person {
>
> private String name;
>
> public void setName(String n) {
> name = n;
> }
> }
>
>
> and...if in case of static ....
>
> 3.
> public class Person {
>
> private static String name;
>
> public static void setName(String n) {
> Person.name = n;
> }
> }
>
> 4.
> public class Person {
>
> private static String name;
>
> public static void setName(String n) {
> name = n;
> }
> }
>
>
> 1 or 2
>
> 3 or 4 ?
>

Personally I prefer (opening Bracket on next line makes nested logic
easy to read):

public class Person
{
private String name;

public void setName(String name)
{
this.name = name;
}
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-04-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
>
> it is quite rare to see people to use the "final" keyword, what is the
> exact advantage of doing so?


It indicates that you don't intend for the value to change. This can
help you catch, at compile time, code which erroneously changes the value of
those variables.

Also, anonymous inner classes can only access surrounding local
variables if they are final.

- Oliver

 
Reply With Quote
 
IchBin
Guest
Posts: n/a
 
      07-04-2006
(E-Mail Removed) wrote:
> Consider the following styles, which one is preferred....
>
> 1.
> public class Person {
>
> private String name;
>
> public void setName(String n) {
> this.name = n;
> }
> }
>
> 2.
> public class Person {
>
> private String name;
>
> public void setName(String n) {
> name = n;
> }
> }
>
>
> and...if in case of static ....
>
> 3.
> public class Person {
>
> private static String name;
>
> public static void setName(String n) {
> Person.name = n;
> }
> }
>
> 4.
> public class Person {
>
> private static String name;
>
> public static void setName(String n) {
> name = n;
> }
> }
>
>
> 1 or 2
>
> 3 or 4 ?
>



Personally I prefer (opening Bracket on next line makes nested logic
easy to read):

public class Person
{
private String name;

public void setName(String name)
{
this.name = name;
}
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)



--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      07-04-2006
(E-Mail Removed) writes:
>>public void setName( final java.lang.String name )

>it is quite rare to see people to use the "final" keyword, what is the
>exact advantage of doing so?


It is intended to be somewhat similar to the in in Ada:

procedure Average(A, B : in Integer; Result : out Integer);

(That was Ada, not Java.)

It expresses my intention only to read from the parameter and
not to change the binding of the name name - though, of
course, the semantics of the final in Java are not the same
as those of in in Ada or const in C.

If I would inadvertently change the in-Parameter, the compiler
could issue a warning. But it also helps human readers to
grasp my intention never to change the binding of the
name name within this method declaration.

 
Reply With Quote
 
howachen@gmail.com
Guest
Posts: n/a
 
      07-04-2006

Oliver Wong 寫道:

> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...
> >
> > it is quite rare to see people to use the "final" keyword, what is the
> > exact advantage of doing so?

>
> It indicates that you don't intend for the value to change. This can
> help you catch, at compile time, code which erroneously changes the valueof
> those variables.
>
> Also, anonymous inner classes can only access surrounding local
> variables if they are final.
>
> - Oliver


thanks...then using "final" is really a good practice that every java
programmer should know....

btw, any comment on the style of static class as above?

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      07-04-2006
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>
> btw, any comment on the style of static class as above?


Not really. I usually prefer to fully qualify names (which means using
"this." or the classname when appropriate), but some of my coworkers prefer
not to. It looks like I'm not bothered by really long identifier names at
all, while they are.

- Oliver

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      07-04-2006
"Oliver Wong" <(E-Mail Removed)> writes:
> Not really. I usually prefer to fully qualify names (which means using
>"this." or the classname when appropriate), but some of my coworkers prefer


I am extreme in this regard. I always prefer the full path and
never use import. So I write java.lang.System.out.println.

I also like descriptive names.
Recently I modified my Java-preprocessor: Now, I can write:

boolean 'can be extended by'( final int cp );

My preprocessor will convert this to

boolean canBeExtendedBy( final int cp );

(It usually will leave character literals untouched.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
Coding style for CPLD vs FPGA Analog Guy VHDL 3 03-14-2005 08:26 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Coding style to prioritize certain inputs Willem Oosthuizen VHDL 5 09-04-2003 05:17 PM
style for coding latches Paul Baxter VHDL 7 08-15-2003 06:19 PM



Advertisments