Are hard-coded STRINGS ever acceptable?

后端 未结 9 966
臣服心动
臣服心动 2021-01-07 04:21

Similar to Is hard-coding literals ever acceptable?, but I\'m specifically thinking of \"magic strings\" here.

On a large project, we have a table of configuration o

9条回答
  •  醉梦人生
    2021-01-07 05:03

    if (config_options.isTrue('FOO_ENABLED')) {...
    }
    

    Restrict your hard coded Y check to one place, even if it means writing a wrapper class for your Map.

    if (config_options.isFooEnabled()) {...
    }
    

    Might seem okay until you have 100 configuration options and 100 methods (so here you can make a judgement about future application growth and needs before deciding on your implementation). Otherwise it is better to have a class of static strings for parameter names.

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

提交回复
热议问题