Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a switch statement appropriate here, taking an enum?

Today I was making a Tetris clone in Java, and when time came to implement the block spawning mechanism, I wrote this switch statement that takes an enum. I've been told to avoid switch statements when possible, but I'm not sure if avoiding one here is possible unless I completely overhaul my original inheritance based design choice.

To implement the different type of blocks, I decided to make an abstract super class called Block and make a child class for every specific type. I also made an enum that marks a block's type. For instance, a ZBlock extends Block object would have Type.ZBlock enum assigned to its Type field variable.

private void spawnBlock(Type type){
        switch(type){
            case I:
                currentBlock = new IBlock();
                break;
            case L:
                currentBlock = new LBlock();
                break;
            case J:
                currentBlock = new JBlock();
                break;
            case Z:
                currentBlock = new ZBlock();
                break;
            case S:
                currentBlock = new SBlock();
                break;
            case T:
                currentBlock = new TBlock();
                break;
            default:
                currentBlock = new OBlock();
        }
    }

When I starting thinking about ways to not use a switch statement here, I couldn't think of anything other than getting rid of all of my child classes and programming the behaviors of the different blocks in a single non-abstract Block class. But it might become a mess since the different blocks have different shapes and wall kick data. So is the switch statement an ok choice here? If not, how can I improve my code and why?

like image 223
orchid Avatar asked Aug 21 '19 06:08

orchid


People also ask

Can you use a switch statement around an enum?

We can use also use Enum keyword with Switch statement. We can use Enum in Switch case statement in Java like int primitive.

Can enum be checked in switch-case statement?

Can enum be checked in a switch-case statement? Yes. Enum can be checked. As an integer value is used in enum.

Can we use enum in switch Java?

Yes, You can use Enum in Switch case statement in Java like int primitive. If you are familiar with enum int pattern, where integers represent enum values prior to Java 5 then you already knows how to use the Switch case with Enum.

When should you use a switch statement?

Switch statements are cleaner syntax over a complex or stacked series of if else statements. Use switch instead of if when: You are comparing multiple possible conditions of an expression and the expression itself is non-trivial. You have multiple values that may require the same code.


1 Answers

Switch is better than using if-else statements. In my opinion it is much cleaner code. Just compare it with the same code using if-else:

private void spawnBlock(Type type){
    if(Type.I.equals(type)) {
        currentBlock = new IBlock();
    } else if(Type.L.equals(type)) {
        currentBlock = new LBlock();
    } else if(Type.J.equals(type)) {
        currentBlock = new JBlock();
    } else if(Type.Z.equals(type)) {
        currentBlock = new ZBlock();
    } else if(Type.S.equals(type)) {
        currentBlock = new SBlock();
    } else if(Type.T.equals(type)) {
        currentBlock = new TBlock();
    } else {
        currentBlock = new OBlock();
    }
}

But often you have the chance to use a different approach than switch or if-else. Just take a look at the other answers or look at this. It explains how you can improve your code using enum and putting the logic directly into the enum.

like image 112
AndiCover Avatar answered Sep 27 '22 15:09

AndiCover