[fpc-pascal] with statement using mulltiple objects

Marius fpclazarus at home.nl
Sun Sep 14 14:20:28 CEST 2014


Jürgen Hestermann wrote:

>Realy? I love it. Consider these two varianst of code from one of my
>programs:

This is a simpler example than the one with with the 4 levels deep
(which can be quite a puzzle if there are multiple records containing
the same fieldname(s)). And yes you may love them but please read on..

>Which one would you prefer?

The second, but not for readability or typing, I would suggest a third
solution by using a variable (like Matthias also showed).

The point is you are very likely to run into refactoring problems.
Suppose your routines are in 2 different methods in a class and there
is class inheritance and one of the base class has a property
errorstring. Suppose you decide to refactor the errorstring in you
record structure to something else (and forget to adjust it properly
which is human). Your first solution will compile but it would reset
the wrong errorstring, your second solution will simply not compile
because it has been prefixed (hence my preference to pick that one).
You can argue its the fault of the programmer but why create an unsafe
situation in the first place (and that just what the with statement
does)...

We have spend weeks (if not months) off hunting down this kind of bugs.
And its this kind of bug what showed up frequently in teams of
programmers and that made us decide to ban the with statement as much
as possible. Sooner or later the with stament is going to bite you, I
just hope you wont have to spend that much time on resolving it ;(


Marius




More information about the fpc-pascal mailing list