[fpc-pascal] Optional param modifier
lazarus at mfriebe.de
Mon Apr 15 00:30:07 CEST 2019
On 14/04/2019 22:08, Ryan Joseph wrote:
> function FindThing: TThing; optional;
> means you MUST check for nil. If the value is nil that’s not an error. “optional” is probably a bad name for this I know.
> function MakeThing: TThing;
> this means maybe check for nil or maybe not check for nil. We don’t know what the programmer intended because no extra information was given (at least in the declaration). Because of the name we probably assume this is going to return a new object which is safe so checking for nil would be a waste.
I would actually argue that it is infinitely more important to check for
nil after MakeThing. (Unless I know for sure it is not needed: proof, docs)
Assuming (taking the danger of doing so) both do what there name
- If I forget to check after FindThing, it is likely to cause an error
very soon, and most probably while I am testing myself.
- If I forget it after MakeThing, it may be very rare to cause an error.
It will likely pass my tests, and cause a customer of mine some stress.
More information about the fpc-pascal