• Post Reply Bookmark Topic Watch Topic
  • New Topic

What are the benefits of this way ?  RSS feed

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.
usually, when I want to create some object, I write :
Date d = new Date( );
but recently, I heared its should split the decleration and instatiation into 2 lines like :
Date d;
d = new Date( );
what are the benefits of this way ?
thanks.
 
Nigel Browne
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cant remember ever seeing a guideline on coding style, recommend that class declaration and initialisation should be spread over two lines. How did this recommendation come about ? Who told you that this was the best way to program ?
A quick look at any of the code samples within the JavaTutorial will show that they always use the style and if it's good enough for the people at Sun then I'll use their style.
 
Peter Laurinec
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also dont see any reason for having declaration and initialization in different lines.
Using
its just fine
 
Daniel Mayer
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words: the suggestion to split declaration and initialization is rubbish... :roll:
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mostly agreed: if you're declaring and instantiating together for general cases, do it on one line.

Where you may want to split it is when declaring a member variable and then instantiating it in the Constructor, perhaps choosing different subclasses of the declared type based on parameters. I've seen plenty of code like the following:I chalk this up to extreme laziness and poor programming habits, though the overall effect on performance is hardly staggering for normal usage.

The other case that I use quite frequently when doing JDBC is when you need access to the local variable from both the try and catch blocks, but you need to instantiate it in the try block:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that instantiation at the point of declaration (i.e., on the same line) is actually preferable, because it mitigates the risk of accessing an uninitialized variable. I don't see any real benefit to separating these lines.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I heared ...


You will hear a lot of things in this littered industry.
You are advised to take such things with a "grain of salt", meaning, don't accept them merely because "the bloke sitting next to you" told you so.

In this particular circumstance, what you have heard is nothing but rubbish possibly based on misinformation - take it upon yourself to prevent the further spread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!