Question

I want to eliminate usage of magic strings in these:

BindingUtils.bindProperty(obj1, "propertyName", obj2, ["childObj", "anotherProperty"]);

or

var ddl:DropDownList = new DropDownList();
ddl.labelField = "propertyName";

it would be sweet to just type something like:

ddl.labelField = GetPropertyName(ComplexType.propertyName);

It would allow easy refactoring and would eliminate runtime errors when property name changes.

Any ideas?

Was it helpful?

Solution

Not sure whether I understand your problem correctly. You can easily define static constants in a separate class to eliminate all magic string.

// In class ConstantContainer

public static const PROPERTY_NAME:String = "propertyName";

// In anywhere else
ddl.labelField = ConstantContainer.PROPERTY_NAME;

OTHER TIPS

'magic strings' are needed. Remember that this is a dynamic language that has pros and cons to everything. This is one of those cons.

There are a few things you can do to limit error like static properties.

The Stack Overflow discussion on the similar topic with some ideas that can be of interest to you:

Use of object vs string vs enum

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top