News:

Precision Simulator update 10.174 (26 April 2024) is now available.
Navburo update 13 (23 November 2022) is now available.
NG FMC and More is released.

Main Menu

Can I Just Say...

Started by John Golin, Sun, 22 Apr 2012 07:58

John Golin

AUURGGGHHHH!H!!!

I just spent about 5 hours looking for a bug.  What was it?

if (this.aSpeaker.muteState = true && sDL != null)

instead of

if (this.aSpeaker.muteState == true && sDL != null)

 :evil:  :x
John Golin.
www.simulatorsolutions.com.au

Hardy Heinlin

QuoteWhat was it?
Too little sleep? :-)

By the way ... well, we all know there are languages where booleans can have more states than true and false so that the "==" method has to be used. However, in languages such as Java we also know that booleans can have only two states; I always name them with an adjective (the Java makers recommend names starting with "is...") and never use "==" in boolean tests, this makes life easier. E.g.:

if (this.aSpeaker.isMuted && sDL != null)

Jeroen Hoppenbrouwers

#2
John,

I've seen hard-core programmers use a different syntax to exactly prevent this kind of hard-to-spot error, but personally I don't use this syntax either as I find it counter-intuitive:

if (true == this.aSpeaker.muteState && null != sDL)

Such a statement cannot be compiled if you mistakenly use the = sign.

Of course it won't help any bit if there is an assignable expression on the left side.

At work, we developed the habit of just asking the neighbour to look over the problem line(s) when you stop seeing your own mistakes. We all know this is not unnecessary bothering of each other. It usually produces the issue in about 15 seconds. Very annoying indeed.


Jeroen

Shiv Mathur

Quote from: Jeroen HoppenbrouwersAt work, we developed the habit of just asking the neighbour to look over the problem line(s) ...

Haha ... I can just picture John leaning over the hedge with his laptop
and saying, "G'day, Mrs. Julia Gillard, could you just have a quick look at
this code and see if you spot something funny?"

martin

#4
Quote from: John GolinWhat was it?
if (this.aSpeaker.muteState = true && sDL != null)
instead of
if (this.aSpeaker.muteState == true && sDL != null)
hehehe John, never happens to me . . . any more...  :D

And while we're at it, remember
if ( string1 == string2)...vs.
if ( string1.equals(string2) )...
  :mrgreen:

John Golin

#5
Quote from: Hardy HeinlinToo little sleep? :-)
Possibly! :)  Also, too much wine the night before...  :)
Quote...
 I always name them with an adjective (the Java makers recommend names starting with "is...") and never use "==" in boolean tests, this makes life easier. E.g.:

if (this.aSpeaker.isMuted && sDL != null)

Good point! I will go back and tidy that up as well! I have a mix of code due to learning as I went  :)  I already have Radio.isWorking and ACP.isWorking... if there is one thing that is consistent in my code, it is lack of consistency!  :lol:

Even though I totally broke the program, I'm glad I'm overhauling the code - It was so 'dirty' in the original state.... :) I love OOP now!

Bloody part time programmers!    :P
John Golin.
www.simulatorsolutions.com.au

John Golin

Quote from: martinAnd while we're at it, remember
if ( string1 == string2)...vs.
if ( string1.equals(string2) )...
  :mrgreen:

Oh yeah - that one is easy to remember - NetBeans prompts me if I make that 'mistake' :)
John Golin.
www.simulatorsolutions.com.au

Jeroen Hoppenbrouwers

What's wrong with a pointer?!      :mrgreen:

martin

Quote from: Jeroen HoppenbrouwersWhat's wrong with a pointer?!
This?
Function:int mymax (int(*a)(int(*)(int(*)()),int(*)(int(*)(int**))), int(*b)(int(*)
(int(*)()),int*,int(*)(int(*)()))){return (int)((((int(*)(int(*)(int(*)()),int(
*)(int(*)())))a)> ((int(*)(int(*)(int(*)()),int(*)(int(*)())))b))?((int(*)(
int(*)(int(*)()),int(*)(int(*)())))a):((int(*)(int(*)(int(*)()),int(*)(int(*)(
))))b));??>

Function call:
mymax((int(*)(int(*)(int(*)()),int(*)(int(*)(int**))))3,(int(*)(int(*)(int(*)
()),int*,int(*)(int(*)())))52);

"This works 100% fine for the intel / motorola architecture."
[size=8][source][/size]
 
8)