* * *

Author Topic: Ada style "for" loop counters  (Read 17497 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 626
    • Burdjia
Re: Ada style "for" loop counters
« Reply #90 on: February 15, 2016, 06:07:04 pm »
If you like ADA, use ADA. 8)

BeniBela

  • Hero Member
  • *****
  • Posts: 555
    • homepage
Re: Ada style "for" loop counters
« Reply #91 on: December 18, 2016, 01:53:21 pm »
And now my program crashed again, because there are loop-scoped variables.

I had  a function like

Code: Pascal  [Select]
  1. var i: integer;
  2. ...
  3. for i := 0 to high(anArray) do
  4.    dosomething(anArray[i], anotherArray[i]);
  5.  
  6. dosomething(somethingElse, anotherArray[i]);
  7.  

copy/pasted dosomething. The value of i seems to have changed between fpc versions.

If it was

Code: Pascal  [Select]
  1. for var i := 0 to high(anArray) do
  2.    dosomething(anArray[i], anotherArray[i]);
  3. ...
  4.  

it would have been an obvious compile error.

Now it does not even give a hint.

zeljko

  • Hero Member
  • *****
  • Posts: 913
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Ada style "for" loop counters
« Reply #92 on: December 18, 2016, 02:30:45 pm »
Never use for variable after the loop.

This is bad:
  var i: integer;
begin
  for i := 0 to high(anArray) do
    dosomething(anArray, anotherArray);
  dosomething(somethingElse, anotherArray);
end;

This is bad:
  var i,x: integer;
begin
  x := -1;
  for i := 0 to high(anArray) do
  begin
    dosomething(anArray, anotherArray);
    x := i;
  end;
  if x >= 0 then
    dosomething(somethingElse, anotherArray
  • );

end;

Thaddy

  • Hero Member
  • *****
  • Posts: 3971
Re: Ada style "for" loop counters
« Reply #93 on: December 18, 2016, 03:15:35 pm »
Now it does not even give a hint.
Why a hint?
You instructed to compiler to -- from? Why would the index  be valid after that? It is not an index anymore.
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

Leledumbo

  • Hero Member
  • *****
  • Posts: 7604
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Ada style "for" loop counters
« Reply #94 on: December 18, 2016, 03:26:52 pm »
And now my program crashed again, because there are loop-scoped variables.

I had  a function like
...
copy/pasted dosomething. The value of i seems to have changed between fpc versions.
Page 57:
"After a for-statement is executed, other than being left by a goto-statement, the control-variable shall be undefined."
It was and is designed that way, it never changes. It's also never a problem unless you think that all repetitive statements are equal, i.e. transformable from one to another within the same type value boundary, because they are not in Pascal world.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5529
Re: Ada style "for" loop counters
« Reply #95 on: December 18, 2016, 03:57:10 pm »
Note that the compiler "could" see that a variable is read that is not defined at that point and emit a warning.

The compiler is not perfect and certainly could have some polishing here and there. But not every little flaw means that the syntax should be blamed and radically changed.

BeniBela

  • Hero Member
  • *****
  • Posts: 555
    • homepage
Re: Ada style "for" loop counters
« Reply #96 on: December 18, 2016, 06:22:35 pm »
Why would the index  be valid after that? It is not an index anymore.

That is why fpc should not compile it happily

Note that the compiler "could"should see that a variable is read that is not defined at that point and emit a warning.

 

ftfy

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5529
Re: Ada style "for" loop counters
« Reply #97 on: December 18, 2016, 06:50:06 pm »
Note that the compiler "could"should see that a variable is read that is not defined at that point and emit a warning.
ftfy

Well, everybody says that about any problem they have. But if you want to hurry up, start creating a patch.

BeniBela

  • Hero Member
  • *****
  • Posts: 555
    • homepage
Re: Ada style "for" loop counters
« Reply #98 on: December 19, 2016, 08:33:01 pm »
When there are ada-like patches they are not merged anyways (like the ifthen intrinsic debacle)...

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 5529
Re: Ada style "for" loop counters
« Reply #99 on: December 19, 2016, 10:32:19 pm »
When there are ada-like patches they are not merged anyways (like the ifthen intrinsic debacle)...

I meant a patch to add the warning, not the Ada syntax (which I think is pointless to introduce that now, it will only cause more confusion and dialectitis)

z505

  • New member
  • *
  • Posts: 38
  • think first, code after
Re: Ada style "for" loop counters
« Reply #100 on: May 12, 2017, 11:40:45 am »
If you like ADA, use ADA. 8)

Or you can use GoLang which allows you to use variables automagically without declaring them first...

My experience with GoLang however tells me that GoLang code is much easier to write, than it is to read. Whenever I am looking at someone's golang code there are virtually no declarations of local variables anywhere (well there are some in rare occasions) and this makes the code much harder to read because you have no clue what type a variable is, for example when a function returns a value.. You have to go to the documentation to see what the function returns as a type, because it's not declared in the program.

This is a double edged sword. Makes programs easier to write, but harder to read.

But for local loop counters, everyone knows what the loop counter "i" is supposed to be for so I can see your point about not declaring it, but I find this such a small issue in programming that ... "is it worth the hassle of implementing such a small feature that makes programming maybe less than 0.000001 percent easier on users" when in fact like Marcov said, a patch could be added to the compiler to warn/forbid people from using "i" loop counters again in the same procedure further down?
think first, code after

Leledumbo

  • Hero Member
  • *****
  • Posts: 7604
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Ada style "for" loop counters
« Reply #101 on: May 13, 2017, 08:12:19 pm »
[My experience with GoLang however tells me that GoLang code is much easier to write, than it is to read. Whenever I am looking at someone's golang code there are virtually no declarations of local variables anywhere (well there are some in rare occasions) and this makes the code much harder to read because you have no clue what type a variable is, for example when a function returns a value.. You have to go to the documentation to see what the function returns as a type, because it's not declared in the program.
Add "no idea what interfaces this struct implement" to the list.

Thaddy

  • Hero Member
  • *****
  • Posts: 3971
Re: Ada style "for" loop counters
« Reply #102 on: May 13, 2017, 09:48:06 pm »
But for local loop counters, everyone knows what the loop counter "i" is supposed to be for so I can see your point about not declaring it
Well, if the compiler knows about i and about the scope you can even skip it's declaration completely by using for in do... Which implies i... 8-)
« Last Edit: May 13, 2017, 09:50:14 pm by Thaddy »
"Logically, no number of positive outcomes at the level of experimental testing can confirm a scientific theory, but a single counterexample is logically decisive."

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus