To construct or not?

BlitzMax Forums/BlitzMax Programming/To construct or not?

Grey Alien(Posted 2006) [#1]
take this example:

Type Ttest
Field a = 10
End Type

Type Ttest2
Field test:Ttest = New TTest
End Type

t: Ttest2 = New Ttest2
Print t.test.a


TTest2 doesn't have a constructor and the test Field is made automatically due to the New TTest statement. Is it preffered that I would make a New() method and in that method put test = New TTest instead? Seems a bit long winded if not needed or is it better for code clarity etc? opinions ...


N(Posted 2006) [#2]
There's no good reason to move it.


Grey Alien(Posted 2006) [#3]
OK thanks, any other opinions? Also imagine this type was a lot bigger and it contained many other types, should I create them all in a constructor or not?


N(Posted 2006) [#4]
Unless you need to call some specific Create function instead of New, there's no really good reason for it other than preference and coding style.

I personally have no preference, it just depends on what I'm doing at the time. If I know the object has to be initialized before anything else, chances are I'll throw it in the Field definition instead of the constructor unless I have to set some values in it, in which case I would put the creation in the constructor as well just in case something in BMax changes that causes the fields to be initialized after the constructor (would be weird, but you never know).


Grey Alien(Posted 2006) [#5]
that's a good point, if you need a certain creation order, putting them in the contructor will be safer.