Hacker News new | ask | show | jobs
by kevinconaway 2832 days ago
> I honestly cannot think of a reasonable argument for why constant values should not be in static constants or enums.

My personal opinion is that values should be in constant values if:

- Its repeated more than once or referenced outside of the file

- The constant value is a "magic number" where the meaning of the value isn't obvious to outsider

If the value is used only once and the meaning of the value is clear, I think extracting a constant out is needless indirection.

For example:

  if (flags & 45676 == 0) {}
is a magic number that definitely should be a constant, even if its only used once:

  if (flags & ROLE_ADMIN == 0) {}
However something like:

  pool.setDefaultTimeoutMillis(1000);
is perfectly obvious in context IMO
1 comments

I will say your take is reasonable, but on projects I manage I still push for usage of const/enum in these cases.