5

I dont know why but my team lead urges everyone to use empty string as a constant string variable from our utility class instead of just putting "" in our code... Its really cringe worthy... Why use Constants.NONE when you can put "" just to avoid null exception working with Strings..

Comments
  • 0
    It is a way to document magic strings (similar to magic numbers). If you decide to change that to something across your module or project (though it seldom happens), it is easier to do.

    By the way, I'd prefer Constants.EMPTY_STRING
  • 0
    @asgs yes it makes sense for a particular case..like an empty message for a particular case... But for everything.. i think its a waste of time...
  • 0
    Depending on the language, you save memory and garbage collector work because "" creates a new string.

    Depending on the app, this can save tens of thousands of strings.

    You'll get the best response from the guy if you just ask him why.
  • 0
    @ravijojila such a micro-optimisation, can't see a real-life situation where that would have any impact

    Yeah, definitely ask your team lead. Worst case scenario, you'll learn something. My guess is that it's just to follow a general good practice of avoiding magic string, it's not strictly necessary for empty strings but might as well apply the same principle everywhere to prevent sloppiness
Add Comment