Jump to content
Tommi Prami

With remover refactoring tool to IDE, Please vote this feature request:

Recommended Posts

Sometimes removing withs, especially nested ones, are really tricky. Easy to mess up. Just got bit by this other day. IDE/Compiler/LSP must know which is which, so it would get first pass  way faster than doing it manually.

https://quality.embarcadero.com/browse/RSP-18953?filter=-2

On a side note thee has not been too much love towards refactoring features in IDE. Hope it would get more and ones we have to get better.

 

-Tee-

  • Like 2

Share this post


Link to post
Guest

really is not necessary remove it... JUST DONT USE IT! 😂:classic_biggrin:

 

hug

Share this post


Link to post
1 hour ago, emailx45 said:

really is not necessary remove it... JUST DONT USE IT! 😂:classic_biggrin:

We have lots of legacy code that still uses "with" and I'm hesitant to touch this code because of this (Guess what's also missing? Unit tests. I add them when I find the time, but progress is slow.) A tool that reliably removes "with" statements would be awesome. Unfortunately it's also far from trivial.

  • Like 1

Share this post


Link to post
On 4/1/2021 at 6:22 PM, dummzeuch said:

We have lots of legacy code that still uses "with" and I'm hesitant to touch this code because of this (Guess what's also missing? Unit tests. I add them when I find the time, but progress is slow.) A tool that reliably removes "with" statements would be awesome. Unfortunately it's also far from trivial.

Exactly.

 

And also removing withs are quite pain by hand. More complicated it is. Nested with clauses are pure evil. 

Even ones that seems trivial are pain to remove sometimes. 

 

-Tee-

Share this post


Link to post

IMHO Borland should've borrowed alias construction from SQL (select Foo as f, Bar as b ...) like this:
 

with some.long.long.nested.object as foo do

  foo.bar := laz

 

  • Like 1

Share this post


Link to post

 

55 minutes ago, Fr0sT.Brutal said:

IMHO Borland should've borrowed alias construction from SQL (select Foo as f, Bar as b ...) like this:

No need for that. You can already write this instead of such a with-clause. 

begin
  var foo := some.long.long.nested.object;
  foo.bar := laz
  ...
end;

That doesn't help converting existing with constructs, though.

  • Like 1

Share this post


Link to post

Even if there would be an option to define an "alias", some programmers will never give up using with clause. The would do it because they can. This is excerpt from InputQuery in Vcl.Dialogs.pas:

Form := TInputQueryForm.CreateNew(Application);
with Form do
  try
    ...
  finally
    Form.Free;
  end;

Regarding the refactoring tool it would be probably useless (buggy as hell) considering that they still didn't get Code Insight/LSP right.

Share this post


Link to post
55 minutes ago, Uwe Raabe said:

No need for that. You can already write this instead of such a with-clause. 


begin
  var foo := some.long.long.nested.object;
  foo.bar := laz
  ...
end;

That doesn't help converting existing with constructs, though.

Unfortunately inline vars are not always equivalent to "with". I'm currently working on a project that has, um.. let's be polite and say, "liberal" use of record arrays.

 

So for example:

type
  TFoo = record
    // Lots of stuff here
  end;

  TBar = record
    Foo: array of TFoo;
    // Even more stuff
  end;

  TFooBar = array of TBar;
var
  FooBar: TFooBar;
begin
  with FooBar[i].Foo[j] do
    WhatEver := 42;

  var Foo := FooBar[i].Foo[j];
  Foo.WhatEver := 42; // Nope.
end;

 

Using an inline var to access an inner record will create a copy of the record while using "with" will use a reference to the record. Only way around that is to use record pointers but the code is horrible enough as it is.

  • Like 2

Share this post


Link to post
3 hours ago, Uwe Raabe said:

No need for that. You can already write this instead of such a with-clause. 

This feature took them 20 years and it still has drawbacks. If they've implemented aliases from the beginning, no troubles with with would happen ever

Share this post


Link to post
2 hours ago, Anders Melander said:

Using an inline var to access an inner record will create a copy of the record while using "with" will use a reference to the record. Only way around that is to use record pointers but the code is horrible enough as it is.

On the positive side: This way Delphi developers learn about pointers and how to use them correctly. 😉

type
  PFoo = ^TFoo;
  TFoo = record
    // Lots of stuff here
  end;

  PBar = ^TBar;
  TBar = record
    Foo: array of TFoo;
    // Even more stuff
  end;

  TFooBar = array of TBar;
var
  FooBar: TFooBar;
begin
  with FooBar[i].Foo[j] do
    WhatEver := 42;

  var Foo := @(FooBar[i].Foo[j]);
  Foo.WhatEver := 42; // Nope.
end;

(Now, It would be really embarrassing if I got this wrong. 😉 )

 

I'm always unsure whether I need the parenthesis in this:

 var Foo := @(FooBar[i].Foo[j]); 

 

Edited by dummzeuch

Share this post


Link to post
4 minutes ago, dummzeuch said:

(Now, It would be really embarrassing if I got this wrong. 😉 )

You got it almost - when using type inference on inline variables from pointers are no typed pointers but untyped so you have to explicitly state the type PFoo for your Foo variable.

Share this post


Link to post
34 minutes ago, Stefan Glienke said:

You got it almost - when using type inference on inline variables from pointers are no typed pointers but untyped so you have to explicitly state the type PFoo for your Foo variable.

Ouch. I guess this shows that I'm not used to inline variables.

Share this post


Link to post
58 minutes ago, Fr0sT.Brutal said:

If they've implemented aliases from the beginning, no troubles with with would happen ever

As long as no time machine is available it doesn't help much to lament about facts that cannot be changed afterwards. (One could as well claim that there would be no with-troubles when N.Wirth didn't invent it in the first place.)

 

The topic is not about with being bad or how it could be made better, but about a tool to convert with statements without breaking existing code (which can be quite tricky shown by Anders).

Share this post


Link to post
3 hours ago, Uwe Raabe said:

As long as no time machine is available it doesn't help much to lament about facts that cannot be changed afterwards. (One could as well claim that there would be no with-troubles when N.Wirth didn't invent it in the first place.)

 

The topic is not about with being bad or how it could be made better, but about a tool to convert with statements without breaking existing code (which can be quite tricky shown by Anders).

So the solution is simple: Let's invent a time machine, go back to 1968 and kill N. Wirth. Problem solved.

Or, a bit less blood thirsty: Get him involved with a girl that keeps him from inventing Pascal. 😉

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×