Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initialising objects and Strings in local methods

 
Mark O' Sullivan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Just wondering what the generally accepted method for initializing objects and Strings are in local methods? I usually initialise to null, but have found issues doing this for the String object, problems obviously concatating, etc. Just wondering what's the generally accepted practice is? Thanks.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark O' Sullivan wrote:Just wondering what's the generally accepted practice is? Thanks.


The best way is to declare a variable as close as possible to where it's actually going to be used. Then its obvious what value you should initially assign to it. It's determined by the algorithm you're implementing.
 
Campbell Ritchie
Sheriff
Posts: 48645
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will rarely need null like that. Only for things like Readers and (SQL) Connectors which ought to be closed in a finally block. They often have to be initialised to null immediately before the first try.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic