• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Constant Versus Non-Constant Variables

Status
Not open for further replies.
Why would someone use a constant variable (locally or globally) over having it be non-constant? Does it provide some speed gain? How do handles behave if they are constant?
JASS:
globals
    constant trigger T=CreateTrigger()
    // Versus.
    trigger T2=CreateTrigger()
endglobals
I know that a constant variable can't be assigned to after being declared, but is that the only change? If this is the case, then why useconstantat all? I'm curious as to how the Warcraft III engine reacts to constantness, not how some third-party tool uses it, or that 'you can't declare constant variables without vJASS'.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
From Jeff Pang's Jass manual (http://jass.sourceforge.net/doc/globals.shtml):

A non-array variable declaration may be prefixed with constant. This means the variable contains a value that can not be changed later (e.g., you can not use set on it). Hence, constant variables must be initialized with a value.

Apparently you can also make code/function constants too. Are these faster than non-constant functions?

If a function declaration is prefixed with constant, like the following:

JASS:
constant function const_func takes integer a returns nothing
     ...
endfunction
then you can not call non-constant functions within the function body. (Nevertheless, note that you can still use the set statement in the body to alter function arguments, so it is not really "constant" in the typical sense; it is more of a hint to the programmer than anything else)

The keyword exists in JASS and isn't some add-on from vJASS. From previous experience in programming languages, I would suggest the constant keyword functions the same way as in JASS. It makes variables immutable, so I would hope this results in some (slight) reduction in access time and/or space storage.

Besides making sure the variable can't be changed, it also is a form of documentation and parametrization (a parameter to some computation).

I don't know anything about how it internally works compared to non-constant variables. Looking forward to hearing about that.

Also are you sure you can create constants locally? That wouldn't really make any sense AFAIK. I would think constants have to be declared in global blocks, and perhaps some constants need to be initialized in init functions etc.

For the trigger case, I don't see a point in making a trigger a constant--does it even make a difference for non-primitive values, e.g. a constant loc? a constant rect? After all you can't really change a trigger object itself (add conditions, sure), unlike a real or a boolean.
 

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
It's a pretty useless thing actually. It's a lame way to show someone that they shouldn't change that variable. Or actually there's a small benefit with with constants in other languages. (that actually got a good editor) If the editor got some kind of auto complete you can make some variables not getting listed there.
Actually I am not sure about that, but I think it worked like that.

edit: ok I went and tested, it works that way.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
From Jeff Pang's Jass manual (http://jass.sourceforge.net/doc/globals.shtml):



Apparently you can also make code/function constants too. Are these faster than non-constant functions?



The keyword exists in JASS and isn't some add-on from vJASS. From previous experience in programming languages, I would suggest the constant keyword functions the same way as in JASS. It makes variables immutable, so I would hope this results in some (slight) reduction in access time and/or space storage.

Besides making sure the variable can't be changed, it also is a form of documentation and parametrization (a parameter to some computation).

I don't know anything about how it internally works compared to non-constant variables. Looking forward to hearing about that.

Also are you sure you can create constants locally? That wouldn't really make any sense AFAIK. I would think constants have to be declared in global blocks, and perhaps some constants need to be initialized in init functions etc.

For the trigger case, I don't see a point in making a trigger a constant--does it even make a difference for non-primitive values, e.g. a constant loc? a constant rect? After all you can't really change a trigger object itself (add conditions, sure), unlike a real or a boolean.

to add on missing knowledge of the sourceforge, you cannot modify any constant or nonconstant variables inside constant function

JASS:
globals
    private integer i = 0
endglobals
constant function a takes nothing returns nothing
    set i = i + 1
endfunction

will not compile
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
I really don't know what is the point of constant keyword in vanillas jass, but Vexorian using them for map optimizer.If a variable has constant keyword it gets inlined, if a function has constant keyword and it doesn't use its parameters more than 1 time it gets inlined.
 
Odd, you can't set variables in a constant function, yet you can re-assign to function arguments. A constant function doesn't imply its arguments are constant anyways. So, constant is only useful as a note to the programmer (also known as a REM, or comment) and to allow 3rd party tools replace instance of the variable with its value? That's pointless, and until I hear otherwise, guess I won't bother typing that word anymore.
 
Level 15
Joined
Aug 7, 2013
Messages
1,337
Well nobody has answered whether the constant keyword does anything for handles (like your example, a constant trigger).

But for primitive types (boolean, real, integer, etc.) I still think it's good style to use the constant keyword when applicable.

Besides making sure the variable can't be changed, it also is a form of documentation and parametrization (a parameter to some computation).

Of course, if you are the only one reading your code, and you know what it does, then the constant keyword probably seems redundant. But for me at least, if it's not a constant, then it's a parameter and should be an argument to a computation. e.g. TOTAL_PLAYERS should be constant, since it should never ever be changed in game (how can the game gain players?), while something like the distance traveled per second when falling might be 9.81 m/s^2 on Earth, but another planet it might be higher or lower, which you would need to tune for your physics engine. In that case it should be an argument to a function, and not a constant (until all testing is done or you only have one planet).

Lastly I thought I read you didn't use (v)Jass, so if that's the case I don't think constant keyword factors into GUI?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
He is not using vJass, he is using Jass

Constant keyword to me seems to be used by blizzard in a way that, for given input, you will always get the same output per running instance. For instance GetTriggerPlayer is constant, because it returns the same unit in the same running instance
 
Level 26
Joined
Mar 19, 2008
Messages
3,140
@edo494 is right.

@Zeatherann constant keyword has its meaning.
We use constant to tell that given data is unwritable.
We use constant to tell user what he should do (constant function) - and yes, it somewhat restricts flexibility.
And foremost, we use constants to define compile-time variables as usually we use them when writing overall code framework.

However, indeed in Jass it might not feel as usefull. Tho in C and C++ you should use it to your advantage when needed.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,202
In JASS as far as I can tell it has no actual benefit at all next to some compile time safety in interface design. Using constant you can guarantee that a variable value will not change during run time, and users of a system can see that the system is not designed for it to change.

As far as I can tell, constant functions made no difference at all, which them returning a run time value all the time.

It may be some feature they added to the JASS language during design phase before they decided to go with an interpreter, which sort of destroys its usefulness. That said I am not sure how constants modify the JASS interpreter byte code, they may cause a value to inline so it does not undertake run time name resolution but I suspect it may make no difference at all and only affect compile time validity.

In a programming language like C/C++, constants have a very different meaning. Constant global values may be inlined into instructions. Register consistency is much easier to optimize with constant variables (no need to reload from memory as you know it will not have changed). In the case of Harvard Architecture systems such as ARM processors you can have global constants written to ROM where as all variables have to be allocated in RAM, which also can speed up execution performance since it frees the data bus for other data. It also imposes strict behavioural requirements on code such as a constant method always returning the same result or a constant argument never being modified, both very useful for interface design.
 
Status
Not open for further replies.
Top