Enum - Coding Guidelines

Embed Size (px)

Citation preview

Tech Talk: Coding Guidelines - enum

Agenda

Definition / Motivation

Syntax in:C

C#

Python

Java

Examples: good and not-so-good

Summary and guidelines

Define Enum

So.. what's an enum?

Define Enum

A set of constants

Rarely changes, if at all

Motivation

Avoid "magic numbers":
int color = 3;
Color color = Color.GREEN;

Motivation

Code Safety:

Compilation passes:
Color color = Color.GREEN; // 3

Compilation fails:
Color color = Shape.SQUARE; // 3

Motivation

Readability:openFile1(true, true)

openFile2(
Permission.WRITE,
SeekMode.FORWARD_ONLY)


Examples: Good? Bad?

Spade, Heart, Diamond, Club

January, February, , December

Mercury, Venus, , Pluto

Supported formats:
JPG, GIF, , PNG

Sunday, Monday, , Saturday

[web]

Syntax - C

// without typedefenum strategy { RANDOM, IMMEDIATE, SEARCH };enum strategy my_strategy = IMMEDIATE;

Syntax - C#

public enum Importance { None, Trivial, Regular, Important, Critical};

Syntax - Python

class Importance(Enum): none = 0 trivial = 1 regular = 2 important = 3 critical = 4

Syntax - Java

public enum Direction { NORTH, EAST, SOUTH, WEST}

Enum in Java

Enum in Java is not only a constant

Enum in Java is a single instance of a class, shared by all consumers

The class, as any class, can have:Additional members

Functions behavior (!)

Enum in Java ex. 1

public enum Direction { NORTH(0), EAST(90), SOUTH(180), WEST(270);

private int angle;

public int getAngle() { return this.angle; }

Direction(int angle) { this.angle = angle; }}

Enum in Java ex. 1

Additional data is bound to each enum member

Reverse lookup can be implemented within the class itself:
public static Direction getByDeg(int d)But.. consider the outcome of an invalid input (e.g. getByDeg(30)):

Throw an exception?

Add an artificial enum member such as UNKNOWN?

Enum in Java ex. 2

public enum ArithmeticOp { ADD { @Override public Double operate(Double a, Double b) { return a + b; } },

MULTIPLY { @Override public Double operate(Double a, Double b) { return a * b; } };

public abstract Double operate(Double x, Double y);}

Enum in Java ex. 2

// in main...

ArithmeticOp.ADD.operate(2.0, 3.0); // 5.0

ArithmeticOp.MULTIPLY.operate(2.0, 3.0);// 6.0

Enum in Java ex. 2

Polymorphism is implemented in enum

Enum class is now responsible to:Data

Implementation

Polymorphism is better implemented using a factory and/or an IoC container

Special Uses of Enum

SingletonSpecifically in Java

Nice for PoC

In real code, use an IoC

Bitwise flags

Bitwise Enum

What:

Enum as a set of Boolean flags

Usually up to 32/64 different flags

Bitwise Enum

How, in C#:

public enum MyColors{ Yellow = 1, Green = 2, Red = 4, Blue = 8}

if (myProps.AllowedColors.HasFlag(MyColor.Yellow)){ // Yellow has been set...}

Bitwise Enum

Why:Reduce memory usage

Trade-off:Increase code complexity

Is it justified?Rarely

Bitwise Enum

Should we use it?

No

Enum When to use

A set of constants,

Related to each other

Small set

Rarely changes, if at all

Selecting one of the enum members means not selecting any other member

Enum When not to use

Not related to each otherAsk:Should I choose one, and only one value, in every code scenario?

Do I use the enum just for convenience of code completion (IDE)?

Enum When not to use

Large set, say > 10 members

Enum When not to use

Occasionally changes:Ask: will I add new members to this enum in the next 1-2 years?

This means that we enum data.

DON'T enum data

Enum When not to use

Flags. Don't use bitwise flags

Summary

From now on...

Enum should be used properly:A small, (almost) never-changing set of constants, related to each other

Other uses might be wrong:Consider a design meeting

Thank you :)