473,473 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Coding style

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
Jul 21 '06
52 2926
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:eS**************@TK2MSFTNGP06.phx.gbl...
"Greg" <Gr**@no-reply.okschrieb:
>CheckBoxPrimaryYN.Enabled = IIf(Not CheckBoxPrimaryYN.Checked And
dr.PrimarySet, False, True)

I'd loose the unnecessary 'IIf' with its additional boxing overhead too,
because 'Not CheckBoxPrimaryNY.Checked AndAlso dr.PrimarySet = 1' already
returns a boolean value.
Thanks Herfried.

However, losing the IIf and using the AndAlso would then require an extra
Not. ie:

CheckBoxPrimaryYN.Enabled = Not (Not CheckBoxPrimaryYN.Checked AndAlso
dr.PrimarySet)

As the OP's question related to which statement is easier to read/maintain,
I'd prefer not to use double negatives.
If I was to pick up someone else's code and saw the code above, I'd have to
use a few brain cells to determine what it was trying to achieve. If,
however, I saw the code that Jonathan provided in an earlier post then I'd
have no trouble understanding what was happening.

So my answer to the OP is that I wouldn't use either of his Statement1 or
Statement2 suggestions. Instead, I'd use Jonathan's suggestion of:

If Not CheckBoxPrimaryYN.Checked And dr.PrimarySet Then
CheckBoxPrimaryYN.Enabled = False
Else
CheckBoxPrimaryYN.Enabled = True
End If

However, as the result of the If statement is only returning a single
action, I'd use the IIf statement.
If multiple actions were required as a result of the If statement, I'd
certainly choose Jonathan's format.

Cheers,
Greg.


Jul 27 '06 #51
Hi,

There are some discrepancies in the OP's examples. The conditions
stated in his first one aren't the same as those in the second.

According to the first example, we have the following conditions to
enable the checkbox:

a) It's checked and dr.PrimarySet 0
b) It's unchecked and dr.PrimarySet = 0

And we have this condition to disable the CheckBox:

c) It's unchecked and dr.PrimarySet <0

Notice that nothing is said about what to do if the checkbox is checked
but dr.PrimarySet <= 0. According to the code, we do nothing, that is,
we leave the checkbox.Enabled property in it's current state. Now, it
may be part of the logic of the application or an omission. We have no
way to tell, since we're not informed about the possible values of
dr.PrimarySet.

Consider now the second example. According to it, we Disable the
Checkbox if:

a) It's not Checked and dr.Primary = 1;

otherwise we enable it.

This states a completelly different logic from the 1st example, and
there's no way to compare both, even though they deal with the same
objects. We can only say that the second example has no relation to the
first, or one of them is a completely mistaken representation.

The OP says he prefers the second example, and issues a statement
similar to:

CheckBox.Enabled = Not( Not CheckBox.Checked _
AndAlso dr.PrimarySet = 1)

To simplify this expression eliminating the double negation, we may
take advantage of the fact that:

Not (A And B) -Not(A) Or Not(B)

Using our current expression (where A =CheckBox.Checked, B=>
dr.PrimarySet = 1):

Not(Not(A) And B)
-Not(Not(A)) Or Not(B)
-A Or Not B

Thus:

CheckBox.Enabled = CheckBox.Checked OrElse dr.PrimarySet <1
Now, if we were to simplify the logic of the first example, we should
decide what to do if the checkbox is checked and dr.PrimarySet <= 0.
I'll assume that when the OP stated dr.PrimarySet 0, he actually
wanted to say dr.PrimarySet <0. I will also assume that if the
Checkbox is set but dr.PrimarySet = 0, we must not mess with the
Enabled property. In other words, to set the Enabled property we'll
have to consider it's current value!

These are, thus, our options:

A) CheckBox.Checked And dr.PrimarySet <0 -Enabled = True
B) CheckBox.Checked And dr.PrimarySet = 0 -Enabled = Enabled
C) CheckBox.Unchecked and dr.PrimarySet = 0 -Enabled = True
D) CheckBox.Unchecked And dr.PrimarySet <0 -Enabled = False

If we were to state this logic in a single expression, it could be:

Dim A As Boolean = CheckBox.Checked
Dim B As Boolean = dr.PrimarySet <0
Dim C As Boolean = CheckBox.Enabled

CheckBox.Enabled = Not(A OrElse B) _
OrElse (A AndAlso (B OrElse C))

Arguably, it's much more clear to use an If:

If CheckBox.Checked Then
CheckBox.Enabled = (dr.PrimarySet <0) OrElse CheckBox.Enabled
Else
CheckBox.Enabled = dr.PrimarySet = 0
End If

In any way, let's hope the VB designers introduce a ternary operator in
the language for the next version:

CheckBox.Enabled = CheckBox.Checked? _
(dr.PrimarySet <0) OrElse CheckBox.Enabled | dr.PrimarySet = 0

=))))

Have fun,

Branco

Jul 27 '06 #52
"Greg" <Gr**@no-reply.okschrieb:
However, losing the IIf and using the AndAlso would then require an extra
Not. ie:

CheckBoxPrimaryYN.Enabled = Not (Not CheckBoxPrimaryYN.Checked AndAlso
dr.PrimarySet)

As the OP's question related to which statement is easier to
read/maintain, I'd prefer not to use double negatives.
You could easily get rid of the double negation by applying De Morgan's law:

<URL:http://en.wikipedia.org/wiki/De_Morgan%27s_laws>:

NOT(A AND B) = (NOT A) OR (NOT B)
NOT(A OR B) = (NOT A) AND (NOT B)

=>

\\\
Me.CheckBoxPrimaryNY.Enabled = _
CheckBoxPrimaryYN.Checked OrElse Not dr.PrimarySet

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Jul 27 '06 #53

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: craig | last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style? style 1: method { }
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
29
by: Ron Burd | last post by:
Hi, As my company is moving into C# they are enforcing the use of styling convention in the source code, such as methods naming conventions, newlines, etc. Does someone know of products that...
4
by: Dotnetjunky | last post by:
Hi, So far, I've found tons of documents describing recommended coding standards for C#, but not a single piece on VB.NET yet. Anybody here knows such a coding standards guideline on VB.NET...
4
by: Mike Labosh | last post by:
I realize that you people have not seen much of me other than some framework responses I have posted. I am primarily a VB guy (yes, you can laugh) But I have lurked here for several years,...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
3
by: ct-86 | last post by:
http://www.cdbook.cn/book.asp?id=2393 Organizational and Policy Issues 1 0. Don't sweat the small stuff. (Or: Know what not to standardize.) 2 1. Compile cleanly at high warning levels. 4 2....
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
1
by: Jim Johnson | last post by:
is this C style coding? I don't seem to see much C++ code in this way. is this a bad programming practice? code seem ugly coding this way. =================
7
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.