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
Nov 16 '05
16 1467
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com.. . 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.
Fair enough. ;)
-A
Incidentally (although I'm sure that this _will_ cause a holy war), I
apply the same reasoning to curly braces and all other manner of code
formatting: if I start work at a shop that formats their code a certain
way, and I insist upon continuing to do it "my way" because my way is
"better," then I am an idiot.
It doesn't matter how stupid "their way" of formatting code looks to
me. If everyone else in the shop is used to that, then that is what is
"more readable" and it is what I should be doing, too.
My dream shop would have an automated code formatter that would just
mash everyone's code and make it look the same every time it was
checked into the library. Visual Studio's "Edit->Advanced->Format
Selection" operation is your friend! Productivity wouldn't jump, but it
would improve.
I read stories of programmers (during the big boom, when we could get
away with anything, including bringing pets to work) insisting on
writing formatters / deformatters so that they could transform others'
code into "their" style, code in "their" style, and then transform the
code back to the group standards. I just shake my head in wonder at
such nonsense.
As a quick comment. I have a pet hate against this minimal typing when
it comes to string comparisons. Does anybody actually find this
readable ?!:
if ( !strcmp( string1, string2 ) )
{
// Strings match
}
else
{
// Strings dont match
}
I've gotten into the habit of putting the fixed value on the
left-hand-side. It takes a bit of getting used to, but as a consistent
approach I use this across the board, regardless of type.
if ( true == bValue )
{
}
if ( 0 == strcmp( string1, string2 ) )
{
}
etc..
etc..
Rich
Bruce Wood wrote: Incidentally (although I'm sure that this _will_ cause a holy war), I apply the same reasoning to curly braces and all other manner of code formatting: if I start work at a shop that formats their code a
certain way, and I insist upon continuing to do it "my way" because my way is "better," then I am an idiot.
It doesn't matter how stupid "their way" of formatting code looks to me. If everyone else in the shop is used to that, then that is what
is "more readable" and it is what I should be doing, too.
My dream shop would have an automated code formatter that would just mash everyone's code and make it look the same every time it was checked into the library. Visual Studio's "Edit->Advanced->Format Selection" operation is your friend! Productivity wouldn't jump, but
it would improve.
I read stories of programmers (during the big boom, when we could get away with anything, including bringing pets to work) insisting on writing formatters / deformatters so that they could transform
others' code into "their" style, code in "their" style, and then transform
the code back to the group standards. I just shake my head in wonder at such nonsense.
On 3 Feb 2005 02:16:07 -0800, "RichS"
<ri************ ****@surfcontro l.com> wrote: I've gotten into the habit of putting the fixed value on the left-hand-side. It takes a bit of getting used to, but as a consistent approach I use this across the board, regardless of type.
Argh, I absolutely hate that. Mathematical tradition always puts
variables on the left and constants on the right of a comparison, and
that's also the natural way to read a comparison. One of the best
things about C# as compared to C++ is that there's no auto-conversion
of everything to boolean, so you no longer have to invert comparisons
to prevent typo bugs...
-- http://www.kynosarges.de
In article <11************ *********@l41g2 000cwc.googlegr oups.com>, ri************* ***@surfcontrol .com says... As a quick comment. I have a pet hate against this minimal typing when it comes to string comparisons. Does anybody actually find this readable ?!:
if ( !strcmp( string1, string2 ) ) { // Strings match } else { // Strings dont match }
This is a different issue altogether. strcmp does not actually return
boolean value. So, although the above will still work, it is
technically incorrect because the other possible return values are
negative or positive numbers denoting which string is "greater"
alphabetically, rather than true or false.
Above should have been
if(strcmp(str1, str2)==0)
//stings match I've gotten into the habit of putting the fixed value on the left-hand-side. It takes a bit of getting used to, but as a consistent approach I use this across the board, regardless of type.
if ( true == bValue )
This is uncessessary extra bit of coding imo. Good C/Java/C#
programmers should be able to read
if(bValue)
happily and not dismiss the ! operand as "hard to read" e.g.
if(!bValue).
Daniel has already given a very good example as to why using
if(bvalue==true )
is a not a good practice and
if(true==bValue )
is just a workaround, imo, for those not confident enough to read pure
C/Java/etc syntax.
Just my 2p worth.
Cheers,
Paul.
> int i = 0; ... if (i = 3) {...}
In good old days of C and C++ this would not have been flagged as a compile
time error which got quite a few people into habit of writing the condition
with constant on the left had side like : if ( 3 == i) This would trap the
missed equal to sign (if (3 = i)) as a compiler error !!
I never liked the
if (!strcmp(string 1, string2))
convention in C. It just struck me as sloppy: strcmp returns a
tri-state value, not a dual-state value, so using ! always struck me as
a cheap trick. I always preferred to code
if (strcmp(string1 , string2) == 0)
in order to highlight the fact that it could be zero, less than zero,
or greater than zero. As GingerDeafMan points out, thanks to Anders
Hejlsberg and team, C# doesn't allow you to treat integers as booleans,
so now everyone has to do it my way. :)
Of course, those few times when I was in a shop that had standardized
on testing strcmp results using the ! operator, I had to hold my nose
and adapt.... :) 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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| | |