Why are we taught "if( variable == constant )"?

Started by
63 comments, last by Chris Hare 19 years, 8 months ago
Quote:Original post by Nathaniel Hammen
I would use if(!strlen(somestring)) as well.


I would do the same thing. I don't know where I inherited this from, but this seems a perfectly natural convention to me, and I immediately understand its intention. However, I can see where someone might see this as an abuse of C++'s implicit integer to bool conversion.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Advertisement
Just to add my 2 cents: A few people seem to think that you only make this mistake if you are a novice programmer or are just tired or lazy. But if you happen to use several different programming languages (some which use == for equality and others which use =) regularly, then you are pretty much guaranteed to keep making this mistake. If I've been writing SQL queries all day, then switch over to some C++ code, it's not hard for me to slip up occasionally, and I won't always spot it right away - not because I'm sleep-deprived, but because I'm use to seeing it that way in the other language.
But I really don't like the backwards style because it doesn't feel natural. Compiler warnings are enough for me, and the syntax highlighting is a good idea.
[Edit] But on a similar note, in Java, I do sometimes write my equals() comparisons backwards because it handles null pointers. E.g.
if (constant.equals(variable))
won't throw a NullPointerException if the variable is null.
Quote:Original post by Etnu
It's more "english like to say"

if(TheNumberOfThings == 5)

Personally, I like Pascal's approach (using := for assignment).

[blaming target=c++][flaming][flaming][flaming]
I'm too. Also i think C and C++ have _pretty_bad_language_design_. <> for template parameters, = that returns a value,== , << and >> used on streams. Tonns Of Crap.
I can use C++ as well as pascal and detect == typos, but every time i'm checking for typos it reminds me that in our world, only complete crap may become popular.

Also,c/c++ have tonns of buggy RTL functions(like printf),etc. It teachs everyone to use dirty hacks all the way and not check for buffer overflows... In pascal, when you're passing variable-length array as parameter, it's passed together with length,and it's common practice to check length.
Also,most implementations of pascal have their owh memory manager,that is ALOT faster than malloc/free/new/delete (by orders of magnitude). (edit:for ones that don't understand how that may be, for instance,FreePascal's RTL (and Borland Delphi as well) calls system only to allocate big memory blocks (>1MB in size), and uses it's own memory manager to allocate smaller blocks inside heap blocks it gets from system.
"long" strings in FreePascal works faster than bloody null-terminated char* in C (and Pascal's strings are 100% safe), and of course faster than C++'s strings.
[/blaming]

i think it may be useful to write parser/preprocessor that just checks for single = inside () ,and generates an _error_ so you just can't make such error and are forced to do things in other way. And also uses !() for template parameters.
Quote:Original post by Pipo DeClown
Quote:Original post by Fruny
Quote:Original post by FeralOfEclecticWizard
Use syntax highlighting to color '=' differently from '==' ? Works pretty well actually...
Clever!
You know how to do it without Visual Assist?


@Fruny

Thank you that means alot as I've been collecting your bits of wisdom for a while now (=


@Pipo

Given that I don't use Visual Assist, yes.

However, given that being able to color anything I want anyway I want is one of the reasons I use vim this fragment may or may not be of use to you.

If nothing else perhaps the other things I color might be a source of ideas...

" {{{ a bunch of operators" syn match		feralOperator1		"[+\-=,:~]"syn match		feralOperator2		"=="			" Often confusedsyn match		feralOperator2		"[!;]"			" specialsyn match		feralOperator3		"[{}()\[\]]"	" Brace/Prensyn match		feralOperator4		"::"			" Scopesyn match		feralOperator4		"->"			" Scopesyn match		feralOperator4		"\."			" Scopesyn match		feralOperator6		"[|&^]=\="		" Bitwise -- MUST be listed before feralOperator5's double bar entry!! (else it overrides it).syn match		feralOperator5		"[<>]=\="		" Logicsyn match		feralOperator5		"[|&]\{2}"		" Logic"syn match		feralOperator5		"[=|&]\{2}"		" Logicsyn match		feralOperator5		"[\?]"			" Logic" }}}


As you may guess, I prefer a VERY colorfull enviroment..

Fwiw (:

loop vars were mentioned as well.. I tend to stick with iInd or a variation (dwInd, etc.) in a special color, of course.

HTH :)

[Edited by - FeralOfEclecticWizard on August 31, 2004 3:40:52 AM]
Quote:Why are we taught...

Because the intent is to learn to program, not C++.

This topic is closed to new replies.

Advertisement