Forum > FPC development

Variables everywhere - is it good idea?

<< < (6/6)

Ñuño_Martínez:
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...

User137:

--- Quote from: skalogryz on August 14, 2015, 07:41:17 pm ---for example

--- Code: ---for i:=0 to list.Count-1 do
  ...

--- End code ---
and

--- Code: ---i:=0;
while i<list.Count-1 do
  ..

--- End code ---
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.
--- End quote ---
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: ---for i:=0 to list.Count-1 do
--- End code ---

--- Code: ---var _for_max: integer;
_for_max:=list.Count-1;
i:=0;
while (i <= _for_max) do begin
  ...
  inc(i);
end;
--- End code ---
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.

skalogryz:

--- Quote from: User137 on August 17, 2015, 09:35:44 pm ---So is Pascal allocating more memory for the for-loops? IE:

--- End quote ---
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.

skalogryz:

--- Quote from: User137 on August 17, 2015, 09:35:44 pm ---Also it's babysitting the programmer.

--- End quote ---
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).

Navigation

[0] Message Index

[*] Previous page

Go to full version