For two choices there is boolean. In my case, I have positive, negative, and neutral which are three choices and cannot be represented by boolean. I've see there's method Math.signum(x) in Java which returns -1, 0, or 1. So, should I follow this practice (simply use -1, 0, 1 without creating constants) or create constants?

int type = -1;
// vs
int type = Type.NEGATIVE;
  • 2
    Could you use an Enum? – matt freake Mar 2 '18 at 11:22
  • I will prefer to define constants, which improves readability. You can also use Enums. – Vivasaayi Mar 2 '18 at 11:31

Whenever confronted with the possibility of making your code easier to read with no downsides, you should do so.

Java enums offer the possibility to handle specific values with readable names. In terms of efficiency, the use of enums is comparable to using integers. In terms of readability, the representation is clear and unambiguous through use of a label.

enum Option {

Therefore your code, rather than have:

if (opt == 2) {
    // Default option

You can get:

if (opt == Option.DEFAULT) {
    // Default option

In your case, you'd have:

enum SignValue {
  • This answer is good for Java, however, for C# I would always recommend using a constant or a normal class over an enum. – TheCatWhisperer Mar 2 '18 at 13:38
  • 1
    in C# enums are basically procedural constructs, and glorified constants. They lack enough features or power to justify their existence. Sure, they have some justified uses, but they are usually abused. – TheCatWhisperer Mar 2 '18 at 14:09
  • 2
    I disagree with @TheCatWhisperer. C#'s enums are ideal for this scenario as that are just "glorified constants", which is all that's needed here. – David Arno Mar 2 '18 at 14:29
  • 1
    @BornToCode Lets just say the returned value of Math.signum(x) is contextless. If the behavior of your code is determined on the return value of Math.signum(x), you risk looking back after 6 months and wonder why you were checking if a number was equal to -1. – Neil Mar 2 '18 at 15:23
  • 1
    @DavidArno, you seem to be happy writing procedural code, maybe you should switch to C ;) – TheCatWhisperer Mar 2 '18 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.