Avoid Default Clause in Switch Case for Enum

Do not define a default clause for enum in switch statement! Say in future, if another fruit type is added to the enum, default clause shall cause the C++ compiler not to warn you that you have not handled the case for the new fruit.

enum class Fruit

Fruit fruit = ...;
    case Fruit::Apple: ... 
    case Fruit::Orange: ... 
    default: ... // <-- Do not ever define a default clause for enum!

As a point of interest, first item under Bad Coding Practices is CWE-478: Missing Default Case in Switch Statement advises programmers to put default clause in their switch statement.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: