Reasons to Use Enums:
- Easier to Debug / more likely to give errors.
- Can look up available properties.
- Property values changes.
Lets break each of these down:
The following does not error, it returns undefined:
({}).SOMEPROPERTY //-> undefined
So something like:
topic.layout(X5.primitives.TopicGrowthDirections.TREEE)
would not error, and instead likely result in a read of the layout.
Contrast this to:
topic.layout("tree")
Where layout
looked like:
{
layout : function(newVal){
if(newVal === undefined){
// getter
} else {
var enumVal = X5.primitives.TopicGrowthDirections[newVal]
if(!enumVal) { throw "WRONG VAL" }
// continue setter
}
}
}
This will throw "WRONG VAL" if an incorrect propertyName
of the enum is provided.
You can still look at X5.primitives.TopicGrowthDirections for values one can pass into layout. There is no difference between using layout()
to do enum lookup or passing the enum value to layout.
Enums are typically used to allow changes to the "value" part of the enum. For TREE
, it allows you to change "layout_tree"
in one place.
Both using enums inside and outside layout()
allow "layout_tree"
to change just as easily.
However, if TREE
changes, there is a difference.
Searching for X5.primitives.TopicGrowthDirections.TREE
would undoubtedly give you fewer results than TREE
or tree
. However, I'd argue that changing enum property names is very uncommon and unlikely. I've never needed to do this.
The reason to do this:
- By providing "tree" you will actually get better warnings.
- You have to write less code.
IMO, these reasons win out against the unlikely event that "TREE" needs to change to "WEB" or something (especially given the mature nature of mindjet's feature set).