How to catch these kind of bugs in Python?

A

asincero

Is there anyway to catch the following type of bug in Python code:

message = 'This is a message'
# some code
# some more code
if some_obscure_condition:
nessage = 'Some obscure condition occured.'
# yet more code
# still more code
print message


In the above example, message should be set to 'Some obscure condition
occured.' if some_obscure_condition is True. But due to a lack of
sleep, and possibly even being drunk, the programmer has mistyped
message. These types of bugs would easily be caught in languages that
have a specific keyword or syntax for declaring variables before use.
I'm still fairly new to using Python on a more than casual basis, so I
don't know if Python has anyway to help me out here.

-- Arcadio
 
J

Jorge Godoy

asincero said:
In the above example, message should be set to 'Some obscure condition
occured.' if some_obscure_condition is True. But due to a lack of
sleep, and possibly even being drunk, the programmer has mistyped
message. These types of bugs would easily be caught in languages that
have a specific keyword or syntax for declaring variables before use.
I'm still fairly new to using Python on a more than casual basis, so I
don't know if Python has anyway to help me out here.

Unit testing, running pylint, pychecker, etc.
 
D

dave.brueck

asincero said:
Is there anyway to catch the following type of bug in Python code:

message = 'This is a message'
if some_obscure_condition:
nessage = 'Some obscure condition occured.'
print message


In the above example, message should be set to 'Some obscure condition
occured.' if some_obscure_condition is True. But due to a lack of
sleep, and possibly even being drunk, the programmer has mistyped
message. These types of bugs would easily be caught in languages that
have a specific keyword or syntax for declaring variables before use.

There are tools that help (e.g. pychecker), but there are a few things
to consider:

1) If the programmer is sleepy/drunk, you're going to have other bugs
too (logical errors, not handling all cases, etc.)

2) Other languages would catch *some* types of these bugs, but would
still miss some of them (I can see a sleepy programmer also using the
wrong variable instead of just mistyping the right one).

So while a tool might assist, it's worth your while to also consider
some strategies for tackling the above two problems and, in the
process, the sleepy-programmer-mistype bugs will get caught as well.
Some type of testing is probably the best answer - be it reusable unit
tests or, at the very least, some interactive testing of code snippets
(which Python makes really easy to do).

-Dave
 
R

Ravi Teja

asincero said:
Is there anyway to catch the following type of bug in Python code:

message = 'This is a message'
# some code
# some more code
if some_obscure_condition:
nessage = 'Some obscure condition occured.'
# yet more code
# still more code
print message


In the above example, message should be set to 'Some obscure condition
occured.' if some_obscure_condition is True. But due to a lack of
sleep, and possibly even being drunk, the programmer has mistyped
message. These types of bugs would easily be caught in languages that
have a specific keyword or syntax for declaring variables before use.
I'm still fairly new to using Python on a more than casual basis, so I
don't know if Python has anyway to help me out here.

The keyword is "obscure condition". The solution is to use a coverage
tool and create unit tests that give you 100% code coverage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top