Recent

Author Topic: Variables everywhere - is it good idea?  (Read 16084 times)

moskalenco_a

  • New Member
  • *
  • Posts: 19
Variables everywhere - is it good idea?
« on: March 15, 2015, 08:02:45 pm »
Hi all.What do you think about this idea?Example,
Code: [Select]
begin
var a:Integer;
a:=0;
for var i:=1 to 10 do
 begin
  var c:=i*2;
  a:=a+c;
 end;
Writeln(a);
Readln;
end.
Is it good idea or is it bad?

Never

  • Sr. Member
  • ****
  • Posts: 409
  • OS:Win7 64bit / Lazarus 1.4
Re: Variables everywhere - is it good idea?
« Reply #1 on: March 15, 2015, 08:10:38 pm »
these expresions are currently illegal the above code will not compile...
if this is a suggestion imho : no thank you very much
this a nice way to lose the eggs and the basket in 10 lines of code
Νέπε Λάζαρε λάγγεψων οξωκά ο φίλοσ'ς αραεύσε

moskalenco_a

  • New Member
  • *
  • Posts: 19
Re: Variables everywhere - is it good idea?
« Reply #2 on: March 15, 2015, 08:12:21 pm »
Yes,it's suggestion.

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
Re: Variables everywhere - is it good idea?
« Reply #3 on: March 15, 2015, 08:37:18 pm »
This is useless. In a construct like

for var i := 1 to 10 do

what is "var" good for ? It has no meaning. In Basic, you can omit it competely

for i = 1 to 10 do ...

In C, you need to specify the type of variable

for (int i=1; i <=10; i++)

Pascal forces you to specify it beforehand, and that is intended.

Code: [Select]
var a,i,c:Integer;
begin
a:=0;
for i:=1 to 10 do
 begin
  c:=i*2;
  a:=a+c;
 end;
Writeln(a);
Readln;
end.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Variables everywhere - is it good idea?
« Reply #4 on: March 15, 2015, 10:15:05 pm »
It can be used to disambiguate that you really want to create a variable in that scope, overriding a possible local variable called I.

That being said with most current IDE having shortcuts to properly declare variables, I think it is an outdated concept.

It was probably important to micromanage stack depth in the C of 1970, but nowadays barely anything is dynamically allocated from the stack anymore (alloca in C), and most big types are not allocated from the stack.

Nitorami

  • Sr. Member
  • ****
  • Posts: 496
Re: Variables everywhere - is it good idea?
« Reply #5 on: March 15, 2015, 10:52:07 pm »
Interesting. But within a function or procedure, any var declaration is allocated on the stack, isn't it ? What is the difference to alloca then ?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Variables everywhere - is it good idea?
« Reply #6 on: March 16, 2015, 04:40:46 am »
Hi all.What do you think about this idea?Example,
Code: [Select]
begin
var a:Integer;
a:=0;
for var i:=1 to 10 do
 begin
  var c:=i*2;
  a:=a+c;
 end;
Writeln(a);
Readln;
end.
Is it good idea or is it bad?
Bad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: Variables everywhere - is it good idea?
« Reply #7 on: March 16, 2015, 05:27:37 am »
Interesting. But within a function or procedure, any var declaration is allocated on the stack, isn't it ? What is the difference to alloca then ?

There is no need to actually also implement it in a nested way. (since that would only generate more instructions). And afaik alloca can take a variable count.

alcalde

  • Newbie
  • Posts: 3
Re: Variables everywhere - is it good idea?
« Reply #8 on: August 09, 2015, 10:02:39 pm »
Hi all.What do you think about this idea?Example,
Code: [Select]
begin
var a:Integer;
a:=0;
for var i:=1 to 10 do
 begin
  var c:=i*2;
  a:=a+c;
 end;
Writeln(a);
Readln;
end.
Is it good idea or is it bad?

If anything it's still too verbose.  :D There was no need to even declare the "a" variable first since it was assigned the value 0. But otherwise, yes, it's a good first step towards bringing Pascal up to date with modern "best practices" in language design.. I can dig up a computer science paper in which it states that "it's now universally accepted that variables should be declared as close as possible to where they were first used". Pascal's declare everything ahead syntax was motivated by Wirth's single-pass compiler design, which was itself motivated by extreme memory limitations at the time. As he put it, when he designed Pascal, "the memory of mainframes was measured in kilobytes".  Over the years Pascal users have invented all sorts of reasons why things were designed the way they were, but the reality is generally that the oddities of Pascal were all the result of the single pass compiler, not because they conferred some sort of benefit to coding.


alcalde

  • Newbie
  • Posts: 3
Re: Variables everywhere - is it good idea?
« Reply #9 on: August 09, 2015, 10:17:07 pm »
Bad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though.

Why do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings).

If you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre-defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it. If you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley-Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Variables everywhere - is it good idea?
« Reply #10 on: August 09, 2015, 10:43:17 pm »
Bad, but if you want, try GNU Pascal. They already have it implemented. I don't think FPC will ever implement it, though.

Why do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings).

If you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre-defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it. If you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley-Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from?
how about design first code later? If you already have a solid design then where the variables are declared should not be an issue if on the other hand you are hacking away trying to come to a solution from your keyboard then yes declaring when you need it or even never declaring it and letting the first assignment define its data type should help a lot, then again why would I want to use such code?
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Michl

  • Full Member
  • ***
  • Posts: 226
Re: Variables everywhere - is it good idea?
« Reply #11 on: August 09, 2015, 11:40:47 pm »
If you use Lazarus and you don't want to step to the declaration, you can use the code completion.

For example write:
Quote
procedure TForm1.Button1Click(Sender: TObject);
begin
  for i:=1 to 10 do
end; 
Step back to the "i" and press [CTRL] + [SHIFT] + [C] and see the magic :)
Code: [Select]
type
  TLiveSelection = (lsMoney, lsChilds, lsTime);
  TLive = Array[0..1] of TLiveSelection;

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Variables everywhere - is it good idea?
« Reply #12 on: August 10, 2015, 04:18:51 am »
Hi all.What do you think about this idea?Example,
Code: [Select]
begin
var a:Integer;
a:=0;
for var i:=1 to 10 do
 begin
  var c:=i*2;
  a:=a+c;
 end;
Writeln(a);
Readln;
end.
Is it good idea or is it bad?

IMHO, it's a modern feature in an antique programming language. It has not the spirit of Pascal but could be useful, like some other modern additions.

Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Basile B.

  • Guest
Re: Variables everywhere - is it good idea?
« Reply #13 on: August 10, 2015, 05:11:20 am »
DWS has it. (https://www.delphitools.info/dwscript). Using var as keyword seems to be motivated to avoid the proliferation of keywords. Actually it wouldn't be a problem since inside a function body var is currently illegal.

But if it has to be done, let's open the breach fully: not only for to, for downto, but also for in and every local declaration.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Variables everywhere - is it good idea?
« Reply #14 on: August 10, 2015, 07:18:47 am »
Why do you believe it's bad? I've done a formal review of languages for two start-ups, and I don't recall seeing a single statically-typed language created in the 21st century (2000-) that didn't use type inference. In computer science it's considered universally true that a variable should be declared as close as possible to where it is first used (in Pascal it's the old page-up, page-up, page-up... sigh). Pascal's declare-everything-upfront design was necessitated by Wirth's use of a single-pass compiler design to save memory. There's simply no need in 2015 to clutter code by pedantically declaring every variable and the good old page-up, page-down (no, IDE improvements should not be used to cover up language shortcomings).
I'm a supporter of such a construct for the sake of readability. It's not only for the compiler, but more for the human. It reduces reading complexity due to mixed declaration-execution, which quite the contrary with your saying, clutters the code more than separated declaration-execution. Page up-page down is better since it's consistent, rather than unpredictable up-down because the variable could be declared nearly everywhere.
If you think of languages as organisms that evolve and pass on their good ideas to future languages, then the notion of defining every variable in a pre-defined section was an evolutionary dead end. Other than COBOL and close Pascal cousins ADA and Eiffel, absolutely no language in decades adopted the notion. Certainly no popular language today uses it.
So what? I'm not making Pascal another language. This concept is good, no popular uses it is another thing. Who says popular = better?
If you were trying to convince a C++, Ruby, Java developer to use Pascal, how would you answer them when they asked why they had to declare all of their variables beforehand, especially when they point out that the Handley-Millner type inference algorithm has existed for 30 years? And if we can't answer, where does the next generation of Pascal developers come from?
I won't try to convince them to use their other-language-mind to code Pascal. If you want to code in Pascal, think in Pascal. I accept the lack of static checking and undetectable variable shadowing in PHP/Ruby/Lua/any language without variable declaration and dynamic typing, I accept the lack of simple procedural coding when coding short simple programs in Java, I accept the lack of modular programming when coding in C/C++, why shouldn't they accept Pascal concept if they have to code in Pascal? FYI, Handley-Millner type inference algorithm is not perfect for imperative languages. In certain simple cases, they might be applicable. But the lack of type inference again improves readability because readers quickly know what the type of a variable is. With type inference, only the compiler knows, at least without analyzing the code deep enough. At the very least one must read documentation or even actual source code.
Code: [Select]
x := StringsReplace(SomeText,['a','b'],['x','y'],[rfReplaceAll]);
Without ever knowing what StringsReplace is, how do you know the type of x? How are you sure that it returns replaced copy of the string instead of returning the number of successful replacements with the string itself is modified inplace?
« Last Edit: August 10, 2015, 10:57:53 am by Leledumbo »

 

TinyPortal © 2005-2018