Giter Club home page Giter Club logo

Comments (6)

SpecLad avatar SpecLad commented on June 16, 2024 1

b if a else a is not equivalent to b or a.

from flake8-simplify.

MartinThoma avatar MartinThoma commented on June 16, 2024

This would be pretty easy to check and implement:

IfExp(
                test=Name(id='a', ctx=Load()),
                body=Name(id='a', ctx=Load()),
                orelse=Name(id='b', ctx=Load()),
            ),

However, I'm uncertain of I would prefer the left or the right expression. Give me a couple of days to think about it. The right expression is shorter, but the left expression seems clearer to me.

from flake8-simplify.

MartinThoma avatar MartinThoma commented on June 16, 2024

The second one I would rather convert to

b if a else a -> b or a. It might need less evaluation.

from flake8-simplify.

MartinThoma avatar MartinThoma commented on June 16, 2024

I've just created the following expression tables with

abs = [("a", ""), ("", "b"), ("a", "b"), ("", 0)]
for a, b in abs:
    print(b if a else a)

Table for a if a else b and a or b:

a b Result
"a" "" "a"
"" "b" "b"
"a" "b" "a"
"" 0 0

Table for b if a else a and a and b:

a b Result
"a" "" ""
"" "b" ""
"a" "b" "b"
"" 0 ""

Table for b or a:

a b Result
"a" "" "a"
"" "b" "b"
"a" "b" "b"
"" 0 ""

Table for b and a:

a b Result
"a" "" ""
"" "b" ""
"a" "b" "a"
"" 0 0

from flake8-simplify.

MartinThoma avatar MartinThoma commented on June 16, 2024

@SpecLad You're completely right. They don't even have a single element in common 😅

This example helped me to make my mind up about this rule. Although it easy to reason which input gives which output, I do need to think about it. I find the boolean expressions a or b / a and b / b or a / b and a harder to reason about than their ternary expression equivalents.

from flake8-simplify.

MartinThoma avatar MartinThoma commented on June 16, 2024

As the ternary expressions (a if a else b) are easier for me to understand than the logical expressions (a or b), I will not implement this rule.

from flake8-simplify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.