# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2008-02-04 23:57:54

xot
Registered: 2007-08-18
Posts: 1,202

### A question of style: arguments

This is not so much a request for help, but rather a survey of your opinion on the handling of arguments inside of a script.

First, in general, I assign arguments to variables at the top of a script for the sake of readability. However, using the arguments directly is faster (by a surprising margin) than using temporary variables, so for very simple scripts I'll sometimes use the arguments directly.

Second, I generally avoid changing the values of arguments inside a script because I feel it is, for lack of a better word, misleading -- when you change those values they don't truly represent the actual arguments anymore.

Take the following code for example:

Expand// arg0 = string to test (string)
// arg1 = valid characters (string)
// arg2 = case insensitive, optional (boolean)
// returns true if the test string contains
// only valid characters, false otherwise
{
var len,i;
len = string_length(argument0);
if (argument2)
{
argument0 = string_lower(argument0);
argument1 = string_lower(argument1);
}
for (i=1; i<=len; i+=1)
{
if (!string_pos(string_char_at(argument0,i),argument1))
{
return false;
}
}
return true;
}

Do you consider that bad form? Where do you guys stand on the direct use of arguments, and on the practice of modifying the values of arguments inside of a script?

Abusing forum power since 1986.

Offline

## #2 2008-02-05 07:09:44

EyeGuy
Member
Registered: 2007-10-18
Posts: 19

### Re: A question of style: arguments

I don't like the idea of changing arguments mid-script.  I know it's equivalent to a call-by-value function in which you are sort of expected to change the value of the argument, but stylistically,  I think "argument0" means the value assigned to an argument and changing the value just seems confusing.

As for using temporary variables for arguments, it all depends on which looks more readable.  I usually just use argument# unless the value is used many times (well, more than once or twice) or it needs to be modified.

If there really is a big difference in adding temporary variables, then for extremely efficient scripts, I could see how you would want to do the faster thing, even changing the argument's value in the script like this.  But for scripts that aren't extremely efficient and likely to be called extreme amounts, then I always say for Game Maker style comes before slight performance difference.

Offline

## #3 2008-02-05 16:02:24

Yourself
Member
Registered: 2007-10-09
Posts: 48

### Re: A question of style: arguments

Do you consider that bad form?

No, not that script specifically.  As long as you don't completely change the value to something that isn't representative of what it originally was, I don't see a problem.

Offline

## #4 2008-02-06 18:31:48

Quimp
Member
Registered: 2007-10-09
Posts: 15

### Re: A question of style: arguments

I believe I'm on the same boat as Yourself. If I pass an array to a script in the form of a string, and as a feature I let the user specify whether the array is global or not. I will then remove the "global." part of THE argument and trigger a boolean variable for calls to global or local functions (variable_array_* or whatever they are).

Offline

## #5 2009-10-29 12:18:46

IamCalle
Member
Registered: 2007-10-20
Posts: 23

### Re: A question of style: arguments

By default, I go for performance all the way.
That is mainly because I have seen how big the margin between assigning every single argument to a temp. variable, or assigning only the very ones needed for the script to function.
On the direct argument changing case I agree with Yourself, it is not considered bad for by me and I happen to practice it myself.

I find it a matter of necessity on whether or not to assign all the arguments to temp. variables.
If the script is inside your application only, then why not go for performance? -After all; you should know your scripts and GM applications especially should work as fast as possible, in my opinion.
If others are meant to use them then I think you may want to go for the GMLscripts' default style.

Offline

## #6 2011-12-30 16:36:52

xot
Registered: 2007-08-18
Posts: 1,202

### Re: A question of style: arguments

Yeah, this topic is quite old, but I realized last night that I needed to add a caveat if you plan to use arguments this way.

If you use argument variables willy-nilly, treating them as temporary variables and assigning values to them for the sake of speed, you will run into problems with newer versions of Game Maker, starting with 8.1 (and presumably GM:HTML5 as well). If an argument isn't passed to the script in the first place, it is unavailable to the script and you will get an error if you try to access it in any way.

So something like this is no longer possible:

Expand// pointless_sqr_function(value)
{
argument1 = argument0 * argument0;  // throws an error because memory
return argument1;                   // for argument1 wasn't allocated
}

Abusing forum power since 1986.

Offline

## #7 2011-12-30 17:35:38

Daniel
Member
Registered: 2011-05-04
Posts: 25

### Re: A question of style: arguments

I always edit the arguments in-place, and will do so until we can somehow define data TYPE's for the arguments, because right now there is no point, but if the function definitions were more like any other language then I would of course use the new argument declerations.

Offline

## #8 2011-12-30 23:05:03

paul23
Member
Registered: 2007-10-17
Posts: 110

### Re: A question of style: arguments

Daniel wrote:

I always edit the arguments in-place, and will do so until we can somehow define data TYPE's for the arguments, because right now there is no point, but if the function definitions were more like any other language then I would of course use the new argument declerations.

Use for me is that I can swap order of arguments easily.. As well as that it seems more clearer.. What I do now is simply:

Expandvar v;
v = DEFAULT VALUE;
if (argument_count > 0) {
v = argument[0];
}

Offline

## #9 2013-05-23 20:31:52

csanyk
Member
From: NE Ohio
Registered: 2011-04-07
Posts: 13
Website

### Re: A question of style: arguments

xot wrote:

This is not so much a request for help, but rather a survey of your opinion on the handling of arguments inside of a script.

First, in general, I assign arguments to variables at the top of a script for the sake of readability. However, using the arguments directly is faster (by a surprising margin) than using temporary variables, so for very simple scripts I'll sometimes use the arguments directly.

Second, I generally avoid changing the values of arguments inside a script because I feel it is, for lack of a better word, misleading -- when you change those values they don't truly represent the actual arguments anymore.

Take the following code for example:

Expandsnip

Do you consider that bad form? Where do you guys stand on the direct use of arguments, and on the practice of modifying the values of arguments inside of a script?

Not at all.  As long as the function is simple, it's OK to define the arg variables in comments like that.

I *do* like named variables for readability and ease of understanding, so I use them almost always, unless optimization is critical.

By the way, which version of GM did you use to determine that delcaring local vars is slower?  Was it 8.x?  Is it any better in Studio since everything's now compiled?

Offline

## #10 2013-05-23 21:19:58

xot
Registered: 2007-08-18
Posts: 1,202

### Re: A question of style: arguments

When that was posted, GM7 was the most recent release and probably what I was using. I don't think I've done that kind of test since.

Abusing forum power since 1986.

Offline