Is there a technic to avoid this bug

Discussion in 'Python' started by hg, Feb 27, 2007.

  1. hg

    hg Guest

    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
    hg, Feb 27, 2007
    #1
    1. Advertising

  2. hg

    hg Guest

    Michele Simionato wrote:

    > pychecker


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

    hg
    hg, Feb 27, 2007
    #2
    1. Advertising

  3. 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
    Diez B. Roggisch, Feb 27, 2007
    #3
  4. On Feb 27, 1:49 pm, "Diez B. Roggisch" <> 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
    Michele Simionato, Feb 27, 2007
    #4
  5. hg

    John J. Lee Guest

    "Diez B. Roggisch" <> 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
    John J. Lee, Feb 28, 2007
    #5
  6. hg

    Ben Finney Guest

    hg <> 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
    Ben Finney, Feb 28, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Laub
    Replies:
    7
    Views:
    361
    Scott
    Apr 23, 2004
  2. Alexander Malkis
    Replies:
    8
    Views:
    507
    Alexander Malkis
    Apr 14, 2004
  3. Roger23
    Replies:
    2
    Views:
    984
    Roger23
    Oct 12, 2006
  4. reju
    Replies:
    7
    Views:
    760
    Juan T. Llibre
    Mar 6, 2007
  5. AAaron123
    Replies:
    3
    Views:
    291
    AAaron123
    Feb 6, 2009
Loading...

Share This Page