Is making an empty string constant worth it?

前端 未结 18 698
無奈伤痛
無奈伤痛 2021-01-01 10:11

I have a co-worker that swears by

//in a singleton \"Constants\" class
public static final String EMPTY_STRING = \"\";

in a constants class

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 10:36

    I'm with your coworker. While the empty string is hard to mistype, you can accidentally put a space in there and it may be difficult to notice when scanning the code. More to the point it is a good practice to do this with all of your string constants that get used in more than one place -- although, I tend to do this at the class level rather than as global constants.

    FWIW, C# has a static property string.Empty for just this purpose and I find that it improves the readability of the code immensely.

提交回复
热议问题