mpen
mpen

Reputation: 282825

Naming conventions for the same variable as a different type?

Many people believe Hungarian notation is bad. How then do you name a variables that represent the same value casted to different types?

I've got a variable called value, that might be a string, or a decimal. What would you call the different formats? strValue, decValue? valueAsString?

Upvotes: 1

Views: 660

Answers (1)

Chris Baxter
Chris Baxter

Reputation: 16353

I think it would largely depend on the context. For instance if the string value was named age, and the decimal was the parsed value then perhaps parsedAge or something along those lines. Really it comes down to what makes sense given what you are doing and the lifetime of that variable. If it only exists long enough to actually collect and parse the value, then I would give the better name to the parsed variable or worry less about the naming of the intermediary.

If you actually need to hold on to both values, then I might consider creating a struct or some similar data structure that represents the various forms for that data value to prevents the need to shift between string and decimal formats etc.

Upvotes: 3

Related Questions