2

What is the better way to handle this piece of code

I have a method as shown below , which will accept a parameter i String and returns a int value

below code works fine .

public static int getLoggerLevel(String level)
 {
        int loglevel = 3;
        if (level.equals("INFO")) {
            loglevel = 3;
        }
        else if (level.equals("ERROR")) {
            loglevel = 4;
        } else if (level.equals("FATAL")) {
            loglevel = 5;
        }

        return loglevel;

}

I thought of putting the Key Values in Map , and retrieve that based on the String , but dont want to create an Map i guess which will consume memory

3
  • 5
    Are you really, really, really strapped for memory? A map with three values isn't going to take much memory...
    – Jon Skeet
    Jun 22, 2013 at 19:18
  • 2
    Is the environment really that memory constrained that you can't use Java's basic structures? If so, is there a benefit in even using Java and not going for a language that produces more memory compact types, like C? Jun 22, 2013 at 19:19
  • Any update on your progress? Aug 1, 2014 at 20:46

3 Answers 3

8

Assuming Java 7:

public static int getLoggerLevel(String level)
{
        switch(level){
            case "ERROR": return 4;
            case "FATAL": return 5;
            case "INFO": 
            default: return 3;
        }
}

On a more general note, you should probably use an enum instead of a string for this sort of thing. It's a perfect fit. Moreover, it will also work in Java 6.


Here is an alternative solution using enums:

public enum SeverityLevel {
    ERROR, FATAL, INFO
}

 public static int getLoggerLevel(SeverityLevel level)
 {
     switch(level){
          case ERROR: return 4; 
          case FATAL: return 5; 
          case INFO: 
          default: return 3;
     }
 }

No quotes around them, they are enum values, this approach also mitigates bugs caused by typing errors. The big bonus is conceptual though, getLoggerLevel now accepts a SeverityLevel and not a string.

14
  • Java 6 compatibility is overrated, since it's end-of-lifed. ;-) (Now, if you're coding for Android, that's a different story.) Jun 22, 2013 at 19:19
  • 2
    @ChrisJester-Young Completely Right. I think that Java 6 compatibility is not the stronger argument towards using an ENUM (rather than strings) here. Enums are designed for these sort of things. Conceptually logger levels aren't strings, they're logger levels :) (Also, what Brian said) Jun 22, 2013 at 19:20
  • 1
    @ChrisJester-Young You don't work in the real world, do you? ;) There are companies out there that still use 1.5 (Or lower ::cringe:: ) Jun 22, 2013 at 19:20
  • @BrianRoach I realise that (at work, we're only just now completing our switch to Java 7). But I take the same stance as Guava, which is "we are not going to support people using 1.5 or lower". ;-) (Okay, Guava actually does have a special 1.5-compatible branch. But it's still a second-class citizen. :-P) Jun 22, 2013 at 19:23
  • 1
    It's worth noting that switch statements for strings and enums will throw exceptions on null values. Jun 22, 2013 at 19:28
0

A map would work and would hardly consume any memory, especially if scoped appropriately so that it was just created once.

0

Use Switch- case simple to read and understand.

public static int getLoggerLevel(String level)
{
    switch(level){
        case "ERROR": return 4;
        case "FATAL": return 5;
        case "INFO": 
        default: return 3;
    }
}

Also, in your code , you can avoid 1st if block. And once you found correct match.. use return there which will avoid checking further code. which will make your code as below

public static int getLoggerLevel(String level)
{
    int loglevel = 3;
    if (level.equals("ERROR")) {
        return 4;
    } else if (level.equals("FATAL")) {
        return 5;
    }
    return loglevel;

}

1
  • Not me, but probably because this answer was posted later and provided no insight over the already existing ones. Jun 23, 2013 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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