Re: How to name variables in a program?
- From: Gerry Quinn <gerryq@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 4 Jun 2005 13:09:05 +0100
In article <d7p7ep$rf6$1@xxxxxxxxxxxxxxxxxxx>, ffffh.no.spam@hotmail-
spammers-paradise.com says...
> "Gerry Quinn" <gerryq@xxxxxxxxxxxxxxxxxxx> wrote in message
> > > > Programming is not mathematics.
> >
> > > Let's say that you were dealing with some calculation that was best done
> > > with complex numbers. How would you be nameing sturctures and
> variables -
> > > in the generalised complex plane (e.g. real and imaginary parts), or in
> the
> > > problem domain (e.g. voltage and current).
> > > i.e.
> > > float Re; float Im;
> > > float Voltage; float Current;
> >
> > I don't understand the question. If the calculation is best done with
> > complex numbers, use complex numbers (any reasonable language can use
> > defined types at a minimum), and give them appropriate names.
>
> I mean: suppose you didn't have a complex class at your disposal and you
> wanted to perform a calculation that was typically carried out using complex
> numbers, how would you name your variables, elements, etc.
First, I can't imagine a language so primitive being used for such a
purpose. You don't always have a complex class (or a vector class that
could be knocked into shape) but surely you can have a datatype.
But assuming that we are restricted to primitives, I would expect that
names such as: impCoil_re and impCoil_im might be used. Those don't
look like very good names and maybe I would find better ways to
distinguish them. In a normal language impCoil would be a local
variable referring to the iompedance of a coil.
> For example, calculations involving instantaneous voltage and current can be
> performed using the complex numbers. However, that's just a mathematical
> tool that happens to work for this problem. So, would you use the
> nomenclature of the tool (i.e. real, imaginary) or would you work in the
> problem domain (e.g. voltage and current).
The problem domain is best, even if, as in this case, we are forced to
see things we should not have to see.
> I believe that most people would feel more comfortable working with the
> mathematical symbols (Re, Im), but your comment that programming was not
> mathematics in relation to the naming of variables might suggest that you'd
> prefer to work with voltage and current in this example.
Absolutely.
> > If you mean temporary variables that plug into some formula, sometimes
> > it's hard to pick a name, but if you understand the problem domain you
> > may be able to choose a suitable one. (Re and Im might be suitable
> > choices here IF it made sense not to use a complex class or datatype.)
> In this case, you are woking in the mathematical 'domain' - not the problem
> domain (or programming domain) - so, it pretty much contradicts your
> statement:
> > "There is another issue, which is that in mathematics it is a good thing
> > if the meaning of a term is arbitrary in every respect other than the
> > mathematical structure in which it finds itself. In programming it is
> > quite otherwise - a term generally refers to something specific, and
> > the equation does not speak of anything."
I am not working in the mathematical domain, unless I am creating a
complex class, or functions to work with complex numbers.
Strictly speaking you are indeed emphasising the mathematics if you use
Re, Im, sum, count, total, difference, or similar names. But this is
really only justifiable when you are writing code to do with
mathematics, or when the context makes it apparent what's happening and
the brevity of 'count' trumps the informational aspect of
'customerCount'. It is not a good thing that the name does not reflect
the purpose - that is a bad thing, cancelled because it is a good thing
that the name is short.
> I see that there is the need in particular circumstances to give variables a
> descriptive name, but there are equally valid cases were a short name
> (particularly if it's a typical mathematical use) when a short name is
> better (so that the structure can be seen more easily).
>
> I don't think may people would have variables called:
> RatioOfCircumferenceToDiameter. If text editors, etc. worked with greek
> symbols, then <greek lowercase pi> might have become the norm for this
> constant (or f<greek lowercase pi>).
On the contrary, RatioOfCircumferenceToDiameter would be a good name
(except for being too long-winded) if we were using pi for that
purpose, which would be very rare. Of course it usually represents a
plug in constant or an angle. We are so familiar with trigonometry
that we don't need to call it HalfCircleAngle in the latter meaning,
but if we weren't familiar with it, it would probably be a good idea.
Indeed, if I were doing angle calculations I might well start off with:
const double PI2 = 4.0 * acos( 0.0 );
PI2 is a full circle in radians, and that is why it is interesting.
The only reason I use the name PI2 is because I am so familiar with
trig. It really means 'full circle in radians'. I may want PI as
well, but only because it plugs into trig formulas. For angles it's
usually a messy and inconvenient value.
We would probably use a shorter name than RatioOfCircleToDiameter if
that was what we wanted to think about. For example, if we defined
diameter as the distance between the nearest pair of parallel lines
that can enclose a shape, we might call it cdRatio. Then
cdRatio_Square would be 4.0, cdRatio_Circle would take the value of the
mathematical constant pi, and other shapes would have values ranging
from 2.0 to infinity.
We wouldn't use pi in calculations to do with this concept for various
shapes. We would set cdRatio_Circle to pi, and then use cdRatio_Circle
in our calculations.
- Gerry Quinn
.
- References:
- Re: How to name variables in a program?
- From: Gerry Quinn
- Re: How to name variables in a program?
- From: Scott Moore
- Re: How to name variables in a program?
- From: Gerry Quinn
- Re: How to name variables in a program?
- From: Joe Butler
- Re: How to name variables in a program?
- From: Gerry Quinn
- Re: How to name variables in a program?
- From: Joe Butler
- Re: How to name variables in a program?
- Prev by Date: Paradigms which way to go?
- Next by Date: Re: Does order of associative operators matter?
- Previous by thread: Re: How to name variables in a program?
- Next by thread: Re: How to name variables in a program?
- Index(es):
Relevant Pages
|