Is making an empty string constant worth it?

前端 未结 18 865
無奈伤痛
無奈伤痛 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:53

    David Arno states: -

    Ironically the whole point of constants is to make them easily changeable

    This is simply not true. The whole point of constants is reuse of the same value and for greater readability.

    It is very rare that constant values are changed (hence the name). It is more often that configuration values are changed, but persisted as data somewhere (like a config file or registry entry)

    Since early programming, constants have been used to turn things like cryptic hex values such as 0xff6d8da412 into something humanly readable without ever intending to change the values.

    const int MODE_READ       = 0x000000FF;
    const int MODE_EXECUTE    = 0x00FF0000;
    const int MODE_WRITE      = 0x0000FF00;
    const int MODE_READ_WRITE = 0x0000FFFF;
    

提交回复
热议问题