user3294236
user3294236

Reputation: 400

Variable name percent from 0 to 1

So this seems like I should have been wondering about this when I first started programming, but I suppose back then I wasn't as concerned with 'perfect' variable naming.

So I have the variables

float lifeTime; float age;

Where lifeTime is the full lifeTime of my object, age the current lifeTime of the object. The object will die when age reaches the full lifeTime.

I was just creating a variable, ranging from 0 to 1, defining the progress of age compared to the full lifeTime. agePercent, if you will. Of course agePercent isn't correct though, as this ranges from 0 to 1.

After reading some other posts about ageFraction, ageGradient, ageNormalized, I felt like none of these fit the purpose.

My solution is agePerunum, simply using latin (I believe it's correct, but I dropped out in highschool). So my question is... is that cool? Does that make sense to you as a programmer? Any thoughts or maybe better ideas?

http://mymemory.translated.net/en/Latin/English/per-unum

Upvotes: 22

Views: 6629

Answers (5)

Nicolay77
Nicolay77

Reputation: 2154

In the same way as bool or boolean can be used to represent numbers that are either zero or one, we can use Zadeh-numbers or fuzzy-values to represent numbers in the continuous interval [0-1].

Upvotes: 0

orad
orad

Reputation: 16074

I have come up with this convention that I follow in my code:

  • If the variable/parameter is in range [0 , 1] use Percent: eg. OwnershipPercent
  • If the variable/parameter is in range [0 , 100] use Percent100: eg. OwnershipPercent100

Upvotes: 1

user3294236
user3294236

Reputation: 400

Actually I really liked proportion but eventually went with ageRatio - somehow that reads better ;) thanks all!

Upvotes: 5

at.
at.

Reputation: 52560

One idea is to make agePercent equal to an actual percentage, so 37 instead of 0.37. But that might make other calculations in your code require an unnecessary conversion step.

Proportion and portion are nouns you could suffix age with. Proportion means the relation of one part to another or to the whole with respect to magnitude, quantity, or degree. Portion means an often limited part of a whole. So ageProportion or agePortion? First one sounds better.

Upvotes: 5

Phylyp
Phylyp

Reputation: 1689

Interesting question, although it is likely there is no correct answer and the answers will be opinion-based.

I understand why you feel using 'percent' might not be correct, but I'd have to say that 'per unum' is going to be harder to parse for someone reading the code, at least on the first reading. And even then, it is likely to end up being an attempt at being fancy, rather than being accurate or clear.

I'd say that 'percent' is actually fine, if you can draw a distinction between how the number is presented vs. how it is stored and calculated. And to this, I would look at Excel - it presents a value like 45% as 45% (or whatever formatting is applied), but internally it still stores the value as a decimal number ranging from 0 - 1, so 45% would be .45.

The decision will also be influenced by how you intend to use the progress of age. Are you displaying it as a percentage value? If so, naming the variable percentage is fine. Or, are you using it as a biasing value or ratio for some other calculation?

In any case, a documentation comment alongside the variable will help clarify things, and modern editors display documentation comments via tooltips.

Upvotes: 0

Related Questions