File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Coding etiquette Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding etiquette" Watch "Coding etiquette" New topic
Author

Coding etiquette

Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
OK, I've been following some Java tutorials which all work so this may be a Q about style or etiquette or good programming practice so your input is appreciated.

The tutorials regularly declare variables as having null values before they get used such as String name=""; I thought I'd streamline this as shown in the code example below by declaring the variable at the point of its use such as... String name=SomeCalculatedResult;

Do any of you have any views on my comments in the Code's footnotes.
My thanks.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Roger Wolfenden wrote:
The tutorials regularly declare variables as having null values before they get used such as String name="";


"" isn't a null value. It's the empty String. Very different from null.

There are a couple different scenarios:

1. Initializing member variables to their default values:

This is pointless, and can lead to unexpected behavior depending on how you write your constructors.


2. Initializing member variables to other than their default values:

This makes sense when you are able to declare and initialize in one shot, without needing extra work first, and if you're going to use the same value regardless fo which constructor gets invoked.


3. Initializing local variables at declaration.

Don't do this unless that value actually means something worthwhile in your method, and you might actually use that value, such as initializing a counter to 0, or initializing a success boolean to false, etc. Don't do it blindly just to make the compiler happy. That's a good way to turn an easy-to-spot compile-time error into a hard-to-debug runtime problem. For instance, in this case, I forgot the case where x = 0 (which could have been handled with a plain old "else"). If I hadn't initialized result, the compiler would have complained that it might not be set when I try to return it. The compiler error would have made me look more closely at my code, and I'd have found my logic error. Now, though, I'll sometimes have my method returning 1 and I won't know why.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

I'd say declare your variables as close as possible to where you use them and avoid initializing them to a default value only to reassign them immediately after.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hi Roger,

Welcome to CodeRanch!

For the starters, null is different than empty string. That given,
is different thanNow, coming to tutorial, IMHO, there are two things which can be done:
orPersonally I prefer the second approach. Also, please bear in mind that sayingmeans you are creating a string of zero length, and when you assign new value to str, you are making that original zero length string eligible for garbage collection, which, in my opinion, is not a good practice.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

Agree with Jelle - I prefer what you're doing now. Try to avoid assigning values (null, empty, or otherwise) that you're never going to use as it's just misleading.
Roger Wolfenden
Greenhorn

Joined: Oct 08, 2012
Posts: 20
Well thanks all, much appreciated. A couple of things...
  • Sorry about my mis-use of the word null, I sort of knew it was different to null but was lazy in my description (now suitably chastened).
  • Perhaps my tutorials are using these String variable=""; declarations to make it easier for newbies like me to understand.
  • Pleased to get the clarification though. It seems like streamlining the code is the way to go.


  • Thanks again.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Roger Wolfenden wrote:
  • Perhaps my tutorials are using these String variable=""; declarations to make it easier for newbies like me to understand.



  • For this particular one, for a member variable I would only use it if I wanted the value to never be null, and for a local variable, I'd probably only use it in the following situations:

    1. I'm building up a String from pieces, and I need to start with an empty String and add to it as I go.

    2. My method returns a String, and which String to return is determined by some complex logic, and the default return value if none of the logic sets it to something else is the empty String.

    However, in #1 I would usually use a StringBuilder (though if it's guaranteed to be no more than a few smallish pieces, I might be lazy and take this route), and #2 I can't imagine arising in real code.

    Roger Wolfenden
    Greenhorn

    Joined: Oct 08, 2012
    Posts: 20
    Jeff, fully understand the logic of needing to set an empty string for your #1 example. I've not yet come across StringBuilder but if it does what its name suggest, I can also appreciate the logic of that too.

    One or two of you mention Garbage collection which my tutorials have briefly touched on but not extensively so I'm guessing that its all about making sure that stuff isn't allowed to 'stay alive' past its intended use which makes good sense. Anyway, you've given me something that I ought to explore further.

    Thanks again.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Coding etiquette