I've been flipping through various coding standards documents for C#/.NET
and all of them say to use:
if ( boolVar ) {...} instead of if ( boolVar == true ) {...}
For the life of my I can't figure out why. If anything, I would guess the
opposite would be better to use since the ! operator is so hard to read.
Upon quick glance,
if ( boolVar == false ) is easier to understand than if ( !boolVar )
Anybody have some more concrete reasons why one is preferred over the other
aside from one being less typing.
Thanks,
-A 16 1465
Alfred:
This can degenerate into a holy war. In general, I would recommend keeping
it as simple as possible. Think about looking at this code 3 months from
now, at 2:00 AM under a deadline after working 15 hour days for 2 months
straight.
8-)... Been there...
John
"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:Ou******** ******@tk2msftn gp13.phx.gbl... I've been flipping through various coding standards documents for C#/.NET and all of them say to use:
if ( boolVar ) {...} instead of if ( boolVar == true ) {...}
For the life of my I can't figure out why. If anything, I would guess the opposite would be better to use since the ! operator is so hard to read.
Upon quick glance,
if ( boolVar == false ) is easier to understand than if ( !boolVar )
Anybody have some more concrete reasons why one is preferred over the
other aside from one being less typing.
Thanks, -A
This is where I invoke my #1 rule of programming style. :)
"Code written to a mediocre standard is better than brilliant code that
is not standard."
or, stated another way:
"If everyone else is doing it a standard way, your clever solution is
by definition no longer clever, unless it offers some stunning
advantage over the standard."
If everyone in the industry were writing
If (boolVar == true)
and I were to write instead
if (boolVar)
claiming that it was "better" because I had to type fewer characters, I
would not blame my colleagues if they were to give me the wedgie to end
all wedgies and tossed me out on the street. Every time they were to
read my code they would have to mentally stop and think about what I
was doing.
The reverse is also true.
Almost every C / C++ / Java / C# programmer out there writes
if (boolVar)
and
if (!boolVar)
that fact, in and of itself, makes the alternative syntax "less
readable". From personal experience, I can say that reading
if (boolVar == false)
is like a speed bump for me. I've been reading C for 20 years and I'm
used to the standard. It has, in effect, become "more readable" for me.
Hi Alfred,
"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message
news:Ou******** ******@tk2msftn gp13.phx.gbl... I've been flipping through various coding standards documents for C#/.NET and all of them say to use:
if ( boolVar ) {...} instead of if ( boolVar == true ) {...}
For the life of my I can't figure out why. If anything, I would guess the opposite would be better to use since the ! operator is so hard to read.
Upon quick glance,
if ( boolVar == false ) is easier to understand than if ( !boolVar )
Anybody have some more concrete reasons why one is preferred over the
other aside from one being less typing.
In short, it prevents syntax errors from becoming logical errors. It's
not unusual, especially for people who haven't used C-variant languages in
the past, to accidentally use the assignment operator (=) instead of the
equality operator (==). Consider this C# code:
int i = 0;
...
if (i = 3) {...}
The above code would be flagged as a compile-time error because the
assignment operator (=) returns the value assigned, which in this case is 3,
which is not a boolean value. On the other hand, if you do this:
bool boolVar = false;
...
if (boolVar = true) {...}
The above code will not cause a compile time error, even though it's
probably not what was intended. The assignment operator (=) returns the
value assigned, which in this case is true, which *is* a boolean value.
If you never use "== true" or "== false" the mistake can be avoided.
Regards,
Daniel
For those who found my preceding reply utterly unconvincing :) I offer
the following:
The problem with Alfred's original sample was the name of the variable.
Programmers are _supposed_ to name their boolean variables to express
the condition that they represent. In that situation, I find this:
if (isADuplexPrint Job)
much more readable than this:
if (isADuplexPrint Job == true)
because in the first example I'm expressing something in terms of my
problem domain ("if this is a duplex print job then I want to...")
whereas in the second example I have to remember that this is a boolean
variable, and that it's either true or false, and that "true" means
that it is a duplex print job.
One encourages me to think about my problem; the other notation
encourages me to think about what's going on inside the machine.
Of course, if you name your boolean variable "flag", or something
equally opaque, then this argument doesn't hold water. But then, we
would never do _that_, would we...? :)
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. . From personal experience, I can say that reading
if (boolVar == false)
is like a speed bump for me. I've been reading C for 20 years and I'm used to the standard. It has, in effect, become "more readable" for me.
That's a good point. I know the times I've run across that style of code,
I've had to pause momentarily to "recompose" myself. ;)
-A
Thanks Daniel. That is definitely a very legitimate reason for avoiding "==
true" and "== false".
-A
"Daniel Pratt" <ko************ ******@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. .. Hi Alfred,
"Alfred Taylor" <om***@R-E-M-O-V-Eyahoo.com> wrote in message news:Ou******** ******@tk2msftn gp13.phx.gbl... I've been flipping through various coding standards documents for
C#/.NET and all of them say to use:
if ( boolVar ) {...} instead of if ( boolVar == true ) {...}
For the life of my I can't figure out why. If anything, I would guess
the opposite would be better to use since the ! operator is so hard to read.
Upon quick glance,
if ( boolVar == false ) is easier to understand than if ( !boolVar )
Anybody have some more concrete reasons why one is preferred over the other aside from one being less typing.
In short, it prevents syntax errors from becoming logical errors. It's not unusual, especially for people who haven't used C-variant languages in the past, to accidentally use the assignment operator (=) instead of the equality operator (==). Consider this C# code:
int i = 0; ... if (i = 3) {...}
The above code would be flagged as a compile-time error because the assignment operator (=) returns the value assigned, which in this case is
3, which is not a boolean value. On the other hand, if you do this:
bool boolVar = false; ... if (boolVar = true) {...}
The above code will not cause a compile time error, even though it's probably not what was intended. The assignment operator (=) returns the value assigned, which in this case is true, which *is* a boolean value.
If you never use "== true" or "== false" the mistake can be avoided.
Regards, Daniel
Hi, Alfred, if ( boolVar ) {...} instead of if ( boolVar == true ) {...}
Because it's redundant. One might as well ask, why
if ( boolVar == true ) instead of if ( (boolVar == true) == true)
Peace,
--Carl
100% correct.
Also, as I was reading this thread, I kept on thinking that in
if(something) {...}
you are looking at the value of 'something', whereas in
if(something==t rue) {...}
you are technically speaking looking at the result of a comparison
between 'something' and a value ('true'). Granted the net effect is the
same, but, technically speaking, there are differences. And if
something was not a boolean, the comparison might actually open a whole
can of worms (but that would be a different playground, right?).
Still, the reason illustrated by Daniel above is probably the best
reason to avoid if(something==t rue).
F.O.R. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: ganesan |
last post by:
Hi Guys,
Could any one knows the best coding standards styles(with variable
declarations for c#) . and if any links or site with the best coding
standards for .NET send me those links
regards
Ganesan
|
by: dotNetDave |
last post by:
About three weeks ago I released the first .NET coding standards book titled
"VSDN Tips & Tricks .NET Coding Standards". Here is what the famous author/
speaker Deborah Kurata says about it:
"David McCarter once again demonstrates his knack for pulling best practices
into one cohesive unit with his new book "VSDN Tips and Tricks: .NET Coding
Standards". This book includes everything from how to set up your project to
how to declare...
|
by: db2sysc |
last post by:
ALl.
Is it possible to get MS ACCESS CODING STANDARDS?
TIA
|
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 initiative.
Typically I find that coding standards are written by some guy in the
company who has a way of coding that he likes and then tries to force
everybody else to write code the way he likes it, not for any rational
reason, but simply for the...
|
by: Konrad Palczynski |
last post by:
I am looking for tool to validate conformity to defined coding standard.
I have already found Parasoft's C++ Test, but it is quite expensive. Is
there any Open Source alternative? I do not need GUI, fancy reports nor
predefined sets of rules.
| |
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 developed significant content for the C programming
language that is available at:
https://www.securecoding.cert.org/
by clicking on the "CERT C Programming Language Secure Coding Standard"
|
by: editormt |
last post by:
A recent poll asked if programming standards are used by development
organisations... and if they are controlled.
None: 20%
Yes, but without control: 49%
Yes, with control: 31%
Participants: 369
Source: Methods & Tools (http://www.methodsandtools.com)
|
by: pat |
last post by:
CodeCheck Coding Standard's Support
As a free service to our customers we offer support in developing
"rule-files" for automating corporate coding standards.
If you have a coding standard that you wish to automate please send the
standard to us in PDF format so we can assist in developing the
automation with codecheck.
If you are developing codecheck rule-files and have a particular
|
by: auratius |
last post by:
http://www.auratius.co.za/CSharpCodingStandards.html
Complete CSharp Coding Standards
1. Naming Conventions and Styles
2. Coding Practices
3. Project Settings and Project Structure
4. Framework-Specific Guidelines
Naming Conventions and Styles
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |