• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String constants

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At my current job, the (new) rule is to define constants for all used constant strings.
This would mean creating some StringConstants class with constants as emptyString, newline, space, zero, one...
because they are used from time to time (toString for example).

Is there any advantage that I miss besides making the codechecker happy to compensate for the longer and less readable code?
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that I can think of.

I think that "", " " definitely count as legitimate string literals, not needing a symbolic constant. Probably, "0", "1" and "\n" (*) do too.

Can you tweak the codechecker not to whinge about these constants? If not, I suggest ignoring those specific complaints.

(*) Note that you should not use "\n" as meaning the text file line-separator character, as it is not portable to do so. You should use System.getProperty("line.separator"). Because that's rather long-winded, I recommend that you do have a symbolic constant, such as MyStringConstants.NL, for this.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope that I can persuade them to exclude some of those string literals.

Thanks for the reminder on the "\n", I had forgotten about that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic