Recent

Author Topic: Making a plea :)  (Read 6414 times)

ericg

  • New member
  • *
  • Posts: 7
Making a plea :)
« on: October 03, 2024, 08:33:59 am »
Hi, all I'm making a plea for supporting a feature that's been debated already, but I feel is sorely missing, and hampering FreePascal in both short and long term, as it's getting established now: inline variable declaration with type inference.

Key scenarios where it is a major improvement vs legacy Pascal.

1. Loop variables

"For" loop variables are a perfect fit, as declaring loop variables leads to ambiguity. The loop variable being undefined after the loop being a documented code smell (https://wiki.freepascal.org/For#Loop_Completion).
But local loop variables also don't make sens to be visible outside the loop: they only open up opportunities for errors and readability complexity, I'm referring to things like "item" in the legacy form of

Code: Pascal  [Select][+][-]
  1. for var i := 0 to someList.Count-1 do begin
  2.    var item := someList[i];
  3.    ...
  4. end;

where inline and scoped declaration makes everything easy to understand and debug, as it's all local.

2. Simplifying long expression and function calls

Being able to introduce easily a tight-scoped variable can be a massive boost on readability AND debuggability

Code: Pascal  [Select][+][-]
  1. begin
  2.    var x := ...some longish expression that computes x...;
  3.    var y := ...some expression with side effects that computes y...;
  4.    MyFunction(x, y);
  5. end;

Even with just two variables, the above code allows to spread parameter evaluations cleanly, and it becomes trivial to breakpoint the code after a particular argument is computed and eval the value.
It also brings to the table the ability to name the argument, which can be a significant boost to code readability for functions with many parameters, or functions you're unfamiliar with the order of parameters.

3. Record initialization

Rather than the legacy variable + fillchar dance, with risk for error and need to read multiple lines of the code, you can just do

var myDescriptor := Default(TSomeDescriptor);
myDescriptor.Field:= 123;
...

I took the descriptor structure exemple, as it's common in APIs. The inline declaration makes everything local and obvious to whoever will read the code.

4. Elimination of the "catalog of variables" anti-pattern

Every function implementation where you have many variables (like in the above points) quickly becomes a massive catalog of variables declaration, followed by implementation. This declarations are not visible without scrolling, and since you have many of them, they're not always readable.

When faced with unfamiliar code, the catalog anti-pattern can also make it non-obvious what a symbol is when reading code: is it a local variable ? an argument ? a property ? something else ?
This can be alleviated by prefixing symbol names, but there will still remain a degree of ambiguity about variable type and scope (esp with nested functions, anonymous procs, where the prefixing is not enough).

Bringing the declaration close to usage just cleans everything up, and makes local portion of code readable and understandable in isolation, which is a huge boon.

5. Avoiding uninitialized variables

I'm placing this last because the compiler can help here, so it's not always fatal. Though there will still be a degree of ambiguity when a variable is passed by reference, or when a variable goes out of logic scope, since it can't get out of compiler scope.

Using nested functions is the only mitigation with legacy Pascal, but it's like using a hammer to swipe a mosquito, and it can be detrimental to readability, as it will break the flow of code.


Final word

Yes, inline variables place an extra burden on IDE and debug tools, but they also lift burdens from the shoulders of developers, especially when reading and debugging code.

For full disclosure, I've been using inline variables declarations in Pascal for 15 years, since I introduced them in DWScript. It took a long time for Delphi to catch, but that was years ago.
Hoping for FreePascal to make its move!





MarkMLl

  • Hero Member
  • *****
  • Posts: 8000
Re: Making a plea :)
« Reply #1 on: October 03, 2024, 09:01:04 am »
I would strongly suggest that you change that silly subject line in the initial post: as it is it's most likely to get the message ignored (or deleted) as spam. If you edit it the forum software will pick up the change automatically.

The issue of inline variables has been discussed ad-nauseam. My solution would be to modify for-do and with-do to include them.

Not going to happen, core team is opposed.

MarkMLl
« Last Edit: October 03, 2024, 09:15:44 am by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 16139
  • Censorship about opinions does not belong here.
Re: Making a plea :)
« Reply #2 on: October 03, 2024, 09:51:00 am »
Inline variables are evil and the terminology is also wrong.
It is not inline at all, it opens a new codeblock. Where's the inline?
The inline is just breaking all the rules of separation of declaration and implementation.

The rest of your silly remarks are either already implemented or do not make any sense for a compiled language.

A more sensible request would be type inferance
« Last Edit: October 03, 2024, 10:03:29 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

AlexTP

  • Hero Member
  • *****
  • Posts: 2479
    • UVviewsoft
Re: Making a plea :)
« Reply #3 on: October 03, 2024, 10:27:48 am »
Inferring of variables types is a dangerous thing! Don't do it. LongInt / ShortInt / QWord may be misdetected, also all float types, also string types.

About other suggestions from 1st post: inline variables maybe can be added under Delphi mode and/or modeswitch.

ericg

  • New member
  • *
  • Posts: 7
Re: Making a plea :)
« Reply #4 on: October 03, 2024, 10:30:36 am »
The issue of inline variables has been discussed ad-nauseam. My solution would be to modify for-do and with-do to include them.

Not going to happen, core team is opposed.

That's why it's a plea :)

The issue here isn't just a small patch in a corner-case (for-do) or statements that IMHO are probably best to obsolete (with-do), but compatibility with the other Pascal dialects, and especially the elephant in the room.

TBH I'm quite open to the possibility of forking the code, maintaining that fork, and letting developers make their choice.
I'm certainly not going to try arguing endlessly, it's just a plea in good faith.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11930
  • FPC developer.
Re: Making a plea :)
« Reply #5 on: October 03, 2024, 10:53:04 am »
The post is conflating inline variable and inferring types. 

Those are separate features, and the fact that OP doesn't understand that is telling.

I also note that your two posts are also contradictory. The first post talks about how the features are good, the second is about compatibility. 

Core is aware of all reasons, and the last time when all members were asked opinion on it, it was decided to deny the features for the time being, to be re-evaluated at some later time, both due to the work and that nobody really wanted it. Also the most likely implementor (Pascaldragon, who does most new language feature) seemed to be against it, so even if the opinion swung, the question remained who was going to do it.

If compatibility is the issue, I suggest to not use the feature in those other languages for the time being. Simply because it probably is too big a feature to be ready and tested for the next major version (what is now trunk), so that likely puts this feature coming into user's hands at least 5-7 years away. And then I'm already being charitable that core would be compelled by your arguments. (which was not the case the last time it was brought up on the sides)

My personal opinion is that it is ugly and redundant, and the benefits are mostly cobbled together fluff. For debunking, see earlier discussions on this forum. The only reason it exists seems to be that Embarcadero desperately wanted to look "modern", whatever that is. 
« Last Edit: October 03, 2024, 11:08:47 am by marcov »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8000
Re: Making a plea :)
« Reply #6 on: October 03, 2024, 11:35:33 am »
Inferring of variables types is a dangerous thing! Don't do it. LongInt / ShortInt / QWord may be misdetected, also all float types, also string types.

I agree. Pascal is already far too lax when it comes to silently promoting (numeric) types IMO.

There's also related problems keeping track of the size of a pointer referent, but just about every language is sloppy at that as is the x86 architecture where (working from memory) the bounds check opcode took a constant parameter so there was no way of saying "the buffer at this address now has /this/ actual size".

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: Making a plea :)
« Reply #7 on: October 03, 2024, 01:09:29 pm »
Also the most likely implementor (Pascaldragon, who does most new language feature) seemed to be against it, so even if the opinion swung, the question remained who was going to do it.

And I'm still against it.

My personal opinion is that it is ugly and redundant, and the benefits are mostly cobbled together fluff. For debunking, see earlier discussions on this forum. The only reason it exists seems to be that Embarcadero desperately wanted to look "modern", whatever that is.

That's part of my reasons as well.

cdbc

  • Hero Member
  • *****
  • Posts: 1644
    • http://www.cdbc.dk
Re: Making a plea :)
« Reply #8 on: October 03, 2024, 01:15:12 pm »
Hi
@Marcov & Sven: +1
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

MarkMLl

  • Hero Member
  • *****
  • Posts: 8000
