Sergey Zuyev said:
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)
G'day. Statement1 and Statement2 do not do the same thing. Statement2 is a
nice, easy way to test all four possible conditions. However, if you only
want to test three of the four conditions, then you'd need to use
Statement1. There are many ways to re-code your two statements. Here's a
couple I'd use:
'Statement 1 - Testing 3 of 4 possibilities
If Not (Me.CheckBoxPrimaryYN.Checked = True And dr.PrimarySet = 0) Then
If Me.CheckBoxPrimaryYN.Checked = False And dr.PrimarySet = 1 Then
Me.CheckBoxPrimaryYN.Enabled = False
Else
Me.CheckBoxPrimaryYN.Enabled = True
End If
End If
'Statement2 - Testing all 4 possibilities
If Me.CheckBoxPrimaryYN.Checked = False And dr.PrimarySet = 1 Then
Me.CheckBoxPrimaryYN.Enabled = False
Else
Me.CheckBoxPrimaryYN.Enabled = True
End If
Cheers,
Greg