[Lazarus] 'with' is evil, isn't it?

Michael Van Canneyt michael at freepascal.org
Wed Aug 3 13:34:48 CEST 2011



On Wed, 3 Aug 2011, Alexander Klenin wrote:

> I also use 'with' extensively, despite the risks,
> since with the right usage the readablity gains are considerable.
>
> 2011/8/3 Max Vlasov <max.vlasov at gmail.com>:
>> 1. Introducing a check in the compiler setting (or a conditional define)
>> that could force the developer to use self inside 'with' constructions.
> ...
>> 2. Some switch (maybe on by default) that prevents from compiling if there
>> are several duplicate names in the same 'with' context (including global,
>> local, fields).
>
> Error is IMHO too severe. I think a warning if the name inside of
> 'with' overrides a name from outer scope
> would be an acceptable compromise.
>
> Of course, a disambiguation facility would be even better,
> but I am afraid FPC developers will resist this improvement :(

Frankly, I don't understand the discussion.
I use 'with' all the time because it increases readability and reduces typing.
I don't want to be punished with heaps of warnings simply because I use a 
language feature. Yes, I am aware that there are risks involved.

If you don't like 'with', then don't use it, end of story.

Introducing the proposed switch is nonsense, since it is useful only when 
dealing with other people's code. For your own code, you don't need this 
switch. Simply don't use 'with', and you're safe. If you type 'with' anyway, 
that would be very inconsequent of you.

And resolving one perceived problem (with) by forcing people to use 'self'
  - which is reserved for the current instance of a class or object -
means introducing a new ambiguity, which has *exactly* the same problem 
as the original when used inside classes. (not to mention that there 
would then be no way to access the class instance 'self' inside a 'with')

Again not very consequent.

Michael.




More information about the Lazarus mailing list