• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding etiquette

 
Roger Wolfenden
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 20
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic