Where do you put the 'Else'?

  • Thread starter Thread starter Fred Smith
  • Start date Start date
F

Fred Smith

After an If statement, I indent all subsequent statements one tab until the
End If. However, I'm struggling with where to put the Else statement. I'd
like to know what convention other programmers follow. Do you:

1. Indent it with the other statements, even though it might be difficult to
find?
2. Keep it in line with the IF statement?
3. Split the difference, and indent it two spaces rather than four?

Thanks for your feedback,
Fred
 
Fred,

IDEs typically have the ability to understand code syntax. For instance,
functions would be highlighted with one color and parameters another. Same
goes for indenting. They automatically indent the code between the beginning
and end of any conditional statement or loop. If there's an ELSE in the
condition, the IDE will keep it inline with the IF. Obviously, all of the
syntax highlighting and indenting is only for the benefit of the programmer.
It has no affect on the way the code executes.

For VBA, some syntax highlighting is done, and there's an AutoIndent
CheckBox in the VBA Editor Options (but I've never actually seen any
difference with it Checked or unChecked!).

Cheers,
Lee
 
lgbjr said:
Fred,

IDEs typically have the ability to understand code syntax. For instance,
functions would be highlighted with one color and parameters another. Same
goes for indenting. They automatically indent the code between the beginning
and end of any conditional statement or loop. If there's an ELSE in the
condition, the IDE will keep it inline with the IF. Obviously, all of the
syntax highlighting and indenting is only for the benefit of the programmer.
It has no affect on the way the code executes.

VBA doesn't align the Else with the IF automatically it aligns to the
previous line. That would be useful.
For VBA, some syntax highlighting is done, and there's an AutoIndent
CheckBox in the VBA Editor Options (but I've never actually seen any
difference with it Checked or unChecked!).

Have you tried it? Unchecking it for me ignores any indenting and starts in
column 1.
 
Fred,

Like Robin I align the IF ... Else, like so

If Condition Then
'do your stuff
ElseIf Condition2 Then
'do something else
Else
If Suib - Condition Then
'do yet more
Else
'but only if condition2 is met
End If
End If

Personally, I never use code like so

If a = b Then c = d

I always use

If a = b Then
c = d
End If

as I feel that the former can confuse in a lot of code, you go looking for
an EndIf that isn't there.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top