Why use static "constants" instead of the actual value?

I am looking at the code for the least squares method, and I came across the following few lines:

static double one = 1.0; static double p1 = 0.1; static double p5 = 0.5; ... 

and I was wondering why someone would define static for 1.0 . I can understand, for example, something for pi , but for trivial mathematical values ​​like 1.0 and 0.1 , etc.? I think this will make the code less readable, but it may have other advantages that I am missing.

So, is there a reason for these definitions? or, if it is not used with modern codes, were there any reasons for this with old compilers? I know that the code I'm looking for has been translated into C / C ++ from FORTRAN. Were there reasons for this in FORTRAN?

+8
c ++ c
source share
5 answers

I know that the code I'm looking for has been translated into C / C ++ from FORTRAN. Were there reasons for this in FORTRAN?

FORTRAN uses pass-by-reference for all parameters of the subroutine. However, unlike other languages ​​with passing by reference, it still allows you to pass "rvalues" as parameters. Behind the scenes, the FORTRAN compiler will convert the code, for example:

 CALL SUBFOO(X + Y, 4) 

for type code

 TEMP1 = X + Y TEMP2 = 4 CALL SUBFOO(TEMP1, TEMP2) 

And this is exactly what your C ++ code does: creating variables to pass a pointer (or link) to a subroutine like FORTRAN.

Of course, this is not idiomatic C or C ++. Usually you pass a double value by value.

(Sample codes copied from my answer to this question .)

+6
source share

Using

 static double one = 1.0; static double p1 = 0.1; static double p5 = 0.5; 

doesn't make much sense to me. It would be wise if the variables were named with something more meaningful. I see a value of something like:

 static double defaultCoefficient = 1.0; static double defaultRateOfIncrease = 0.1; static double defaultRatio = 0.5; 

In the second set, the use of variables in the code is much more significant than the use of the corresponding constants.

It would be more appropriate to make them permanent.

 static double const defaultCoefficient = 1.0; static double const defaultRateOfIncrease = 0.1; static double const defaultRatio = 0.5; 

I know that the code I'm looking for has been translated into C / C ++ from FORTRAN.

If the translation was performed by the program, it is understandable why the variables are called as they are.

If the translation was made by a person, they probably followed some recommendations on how to move quickly and not necessarily create meaningful variable names, understanding the code.

+5
source share

In one case, when I saw this being used, some (old, built-in) platforms use Harvard architecture.

Let's say I have a (external library) function that takes a pointer as an argument:

 void Foo(double *Bar); 

If I want to pass a constant to this function, I obviously cannot write it directly. I have to do this as in the first post, so I can take the address of one , for example.

 Foo(&one); 

On some Harvard platforms, const values ​​are placed in ROM or flash memory instead of RAM, so this will not work, because you will take the ROM address instead of the required RAM address.

Then a global (static) variable is created, so it is initialized once in the startup code (the value copied from ROM to RAM once) instead of each time it is necessary, making it an automatic variable.

I use the case that I admit.

+5
source share

No, this constant is stupid. If for any other reason than this is actually not a constant.

+3
source share

There is actually a good reason for this, and it includes the software development process itself.

Let's say you have a translation unit that calls some function with some numerical parameter, of course, you will make it a constant:

 static const double epsilon = 1.e-3; 

Nice, clean code. But now you understand that the epsilon that you installed is not so good, and you need the best. You really don't have the right way to determine what it should be, so you go for the trial version and the error:

 static const double epsilon = 1.e-4; 

You are rebuilding your program. And this is still not good. If you change it again, you will have to wait for the build to complete, and this may take some time on some non-trivial projects. What to do?

Well, debuggers allow you to change the value of variables if they are in memory (and are not excluded, like true constants). So we do the following:

 static double epsilon = 1.e-4; 

Now we set a breakpoint somewhere in this file. And we can change epsilon without having to rebuild our program every time. We end up saving valuable development time. And we find the right meaning in timelessness.

Will we leave this not const ? Not. This is a constant, so we mark it as const before checking our code. Leaving it non-constant is the smell of code. There is no purpose for this.

+1
source share

All Articles