• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding conventions

 
Aleksander Zielinski
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm almost finishing coding and checking what I wrote in the mean time. I have a simple question about declaring a reference. Sun says we should create object in the smallest scope needed for the object, that's ok but what about reference? Which one is more appropriate? Pay attention to "String [] myArray" declaration please.


Snippet one:



Snippet two:

 
Mark Smyth
Ranch Hand
Posts: 288
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the location of the the reference declaration that determines the scope of an object not where you create the object. In your example the second option is better as the variable is declared and only in the block of code that it is used in. This makes the code easier to understand.
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

The specs from sun

create object in the smallest scope needed for the object, that's ok but what about reference


is a line to follow not a must. You must decide where it fits and where in does not.
IMHO If the myArray is used in other palaces also then 1 otherwise 2.

Regards M
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic