Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there a technic to avoid this bug

Reply
Thread Tools

Is there a technic to avoid this bug

 
 
hg
Guest
Posts: n/a
 
      02-27-2007
Hi,

In C/C++ I got used to write an expression like so:

#define TEST 0

if (TEST == value)
{

}

in order to avoid the usual bug:
if (value = TEST)
{

}

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 ?

Thanks,
hg


 
Reply With Quote
 
 
 
 
hg
Guest
Posts: n/a
 
      02-27-2007
Michele Simionato wrote:

> pychecker


Thanks all ... pydev extension does not however ... will have to install
pychecker also.

hg

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-27-2007
hg wrote:

> Hi,
>
> In C/C++ I got used to write an expression like so:
>
> #define TEST 0
>
> if (TEST == value)
> {
>
> }
>
> in order to avoid the usual bug:
> if (value = TEST)
> {
>
> }
>
> 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.

However, it might be that either pychecker or pylint will give you a warning
for such statements.

Diez
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      02-27-2007
On Feb 27, 1:49 pm, "Diez B. Roggisch" <(E-Mail Removed)> wrote:
> However, it might be that either pychecker or pylint will give you a warning
> for such statements.


Yep, pychecker gives a warning "Statement appears to have no effect"

Michele Simionato

 
Reply With Quote
 
John J. Lee
Guest
Posts: n/a
 
      02-28-2007
"Diez B. Roggisch" <(E-Mail Removed)> 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
 
Reply With Quote
 
Ben Finney
Guest
Posts: n/a
 
      02-28-2007
hg <(E-Mail Removed)> writes:

> 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 ?


Use pylint to check your code for common mistakes.

<URL:http://www.logilab.org/projects/pylint>

===== bad_assign.py =====
""" Demonstrate a logical error """
value = None

value == 10
if value == 10:
print "Yep"
else:
print "Nope"
=====

$ python ./bad_assign.py
Nope

$ pylint ./bad_assign.py
************* Module bad_assign
C: 2: Invalid name "value" (should match (([A-Z_][A-Z1-9_]*)|(__.*__))$)
W: 4: Statement seems to have no effect

[...]

--
\ "When I was crossing the border into Canada, they asked if I |
`\ had any firearms with me. I said, 'Well, what do you need?'" |
_o__) -- Steven Wright |
Ben Finney

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
Avoid having a SQL express for web parts and avoid personalization Roger23 ASP .Net 2 10-12-2006 10:54 PM
Avoid wasting time or how to avoid initialization Alexander Malkis C++ 8 04-13-2004 11:23 PM
How to avoid "f.close" (no parens) bug? Stephen Ferg Python 12 02-13-2004 06:40 AM
Foto technic Curt Digital Photography 0 08-07-2003 07:45 AM



Advertisments