views:

375

answers:

4

I am working with Delphi. Does it make any difference in performance if we write if condition in different ways? For example:

if (condition) then
   someVar := someVal
else
   someVar := someOtherVal;  

Or we can write:

if (condition) then begin
   someVar := someVal;
end else begin
   someVar := someOtherVal;
end;  

I prefer the second option just because it is looks better than the first.

+3  A: 

This will not make a difference in performance.

begin and end tell the compiler where a block of code starts and finishes, but no computation needs to be done there.

Miel
+18  A: 

No, there is no difference in performance, the code created will be identical.

An aspect that might be more important than that the second option looks nicer, is that it is better for maintainence. If you need to add another statement in the else block, you will not accidentally forget to add the begin and end, which would put the statement outside the if and always be executed.

Guffa
@Guffa Good Point... +1
Himadri
+1 for the maintenance tip.
Frank Shearar
If you use proper indentation and know what you are doing, how could you possible "accidentally forget to add the begin and end"? I could spot missing `begin`s and `end`s in my sleep!
Andreas Rejbrand
@Andreas: Everyone is not as sharp as you are, and most people have those off moments that you seem to never have. ;) Writing maintainable code is not about writing something that you can maintain, but writing something that most programmers can maintain. My favorite quite come to mind: http://stackoverflow.com/questions/1103299/help-me-understand-this-brian-kernighan-quote
Guffa
People who never make mistakes, by their own accounting... .... well. There are some accounting flaws, for sure. :-)
Warren P
+2  A: 

The only thing you should keep in mind about if-elseif-else is to keep the common cases up in your code before edge cases, so that the least possible conditions are evaluated.

galambalazs
Are you sure that you read the question?
Andreas Rejbrand
@galambalazs Yes, that can be helpful in performance issue... Should not get -1.
Himadri
The answer has been edited. I would remove my -1 if only the server let me...
Andreas Rejbrand
never mind.. :)
galambalazs
A: 

Begin and End do not slow down your code, as others have already said. I am writing another answer to encourage you even more explicitly to ALWAYS use begin and end whenever you could use them.

It is good to be liberal with using Begin and End, and not worry about them slowing you down (because they don't).

If you go the other way, and leave out begin and end wherever you can, you get into a different type of trouble.

This has happened to me lots. You can get in trouble when you insert a line into a place where no begin and end statement exist. You then end up scratching your head wondering what you did that broke your code. Begin-end-everywhere, even where not needed, is standard operating procedure for a lot of Delphi coders.

Warren P
I never use `begin ... end` when they are not necessary. This makes the code a lot more readable. I have never encountered a problem with this - no bugs, no head-scratching, no problems. I cannot see any reason for an experienced Delphi developer not to skip unnecessary`begin ... end`s. If you only indent your code correctly, and are aware of the phenomenon of the "dangling else", then nothing can go wrong. (I have developed in Delphi since the age of 12, so I know the langauge instinctively.)
Andreas Rejbrand
I can see at least one reason why an experienced Delphi developer might use unnecessary begin...end: He might have coworkers that are not as experienced.(And besides: I think of myself as quite experienced and I actually do use them quite often. Maybe I am not as experienced as I think or maybe your statement is just wrong.)
dummzeuch
As anyone reading can see, people disagree, and the matter is very very small, and of little final significance. You must learn to fix the problem of the dangling-else when it occurs, and you must do what you think is best, when you write your code. When you work with a team of developers, hopefully you can all come to an agreement.
Warren P