The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,262 other subscribers

Oh boy, VB.NET and JavaScript both have a `with` keyword too!

Posted by jpluimers on 2024/03/05

Last year, within a week, I saw two tweets of languages that, like Pascal, have a with statement as well:

  1. [Archive.is] Shawn Wildermuth 💻☕🎸🎥🎮 on Twitter: “JavaScript’s Forgotten Keyword (with)”
  2. [Archive.is] John Kaster #BlackLivesMatter on Twitter: “@suited_aces @marcocantu @delphijunkie @JimMcKeeth @jpluimers I present “with”… “

The first points to an article that shows the JavaScript implementation of with is very similar to the Pascal one: [Wayback] JavaScript’s Forgotten Keyword (with) – DEV Community.

Just in case some of my readers do not know my opinion of the Pascal with statement  (it even has it’s own blog category), I really think you should not use it Delphi: you should avoid the with statement as it makes your code less future proof.

The reason not to use it is called [Wayback] Accidental Shadowing in computer language speak (it also can rear its head when you define variables at different block levels like for instance this golang example: [Wayback] Warning for accidental variable shadowing with block scope – Technical Discussion – Go Forum).

Even the JavaScript specification advises against using the with keyword in [Wayback] with – JavaScript | MDN

**Warning:**Use of the with statement is not recommended, as it may be the source of confusing bugs and compatibility issues. See the “Ambiguity Contra” paragraph in the “Description” section below for details.

There have been various proposals to extend the Delphi implementation of the with statement to make it more resilient to Accidental Shadowing by forcing the usage to be prepended by a . (dot) or alias, as for instance seen in [Wayback] Re: “with” Coders are Monsters – delphi / [Wayback] delphi • View topic • “with” Coders are Monsters:

This is in fact what the second twitter messages pointed to: a VB.NET example doing just that: prepend with a dot: [Wayback] Maarten Balliauw on Twitter: “Looks like using With makes it pretty clean!… “

I was not even aware that VB.NET had it, but it has: [Wayback] With…End With Statement – Visual Basic | Microsoft Docs

And it has similar debugging issues as with Delphi as per [Wayback] The VB.NET ‘With’ Statement – embrace or avoid? – Stack Overflow:

Find the beginning of a With statement and set a breakpoint. Step to the next line (so you’re hiding the first line right under the if block). Highlight it, then ‘Add Watch’. You should see this: ‘With’ contexts and statements are not valid in debug windows.

–jeroen

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.