Functions and/or methods.

BlitzMax Forums/BlitzMax Beginners Area/Functions and/or methods.

tonyg(Posted 2007) [#1]
What are the pros/cons of doing each of the following :
Type ttest 
	Field x:Int=5
	Function set_xval1(temp:ttest, xval:Int)
		temp.x = xval
		Print temp.x
	End Function
	Function set_xval2(temp:ttest , xval:Int)
		temp.set_x(xval)
	End Function
	Method set_x(xval:Int)
		x = xval
		Print x
	End Method
End Type
Local test:ttest = New ttest
'test1
ttest.set_xval1(test , 1)
'test2
ttest.set_xval2(test , 2)
'test3
test.set_x(3)

or any other way for doing the same sort of thing?
I'm currently using 'test2' (to fit in with Bmax way of doing things) but find myself writing huge amounts of code.


Brucey(Posted 2007) [#2]
I guess the set_xval2/set_x combination gives you the most flexibility.

I certainly use the Functions when I'm calling into Max from C/C++ and passing in the Object reference.

For most use, the set_x() method is probably enough, unless you need that extra layer of abstraction?

I wouldn't use set_xval1(), since set_x() is doing the same thing essentially, and you are in effect duplicating code - which is always not a great idea.
Of course, if you are desperate for speed, you might not want the extra "call" that set_xval2() introduces...


Who was John Galt?(Posted 2007) [#3]
There's not a lot of difference, but I would always use the method in the example you give. Generally I use a function where I need to do operations on type globals or all instances of the type at once. In some instances functions have the advantage in that max has function pointers but not method pointers.


tonyg(Posted 2007) [#4]
Thanks for the responses and it's help get my head straight.
I think for readbility set_xval2 is probably best with the option to use the method for speed critical stuff.


Grey Alien(Posted 2007) [#5]
intersting. Personally, as my logic is separate from my draw I wouldn't even have a method or function for such a simple assignment i.e. I'd just use temp.x = 5, then I'd be calling Print later on as part of the draw cycle. Of course I realise taht your code is just a cut down example and if there were more variables to be set and perhaps other methods to be called then, yeah I'd wrap it all up in a nice method, but I wouldn't bother with a function to call the method as I rarely use TSomething.myfunction() except for when creating new instances...


impixi(Posted 2007) [#6]
I’m in Grey’s camp. Unless you need to perform boundary checks or a calculation on the passed parameters, you might as well just set and get your fields’ values manually.

test.x = 3
Print test.x

I know it doesn’t conform to strict object-oriented principles, but in a ten thousand iteration loop, for example, it *should* be faster without the call to a function or method every time.

Now, in the case of more complicated ‘setters’ I’m not sure which of your above examples would be ‘better’. Is there a noticeable performance difference? Possibly (might have to whip up some test code). Which is more ‘readable’/’maintainable’ in my opinion? I’d say instance-based methods are better, but obviously that’s a personal choice...


tonyg(Posted 2007) [#7]
Just to clarify. This is a very simplified example to show the available methods and not a request for help on setting a variable.
There is a slight speed difference using set_xval2 and a
very very very very slight difference between set_xval1 and set_x.
My main concerns are readability (without affecting speed too much) and portability so doing this in-line isn't an option.
Thanks for the responses.