[fpc-pascal] Re: classes initialization

L L at z505.com
Wed Mar 26 00:32:03 CET 2008


Joao Morais replied to Jonas with:

 >I think I didn't get your point. What about this piece of code:
 >
 >interface
 >
 >function foo: tfoo;
 >
 >implementation
 >
 >var
 > _foo: tfoo;
 >
 >function foo: tfoo;
 >begin
 > if not assigned(_foo) then
 >   _foo := tfoo.create;
 > result := _foo;
 >end;
 >
 >Is it safe?

What about this:

var
  _foo: tfoo = nil;

That way you declare it right there and then.. ensuring it is not assigned.

Hence why I think a good human practice is to always init everything 
even if it psychologically affects some people in ways such as "will 
this slow the program down though? Do I need to init this stuff if I am 
going to init some of it later"? which is evil premature optimization 
that causes bugs.

L505




More information about the fpc-pascal mailing list