Felix Cartwright
Felix Cartwright

Reputation: 129

Chaining C# Constants:

At my last firm, I came across what seemed like a really clever way of creating constants for the indices of certain pieces of a delimited string, or columns in a SQL table, but I've long wondered if there's a performance or other flaw I didn't catch.

For example, if there were five data fields in a single delimited string containing address information, you might see the following:

const int INDX_NAME = 0;
const int INDX_ADDR = INDX_NAME + 1;
const int INDX_CITY = INDX_ADDR + 1;
const int INDX_STATE = INDX_CITY + 1;
const int INDX_ZIP = INDX_STATE + 1;

That way, if in the future, the program needed to separate what could be two lines of an address between the name and city (say, street address on one line, suite number on the other), all the programmer would have to do is add the line

const int INDX_ADDR2 = INDX_ADDR + 1;

and change the line creating INDX_CITY to

const int INDX_CITY = INDX_ADDR2 + 1;

In short, this seemed like a really clever way of handling column/piece index lists that could change, especially if the list was 50+ items, and you need to add something at index number 3. However, I wasn't sure how much this could start affecting performance as the list starts getting large, or whether there was some other inherent flaw I wasn't seeing.

Any thoughts? Is there something that makes this lazy programming instead of clever?

Thanks.

-EDIT-

I can see where the value of enums would be given the responses I've been getting, but I'm concerned that they'd only be useful if every field I'm using is exactly 1 spot away from every other. What if I'm using pieces of some data storage that other programmers are also referencing, but I only care about a subset of the total fields, so I need to skip blocks of fields that I don't care about.

For example, I'm using 100 data fields from a delimited string that 300-fields long, but my application doesn't care about fields 1-4 (array counting). Is there a clean way to use enums to replace

const int INDX_RECORD_ID = 0;
const int INDX_DATE_UPDATED = INDX_RECORD_ID + 5;
const int INDX_USER_UPDATED = INDX_DATE_UPDATE + 1;
...

?

Upvotes: 1

Views: 186

Answers (2)

Servy
Servy

Reputation: 203847

You should really use an enum here. They are designed for exactly this type of situation:

public enum MyEnum
{
    NDX_NAME,
    INDX_ADDR,
    INDX_CITY,
    INDX_STATE,
    INDX_ZIP
}

Adding values (to any position) will update all of the indexes accordingly.

Using an enumeration will have several advantages: (not an exhaustive list)

  1. You could have a parameter to a function or a variable typed to the enumeration, rather than an index. This helps make the intent clearer to other coders then knowing that, "This parameter needs to be given one of the constants in this other class."
  2. You don't need to worry about making a mistake where you don't add one to the previous constant. I could easily see that happening when you add in a new item in the middle, or just as a copy/paste error.
  3. You can use other enum tools, such as the ability to get the string value of an enum (as well as the integer value) for easier displaying, you can use reflection to iterate the possible values, you can parse a string or an integer into the appropriate enum value, etc.

As for performance, the values (for both an enum and your constants) will be evaluated at compile time, not runtime, so at runtime it won't have any additional cost over manual numbering.

Upvotes: 5

paulsm4
paulsm4

Reputation: 121881

No, it's not "lazy". I think it's simply good practice. And it incurs no runtime penalty because const fields must be compile-time constants.

Upvotes: 5

Related Questions