• Post Reply Bookmark Topic Watch Topic
  • New Topic

String constants  RSS feed

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • 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
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!