473,385 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is there a technic to avoid this bug

hg
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
Feb 27 '07 #1
5 1156
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
Feb 27 '07 #2
On Feb 27, 1:49 pm, "Diez B. Roggisch" <d...@nospam.web.dewrote:
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

Feb 27 '07 #3
hg
Michele Simionato wrote:
pychecker
Thanks all ... pydev extension does not however ... will have to install
pychecker also.

hg

Feb 27 '07 #4
"Diez B. Roggisch" <de***@nospam.web.dewrites:
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
Feb 28 '07 #5
hg <hg@nospam.orgwrites:
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

Feb 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: seesaw | last post by:
Is it right thing to always avoid using "new" to create objects? What if after starting the application, then decide which and how many objects to create? (Seems like under such situation is there...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
3
by: Thomas Müller-Lynch | last post by:
How can I avoid late binding with the directive strict = tru My ASP .net-file looks like this <%@DEBUG=true TRACE=true Strict=false EXPLICIT=true% .. dim footerValues as Arra footerValues =...
32
by: KK | last post by:
Hello all, I have a unsigned char buffer 'buffer' and I need to convert the first 12 bytes of it into a string. Below is a code that should work, however, how can I avoid reinterpret_cast...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
6
by: howa | last post by:
Since it is part fo the standard, why I always heard that we should avoid iframe? any comments? thanks.
5
by: Kevin | last post by:
Hi! Is there anyway to avoid flicking between pages navigation in Asp.Net 2.0, like smartnavigation atribute in asp.net 1.1? Thanks and Regards.
4
by: DC | last post by:
Hi, the subject says it all. Since visible=false controls will still run though Page_Load, I was hoping that the MultiView would somehow manage to avoid a Page_Load of controls in its inactive...
11
Niheel
by: Niheel | last post by:
http://bytes.com/images/howtos/information_overloaded.jpgPaul Graham wrote an interesting article a few months back about how the internet is leading to information overload for information workers...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.