gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > Sending constant values to a function (which is better)

#125826 - keldon - Tue Apr 17, 2007 11:01 am

This is not c/c++ specific so I put it here. Which do you think is better, and why.
(1)
Code:
   int width;
   int height;
   boolean hasZBuffer;
   Bitmap bitmap = new Bitmap ( width = 80, height = 60, hasZBuffer = true );

(2)
Code:
   final (or const) int width = 80;
   final (or const) int height = 60;
   final (or const) boolean hasZBuffer = false;
   Bitmap bitmap = new Bitmap ( width, height , hasZBuffer );

(3)
Code:
   Bitmap bitmap = new Bitmap ( int width = 80, int height = 60, boolean hasZBuffer = true );


I quite like the third one because you can keep the variable local to the function while also providing a descriptive name of the variable being sent; but you cannot do this in any language (I know). The next one for me is the first one, and you can use some form of notation to separate these variables from others.

EDIT: in fact for the third one (if it were to be a new feature to a language/script language) you could have a different keyword altogether such as *hmm* param!

#125827 - Lick - Tue Apr 17, 2007 11:19 am

What about (4):
Code:
Bitmap bitmap = new Bitmap(80, 60, true);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
bool zbuffer = bitmap.hasZBuffer();


I think in all of your examples, the passed values are constant and known anyway. If you dynamically retrieve the passed values, then it's a different case.
_________________
http://licklick.wordpress.com


Last edited by Lick on Tue Apr 17, 2007 11:20 am; edited 1 time in total

#125828 - kusma - Tue Apr 17, 2007 11:19 am

I'm not a big fan of default parameters for things that aren't obvious as a default (width and height fall into these categories IMO - it might not have if I knew about the context). Another thing to keep in mind is when you read code like this:

Code:
Bitmap *bmp = new Bitmap(34,324, true);


do you really know what true means when you read the code? I sure don't (IMO a zbuffer are not a property of a bitmap - a bitmap and a zbuffer (which again is a special case of a bitmap) are possible properties of a render-target). Anyway, I think it's more understandable with something like:

Code:
Bitmap *bmp = new Bitmap(234,234, ZBufferEnable);


10 months into your project, this you'll understand this line right away. The other one, you'll most likely have to look up the prototype to understand.

edit: added code-tags

edit: oh, and it seems I completely missed the point of the question. Ohwell, ignore me please :)


Last edited by kusma on Tue Apr 17, 2007 11:22 am; edited 1 time in total

#125829 - Lick - Tue Apr 17, 2007 11:21 am

Kusma has a point there.
_________________
http://licklick.wordpress.com

#125859 - Ant6n - Tue Apr 17, 2007 5:27 pm

i dont really see what you are asking here; but your number 3) there looks like python syntax

#125863 - DekuTree64 - Tue Apr 17, 2007 6:12 pm

I like the first syntax in the poll, but I think I'm treating it differently than you are... What I'm thinking is passing parameters by name rather than by order:
Code:
class Bitmap {
    Bitmap(int width, int height, bool hasZBuffer = false);
};
...
Bitmap good1 = new Bitmap(width = 10, height = 10);
Bitmap good2 = new Bitmap(height = 100, hasZBuffer = true, width = 200);
Bitmap bad = new Bitmap(width = 50); // Causes error, height undefined

i.e. parameters without default values are required, but you can pass all the parameters in any order because you specify the name along with the value. The name should make it obvious enough what type it is, and you shouldn't have to create intermediates.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#125867 - josath - Tue Apr 17, 2007 7:07 pm

You can do this in javascript, actionscript (used in Flash), or other ECMAScript-based languages

Code:

var mybitmap:Bitmap = new Bitmap({width:800, height:600, zBuffer:true});


The order of the paramaters do not matter. You can throw errors in constructors in AS3 if the paramaters you want aren't defined, but I'm not 100% sure about other versions of actionscript or javascript.

#125869 - simonjhall - Tue Apr 17, 2007 7:14 pm

I do image processing every day, and I'd roll with the second option. I also don't like it when more than one thing happens per line (eg the others you're setting stuff as well as calling stuff). Oh and the enumeration stuff suggested by kusma sounds a good route too...

Btw keldon, you have 1337 posts. Teh pwn, buddy, teh pwn.
_________________
Big thanks to everyone who donated for Quake2

#126077 - sgeos - Thu Apr 19, 2007 9:23 pm

kusma wrote:
I'm not a big fan of default parameters for things that aren't obvious as a default

Then make them obviously default?
Code:
// default values
#define DEFAULT_BMAP_WIDTH       80
#define DEFAULT_BMAP_HEIGHT      60
#define DEFAULT_BMAP_HASZBUFFER  true


There is no best general pattern for something like this. It really depends on what you are doing- do you need the values later? Here are a few variations.
Code:
// default bitmap
Bitmap bitmap = new Bitmap ( DEFAULT_WIDTH, DEFAULT_HEIGHT , DEFAULT_HASZBUFFER );


Code:
// pass default values and verify (they might be "corrected")
Bitmap bitmap = new Bitmap ( DEFAULT_WIDTH, DEFAULT_HEIGHT , DEFAULT_HASZBUFFER );
width   = bitmap.getWidth();
height  = bitmap.getHeight();
zbuffer = bitmap.hasZBuffer();


Code:
// pass (default) values; change values if you don't want defaults
int     width      = DEFAULT_HEIGHT;
int     height     = DEFAULT_WIDTH;
boolean hasZBuffer = DEFAULT_HASZBUFFER;
Bitmap bitmap = new Bitmap ( width, height , hasZBuffer );


Code:
// pass (default) values and verify; change values if you don't want defaults
int     width      = DEFAULT_HEIGHT;
int     height     = DEFAULT_WIDTH;
boolean hasZBuffer = DEFAULT_HASZBUFFER;
Bitmap bitmap = new Bitmap ( width, height , hasZBuffer );
width   = bitmap.getWidth();
height  = bitmap.getHeight();
zbuffer = bitmap.hasZBuffer();

The nice thing about using macros as constants is that it can make finding bugs easy. (There is an easy to find bug in one of the the above code samples that I didn't bother to fix.)

-Brendan