views:

395

answers:

8

It obviously depends on the context you are using them in but, I was wondering if there is a universally accepted way to name such variables, or at least in a mathematical context.

I've often seen:

float k         = someValue;
float oneMinusK = 1 - k;

...which seems as descriptive as much as meaningless to me.

Please note that I'm not asking how to name a variable, but how to do it in this very case. Examples and contexts where you used them will be much appreciated,

Thanks.

+9  A: 

You should name your variables based on what it means in terms of the domain you are working on not the algorithm you used to produce it. Thus if k represented your house number k-1 may represent your next door neighbors house number. Name it accordingly.

Scott James
But what if this is an intermediate step in a longer process that doesn't have a suitable domain representation?
Rob Wells
Good point! I guess as long as the scope of the variable is short, and it aids clarity, then naming as oneMinusK is reasonable.
Mitch Wheat
Yes. That was what I was thinking, especially if you want to look at an intermediate point in a large calc. for example. However, as you rightly point out if the variable has representation within the domain then use that rather than some obstruse expression of its numerical properties. (-:
Rob Wells
Disagree. Look for meaning. It has to be there. If it's obscure, it may mean some digging to understand the small-scope intermediate results.
S.Lott
plus, it was 1-k
nickf
If you have an intermediate step in a longer process then the process becomes the domain. If this is the case I would that that --POTENTIALLY-- nMinusOne is a fair name given the domain. It seems clear though that the author intented a probability slant so the answer is moot :-)
Scott James
+14  A: 

In probability 1-k is the probability of X not occurring, given that k is the probability of X occurring.

So

float will_win_lottery = 0.00000000001;
float will_not_win_lottery = 1 - will_win_lottery;
Vinko Vrsalovic
Thanks, I'm looking for answers like this.
Trap
A: 

Does it really matter? Use i; it's not any less descriptive than k. Things like this need to be documented/commented if you're that OCD about code descriptiveness.

orlandu63
well, it matters to me :)
Trap
+7  A: 

I would call it the Complement.

Dan Dyer
I think the complement more accurately describes ( 0 - n ) though.
_ande_turner_
In probability 1 - n is the complement, so I guess it depends on the domain.
Dan Dyer
Ahh. It's nice to learn things. :) lol ... Cheers
_ande_turner_
+2  A: 

I would probably calculate that when I needed it. How much time do you think it saves to store it in a variable? Remember that premature optimization is the root of all evil.

Bill the Lizard
Nice point, but this question occured to me while writing a critical part where speed did really matter.
Trap
I would just go with oneMinusK then, and document that the variable is for an optimization where it's declared. Also, make sure you measure it with and without the variable to make sure it really saves you something.
Bill the Lizard
I might give the value a name regardless of any performance concerns. It depends on whether the code is easier to understand with or without a symbolic name.
Kristopher Johnson
+1 to KJ. Naming a calculation is often for the reader; I wouldn't consider it an optimization
Michael Easter
I think the whole point of the original question is that it's hard to come up with a better name than 1 - k.
Bill the Lizard
A: 

Are these supposed to be constants ?

If you are doing it for legibility reasons exclusively why not create a method a la Dan's suggestion.

float complement(float n) { return (1.0 - n); }
_ande_turner_
A: 

Your usage already seems descriptive enough, just go with it.

Turnkey
+1  A: 

There is no way to answer your question without knowing what "k" represents. Ironicly, the reason why that is not possible is the poor naming of the variable "k" in the first place, so that is what you should worry about instead. If you give "k" a more describing name, a good choise of naming for "k-1" should come naturally, like in the example of "will_win_lottery" and "will_not_win_lottery".

CooPs