Recent

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

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Variables everywhere - is it good idea?
« Reply #15 on: August 10, 2015, 09:55:13 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).

In computer science it is also considered universally true that you should keep procedures and methods short, so there should be no need for page-up and page-down :-)

dieselfan

  • New Member
  • *
  • Posts: 16
Re: Variables everywhere - is it good idea?
« Reply #16 on: August 13, 2015, 01:01:46 pm »
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).
Hence intialisation SHOULD occur on first few lines ;). That would comply with what you learnt?
****
Var x: integer;
begin
  x := 0;
****

In computer science it is also considered universally true that you should keep procedures and methods short, so there should be no need for page-up and page-down :-)

Yes and in the OP's post there would be many "var" instances leading to longer code...vs just ONE above BEGIN. ;)
AMD 1800X
Manjaro KDE
Rarely Windows 10 Pro

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Variables everywhere - is it good idea?
« Reply #17 on: August 13, 2015, 06:01:09 pm »
All i know is, if such feature came to be i definitely would use it. Alot. Recently i'm dealing with C# since it's for commercial games with Unity, but this language feature is convenient.

Code: [Select]
for var i: integer:=0 to 9 do...
// Or
for integer i:=1 to 10 do

Just saying "for var i:=1 to 10 do" is impossibility, it has to be typed.
"for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too.

For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)"

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Variables everywhere - is it good idea?
« Reply #18 on: August 14, 2015, 11:03:23 am »
"for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too.

For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)"
That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Variables everywhere - is it good idea?
« Reply #19 on: August 14, 2015, 11:51:07 am »
That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around.

You obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general.

What you state is false.

If a loop can be determinded to end in finite space ..... Boogle or Ging on that one. Sigh. Mayor lack. I object to syntactic sugar. It isn't in Pascal, but it also isn't in COBOL(1959) or BASIC(1964) which are much older then Pascal(1971).

Regularly it is a processor feature/ assembler convention, like (x)cx on Intel. It's called a counter...
« Last Edit: August 14, 2015, 11:59:27 am by Thaddy »
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Variables everywhere - is it good idea?
« Reply #20 on: August 14, 2015, 12:58:00 pm »
You obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general.

What you state is false.
Quite the opposite, not only compiler history, I also learned compiler construction and programming language design. For loop in C family IS syntactic sugar:
Code: [Select]
for (init;cond;inc)  { body; }
is syntactically and semantically equivalent with:
Code: [Select]
init;
while (cond) { body; inc; }
Hence, you can't do:
Code: [Select]
unsigned char i;
for (i = 0; i <= 255; i++) { ...; }
above for loop goes infinitely or crash with overflow exception depending on the language. OTOH, while you can't do:
Code: [Select]
var b: Byte;
b := 0;
while b <= 255 do begin
  ...
  Inc(b);
end;
which will trigger range check error, you can do:
Code: [Select]
var b: Byte;
for b := 0 to 255 do ...;
which will perfectly runs the loop 256 times. Objection?

Chapter 6.8.3.9 of the ISO standard shall enlighten you about for and while loop equivalence in Pascal, and that's no syntactic sugar.
« Last Edit: August 14, 2015, 01:00:38 pm by Leledumbo »

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Variables everywhere - is it good idea?
« Reply #21 on: August 14, 2015, 04:55:19 pm »
That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around.

You obviously didn't study any compiler history. Or processor history for that matter. Or algorithms in general.

What you state is false.
>:D
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Variables everywhere - is it good idea?
« Reply #22 on: August 14, 2015, 07:04:05 pm »
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".
Actually it's going the opposite now. Specifically, because of the popularity of using closures.
Closure requires a declaration of the variable prior to the declaration of closure (anon. function)

That brings a developer to declare all (for consistency) variables in one place at the beggining of the code. The only exception is loop variables.
But due to informal convention of naming loop variables (i.e. "i", "j","k") it's normal to declare them ahead of the code.

