language agnostic - Are hard-coded STRINGS ever acceptable? -


Similarly, but I'm particularly thinking of "magic strings" here.

Project on a large, we have a table of such configuration options:

  name value --------- FOO_ENABLED Y BAR_ENABLED N ..  

(hundreds of them).

Generally, to test an option like this, call a generic function:

  if (config_options.value ('FOO_ENABLED') == ' Y ') ...  

(Of course, this same option may need to be checked in many places in the system code.)

A new option , I was thinking of adding a function to hide "magic string" like this:

  if (config_options.foo_enabled ()) ...  
<

However, colleagues thought that I Zadah fell into the water and has objection to doing this, because it likes hard-coding:

  • That's what we normally do
  • This makes it easy Makes it to see what's going on when debugging the code

The problem is, I can see their point! In fact, we are never going to change the name of the option for any reason, so I can think of my work for only one benefit that the compiler will catch any type of font as fawn_nanted () But not 'FO_ENABLED'.

What do you think? Do not I have any other benefits / disadvantages?

"post-text" itemprop =)) {...}

Restrict your hard-coded y probe to a place, even if it's a wrapper class for your map What is the meaning of writing?

  If you have 100 configuration options and 100 methods (config_options.isFooEnabled ()) {...}  

OK (Here you can decide on future application development and requirements before deciding on your implementation). Otherwise, it is better to have a class of static wires for parameter names.

  If (config_options.isTrue (ConfigKeys.FOO_ENABLED)) {...}  

Comments