Is there a technic to avoid this bug

John J. Lee jjl at pobox.com
Tue Feb 27 20:34:30 EST 2007


"Diez B. Roggisch" <deets at nospam.web.de> writes:

> hg wrote:
[...]
> > In a relatively similar domain, I spent a few hours find this bug:
> > 
> > value == self.Get_Value()
> > if value == WHATEVER:
> >    do this
> > 
> > instead of
> > value = self.Get_Value()
> > if value == WHATEVER:
> >    do this
> > 
> > Is there a way to avoid such a bug with some type of construct ?
> 
> No. In a language inherent with sideeffects, there is nothing that should
> force you to not write that.
[...]

It's illegal in C#:

// -------- compare.cs ----------
class BadComparison {
    static void Main() {
        1 == 2;
    }
}
// -------- end -----------------

$ mcs compare.cs
compare.cs(3,9): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Compilation failed: 1 error(s), 0 warnings
csharp[0]$


// -------- compare2.cs ----------
class BadComparison {
    static void Main() {
        bool falsehood = 1 == 2;
    }
}
// -------- end -----------------

$ mcs compare2.cs
compare2.cs(3,14): warning CS0219: The variable `falsehood' is assigned but its value is never used
Compilation succeeded - 1 warning(s)


John



More information about the Python-list mailing list