Here's the most recent example I've touched. Example of dataTable.js code (jquery.dataTables.js):
Code: [Select]
/**
* Draw the table for the first time, adding all required features
*  @param {object} settings dataTables settings object
*  @memberof DataTable#oApi
*/
function _fnInitialise ( settings )
{
var i, iLen, iAjaxStart=settings.iInitDisplayStart;
var columns = settings.aoColumns, column;
var features = settings.oFeatures;

/* Ensure that the table data is fully initialised */
if ( ! settings.bInitialised ) {
setTimeout( function(){ _fnInitialise( settings ); }, 200 );
return;
}


lazjump

  • Jr. Member
  • **
  • Posts: 61
Re: Variables everywhere - is it good idea?
« Reply #23 on: August 14, 2015, 07:24:55 pm »
I read this thread and I appreciate how Leledumbo explains things clearly 
I thought Delphi was expensive until I learned the price of ExtJS

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Variables everywhere - is it good idea?
« Reply #24 on: August 14, 2015, 07:41:17 pm »
"for (int i=1; i<11; i++)" from C world the "int" is a specific type, not just general loop counter, and could be anything else too.

For who cares it could be "for (float i=1.5; i<12.33; i+=0.6)"
That's the difference with Pascal's for. In those languages, for loop is a syntactic sugar for while loop. In Pascal, for can do what while can't do, so does the other way around.
(Object) Pascal for-loop is not C for() in a few ways (where C for-loop is indeed a sugar over while):
* the number of iteration is estimated once and prior to execution of the loop. That might take effect on the efficiency and logic of the code.
for example
Code: [Select]
for i:=0 to list.Count-1 do
  ...
and
Code: [Select]
i:=0;
while i<list.Count-1 do
  ..
would be act differently if contents of list changes within the loop.
For - loop would execute the "count"-times that was estimated prior to the first iteration, and "while" would check the condition on each iteration of the loop. If "list.Count" is time consuming operation, then for-loop will be even faster.

* in Object Pascal it's not allowed to modify the loop-variable (it was allowed in Turbo Pascal for example)

* With two statements mentioned above, the compiler could optimize the usage of loop variable. For example, if loop variable is not used within the loop.
Code: [Select]
var
  a,b,c: integer;
begin
  for i:=0 to 1000 do   c:=a+b;
end;
The compiler could actually code the loop as following
Code: [Select]
  for i:=1000 downto 0 do c:=a+b;
The efficiency is gained (for certain CPUs) where comparison to zero instruction is faster than comparing two numbers.
Delphi does such optimization (that might freak-out unprepared developer debugging such optimized loop), FPC doesn't at the moment.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Variables everywhere - is it good idea?
« Reply #25 on: August 17, 2015, 07:46:17 pm »
As a former C++ programmer, I really discourage it.  It is confusing since you can declare a variable anywhere.

One of the advantages of Pascal is that declarations are in places you can find.  So you can find them.  I know, there are "search" buttons in any IDE but sometimes it's faster to scroll up some lines to the start of the code.

Etc...
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Variables everywhere - is it good idea?
« Reply #26 on: August 17, 2015, 09:35:44 pm »
for example
Code: [Select]
for i:=0 to list.Count-1 do
  ...
and
Code: [Select]
i:=0;
while i<list.Count-1 do
  ..
would be act differently if contents of list changes within the loop.
For - loop would execute the "count"-times that was estimated prior to the first iteration, and "while" would check the condition on each iteration of the loop. If "list.Count" is time consuming operation, then for-loop will be even faster.
In that Count case i definitely like the "while" case more. If you actually do delete item and try to access it inside, the program may crash for range error. Delete loops should be done with negative increment anyway, from count-1 to 0.

So is Pascal allocating more memory for the for-loops? IE:
Code: [Select]
for i:=0 to list.Count-1 do
Code: [Select]
var _for_max: integer;
_for_max:=list.Count-1;
i:=0;
while (i <= _for_max) do begin
  ...
  inc(i);
end;
That seems more instructions to me (but i can understand the .Count is a getter, so it may in worse case go through a function each loop. Then use temp variable to optimize manually.). Also it's babysitting the programmer.

I only mentioned the for-loop case for this thread since original poster started with it, it's only the tip of the iceberg. I like declaring all kinds of variables between code. Even still there is always need for pre-declared private variables.
« Last Edit: August 17, 2015, 09:37:53 pm by User137 »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Variables everywhere - is it good idea?
« Reply #27 on: August 17, 2015, 10:50:40 pm »
So is Pascal allocating more memory for the for-loops? IE:
Pascal does not, but a compiler could.
As well as it depends on number of factors, such as:
* code of the loop itself
* compiler code optimization selected
Typically a loop variables are not in a memory, but instead are kept in registers.
BUT if the code within a loop is huge, or nested loops are used, the value of the loop-variable should be stored in memory (i.e. on stack).

Pascal (language) has not requirements on how loop-variable should be allocated within the loop.

Thus using while you, might get more predictable code generated, but in some cases For-loop could be optimized by a compiler better (performance wise), such as for-unrolling and/or reverse index counting.
« Last Edit: August 17, 2015, 10:54:21 pm by skalogryz »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Variables everywhere - is it good idea?
« Reply #28 on: August 17, 2015, 10:52:29 pm »
Also it's babysitting the programmer.
You say that, as if it's something bad.

Languages/Compilers are doing that all of the time these days, unless you're writing asm/c/c++.
People are requesting to add more babysitting features into the languages, such as more ref-counting types, garbage collection, closures (for parallel processing in the first place).

 

TinyPortal © 2005-2018