Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-09

Nesting

4 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Juha Manninen (gmail

A new Lazarus review :
http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or- failure/

has this comment about Lazarus source:
---
Abundant use of the Exit() command instead of nesting code in If/then/else. It has been proven (last time in Delphi Informant Magazine) that allowing a method to nest itself out makes faster code. It is also easier to read and study. Exit should of course be used (it must be used in many situations) but prudently.
---

Does nesting really create faster code?

For readability I like the nesting style, except when there are very many such tests in one function.

Juha

Dimitri Smits
Show quoted text 18 lines
A new Lazarus review : http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or- failure/ has this comment about Lazarus source: --- Abundant use of the Exit() command instead of nesting code in If/then/else. It has been proven (last time in Delphi Informant Magazine) that allowing a method to nest itself out makes faster code. It is also easier to read and study. Exit should of course be used (it must be used in many situations) but prudently. --- Does nesting really create faster code?

not really tested, but think about the following:
1) with nesting:
you validate an expression on being true/false. According to the result, you jump over a return statement of over a jmp statement to the cleanup block. (if not optimized by compiler) So, you ALWAYS jump/call.
2) without nesting:
you validate an expression on being true/false. According to the result, you jump to the clean-up block. Otherwise you continue.

I haven't studied processors that intimately on their branch-prediction beyond the original Pentium (586), but there it could have 'significant' repercussions with pipeline-stalling resulting in a few clock-cycles extra for a jump statement (regardless if it was taken).

For readability I like the nesting style, except when there are very many such tests in one function.

that is a matter of taste. I find the *overuse* of exit somewhat bad programming. It is not really clear in a function/procedure all the time during debugging "why this piece of code is never visited" when you have a few of those blocks above the said "tested" statement. A remnant of C-style programmers? Ofcourse there are cases where you need to use it, but in most cases it is about writing the correct control-statement.

kind regards,
Dimitri Smits

Luiz Americo Pereira Camara
to Juha Manninen (gmail: A new Lazarus review : http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-su…
Juha Manninen wrote 16 lines
A new Lazarus review : http://delphimax.wordpress.com/2010/09/13/freepascal-and-lazarus-success-or- failure/ has this comment about Lazarus source: --- Abundant use of the Exit() command instead of nesting code in If/then/else. It has been proven (last time in Delphi Informant Magazine) that allowing a method to nest itself out makes faster code. It is also easier to read and study. Exit should of course be used (it must be used in many situations) but prudently. --- Does nesting really create faster code?

If i understand right what you mean by nesting, the produced code is equal at least in my simple test. See attached.

I dont know for more complex situations

Luiz

leledumbo
to Dimitri Smits: not really tested, but think about the following: 1) with nesting: you validate an…

I sometimes use that kind of Exit because it's cheaper in code, though it makes the code flow less structured. Probably because C family bad coding doctrine (my university is a C family slave). But when I'm in full consciousness, I use if/else.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Nesting-tp2837567p2838663.html Sent from the Free Pascal - General mailing list .