Re: Coding style



I would typically prefer the condensed version #2 as well, potentially with one of the logic extensions mentioned before. The big downside of the first method (nested if's), other than maintainability is the possibility that you won't reset the enabled value on one of the else blocks. In your sample code, I believe the (Me.CheckBoxPrimaryYN.Checked = False) evaluation won't reset the enabled status if it is not checked. This may be by design, but I have seen the case often enough that it is simply an oversight when the direct assignment of option 2 wouldn't have that problem which helps to facilitate the maintainability and reliability of the code.

One additional thing to consider which may optomize the expression is to use logical short-circuting (AndAlso/OrElse) rather than the simple logical evaluation.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

"Sergey Zuyev" <SergeyZuyev@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:FBCFC5BE-9706-4BFB-A6BC-2D5A95B54B0F@xxxxxxxxxxxxxxxx

Hello All
I work at software company and we are having a really big discussion
about
coding styles and it seems that more people
prefer statement 1 to statement2 , which was a big surprise to me.
Please
help us with your comments. Thanks
Which way is better way 1 or 2?

1. 'set enable status on CheckboxPrimaryYN

If (Me.CheckBoxPrimaryYN.Checked = True) And (dr.PrimarySet > 0) Then

Me.CheckBoxPrimaryYN.Enabled = True

Else

If (Me.CheckBoxPrimaryYN.Checked = False) Then

If (dr.PrimarySet = 0) Then

Me.CheckBoxPrimaryYN.Enabled = True

Else

Me.CheckBoxPrimaryYN.Enabled = False

End If

End If

End If

2. 'set enable status on CheckboxPrimaryYN

Me.CheckBoxPrimaryYN.Enabled = Not (Me.CheckBoxPrimaryYN.Checked =
False And dr.PrimarySet = 1)

-- Programmer

And it doesn't seem fair that you used so much wording for #1 when #1
could also have been written as follows:

If CheckBoxPrimaryYN.Checked AndAlso dr.PrimarySet > 0
CheckBoxPrimaryYN.Enabled = True
ElseIf Not CheckBoxPrimaryYN.Checked
If dr.PrimarySet = 0
CheckBoxPrimaryYN.Enabled = True
Else
CheckBoxPrimaryYN.Enabled = False
End If
End If
Or I would have even taken the mix of both to simplify:

#3
If CheckBoxPrimaryYN.Checked AndAlso dr.PrimarySet > 0
CheckBoxPrimaryYN.Enabled = True
ElseIf Not CheckBoxPrimaryYN.Checked
CheckBoxPrimaryYN.Enabled = dr.PrimarySet = 0
End If
The reason behind why so many people would prefer #1 over #2 is
simplification. Otherwords, it's easier to follow #1 than it is #2
(because the if's are broken down and not compacted onto a single
line). People can follow the if's easier and get the big picture
rather than having them compressed onto a single line and have to use
more brainpower to expand the logic.

HTH :)

Mythran



.


Loading