Re: Making a plea :)
« Reply #9 on: October 03, 2024, 01:18:34 pm »
Also the most likely implementor (Pascaldragon, who does most new language feature) seemed to be against it, so even if the opinion swung, the question remained who was going to do it.

And I'm still against it.

And I for one respect that, even if I disagree: the amount of work you've put into the compiler gives you precedence.

As anybody who's read my notes on some of the more obscure bits of Pascal's apparent history probably appreciates, if I were to work on something comparable I'd start at a far lower level.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Ryan J

  • Full Member
  • ***
  • Posts: 138
Re: Making a plea :)
« Reply #10 on: October 03, 2024, 01:27:23 pm »
My personal opinion is that it is ugly and redundant, and the benefits are mostly cobbled together fluff. For debunking, see earlier discussions on this forum. The only reason it exists seems to be that Embarcadero desperately wanted to look "modern", whatever that is.

I've never seen another language with a requirement to pre declare variables outside of the code. Pascal is left alone in this regard and all programmers have been doing it differently when they use other languages. Never heard of people wanting this feature in other languages so I think Pascal simply lost the argument in the broader field.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5750
  • Compiler Developer
Re: Making a plea :)
« Reply #11 on: October 03, 2024, 01:40:47 pm »
My personal opinion is that it is ugly and redundant, and the benefits are mostly cobbled together fluff. For debunking, see earlier discussions on this forum. The only reason it exists seems to be that Embarcadero desperately wanted to look "modern", whatever that is.

I've never seen another language with a requirement to pre declare variables outside of the code. Pascal is left alone in this regard and all programmers have been doing it differently when they use other languages. Never heard of people wanting this feature in other languages so I think Pascal simply lost the argument in the broader field.

And yet it's one of the basic principles of the language Pascal, one of the core principles. To give that up would mean, why use Pascal at all?

Also your assumption is wrong: e.g. in the Linux kernel devs are required to do declaration first despite GCC allowing differently, because it improves maintainence. They'd definitely benefit from a "feature" that enforces declaration first.

440bx

  • Hero Member
  • *****
  • Posts: 4727
Re: Making a plea :)
« Reply #12 on: October 03, 2024, 01:53:23 pm »
e.g. in the Linux kernel devs are required to do declaration first despite GCC allowing differently, because it improves maintainence. They'd definitely benefit from a "feature" that enforces declaration first.
First, I am definitely _not_ in favor of inline variables but, there is at least one case in which they enable a programmer to write cleaner and simpler code and that is when using an "on the fly"/temporary variable makes it possible to break a complex expression into 2 or more simple ones whose results exist only until the value of the complex expression is determined.

Anonymous functions/procedure come somewhat close to providing such a facility but, they have a number of undesirable downsides that could easily be eliminated.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

zamtmn

  • Hero Member
  • *****
  • Posts: 640
Re: Making a plea :)
« Reply #13 on: October 03, 2024, 01:59:30 pm »
Everyone will decide for themselves whether to use or not to use. But the question of compatibility with Delphi is up to you PascalDragon

MarkMLl

  • Hero Member
  • *****
  • Posts: 8000
Re: Making a plea :)
« Reply #14 on: October 03, 2024, 02:02:32 pm »
I've never seen another language with a requirement to pre declare variables outside of the code. Pascal is left alone in this regard and all programmers have been doing it differently when they use other languages. Never heard of people wanting this feature in other languages so I think Pascal simply lost the argument in the broader field.

In that case I suspect that you haven't looked very hard- particularly when you consider what languages were available when Pascal was first implemented.

However there are three cases here, and you need to consider them separately.

* No declaration at all, type automatically inferred from name or context: that's a no-no for any reputable descendant of Pascal.

* Declaration before use: even C does that.

* Declaration after block-start but before any code: ALGOL-style.

* Declaration before block-start: Pascal-style but also COBOL etc.

The one thing I don't know is whether ALGOL-60 and ALGOL-W, Pascal's predecessors, hewed to the second or third of those.

With respect to PascalDragon and the other developers- I am /really/ not trying to wind them up by repeating this- what I've suggested in the past was a very limited introduction of locally-scoped variable declaration within the for-do or with-do syntax. Anything else just ain't Pascal.

MarkMLl
« Last Edit: October 03, 2024, 02:04:13 pm by MarkMLl »
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018