Declaring variables a lengthy way..?

I keep asking myself lately and its bugging me, out of curiosity why do people declare variables like this…

int level_width = current_level / 4 * 384 + 640;

When it could be:

int level_width = current_level / 2176;

?

Is it to save using a calculator? Sorry if it sounds stupid, I have been pretty anxious to ask on here…

Generally it’s essentially ‘documentation’ to explain where a value comes from rather than just having a ‘magic number’ in the code.

I can’t tell from that limited context what that particular example is doing but I’d guess it’s something to do with screen resolution or something like that - explicitly calculating the result makes it easier to see why that particular value is used. It’s also somewhat easier to maintain if (for example) you change the resolution.

A better approach would be to use a constant and calculate it only once, e.g. something like this:


private static final LEVEL_WIDTH_MODIFIER = 1 / 4 * 384 + 640;

This also has the benefit of slightly better self-documenting code.

If you were really anal you could add more constants for the terms in the calculation.

  • stride

Because the order of operations will output a different number for each.
According to the order of operations and assigning current_level to 400, the first problem will output

39040

since

400/4 = 100

then

100*384 = 38400

and

 38400+640 =39040

but the second problem will output

.1838

since

400 / 2176 = .1838

. However, if you wrapped the second part of the first problem in parenthesis, the output will be .1838 because it will calculate everything in the parenthesis before dividing it by currentlevel

usually, if the result would be the same, it’s about clarity or ease of tweaking. one would expect a comment explaining the context of the numbers.

if the calculation is simple it’s also very likely compiler will optimize it for us, so speed is not an issue. usually speed doesn’t matter for the unoptimized case neither.

we had a similar discussion which went about using bitshifts [icode]x << 2[/icode] over “regular” operators [icode]x * 4[/icode].

[quote=“StrideColossus,post:2,topic:52279”]
it can be a tool to express details of the context the numbers are in.

Yep, constant folding is one of the few opts javac actually does… that 4 * 384 + 640 will show up in the bytecode as 2176

Like others have said, one reason is to make your code easier to read. Here is a concrete example:

Say I need to calculate the number of seconds in a year. I could do that ahead of time and just have:

int x = 31536000;

But since I’m bad at naming variables and writing comments, people reading my code have no idea what that variable means.

And now if I need that to be 2 years, or 10 years, I have to do that calculation all over again.

Instead, I could do this:

int x = 365 * 24 * 60 * 60;

Now it’s easier for people reading my code to see what that variable represents, and if they want to change it to 2 years, they can:

int x = 2 * 365 * 24 * 60 * 60;

This is a bit of a contrived example, but it gets at the “why” of your question.

As StrideColossus said, that kind of thing is even clearer if you make it explicit what the values mean. E.g.

final int SECONDS_PER_MINUTE = 60;
final int MINUTES_PER_HOUR = 60;
final int HOURS_PER_DAY = 24;
final int DAYS_PER_YEAR = 365;

int secondsPerYear = DAYS_PER_YEAR * HOURS_PER_DAY * MINUTES_PER_HOUR * SECONDS_PER_MINUTE 

Obviously none of those will change so it’d be overkill but usually the constants are more obscure things you might want to tweak separately, like

final int MEMORY_TARGET_PERCENT = 80;
final int FOOS_PER_BAR = 6;
final int PERFORMANCE_RESERVE = 42;

Pods[] buffer = new Pods[Math.PI * MEMORY_TARGET_PERCENT / FOOS_PER_BAR + PERFORMANCE_RESERVE];

Generally with members you tend to store those numbers anyway.

Mainly for readability, like what if you wanted it to be divide by 6, you usually put these in a field for easy got swapping of code.

Great thanks I understand

It was from a Java4K game though, maybe used for memory / performance reasons, avoid constants etc?

But its not just that, it can be midly seen in Libgdx peoples sources to. But like everyones said above, you can see more visually why its done, because of like a doc thing when your using box2d vars outside box2ds world variables. Making sense?