what is a boolean trap? (programming antipattern) (beginner - intermediate) anthony explains

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2021
  • today I talk about a boolean trap, how you can spot one in code review, and how you can fix the problem using a few python features!
    - all parameter types: • all python argument / ...
    playlist: • anthony explains
    streamed on / anthonywritescode
    ==========
    twitch: / anthonywritescode
    dicsord: / discord
    twitter: / codewithanthony
    github: github.com/asottile
    stream github: github.com/anthonywritescode
    I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 17

  • @StopThatShitPls
    @StopThatShitPls 3 ปีที่แล้ว +21

    Why not just define two functions - pop_first and pop_last?

    • @anthonywritescode
      @anthonywritescode  3 ปีที่แล้ว +10

      that would work well too!

    • @MagnusAnand
      @MagnusAnand 3 ปีที่แล้ว

      @@anthonywritescode Here you have some solutions adamj.eu/tech/2021/07/10/python-type-hints-how-to-avoid-the-boolean-trap/

  • @jurgengmach4636
    @jurgengmach4636 ปีที่แล้ว +2

    I am waiting for the sequel: "what is a troolean trap?" 🤣

  • @MagnusAnand
    @MagnusAnand 3 ปีที่แล้ว +1

    cool video

  • @C0pernicus
    @C0pernicus 7 หลายเดือนก่อน

    Is there an advantage to using an Enum over typing.Literal?

  • @marco_gorelli
    @marco_gorelli 2 ปีที่แล้ว +1

    Do you know if there exists a tool to detect boolean traps? e.g. flake8 extension, or even a tool to re-write them. If not, I'm tempted to try writing one

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว

      not that I know of! but it'd be pretty easy to detect if the codebase uses type extensions (non-namedonly bool parameter) or a call which takes a boolean literal in a positional argument

  • @420_gunna
    @420_gunna 2 ปีที่แล้ว +1

    I haven't seen someone use the enum.Enum constructor like that before -- I usually see people write a class that inherits from Enum, then defines a bunch of class attributes like Tomato = "TOMATO", Lettuce = "LETTUCE", etc. Is using the enum.Enum primary constructor directly (like enum.Enum("Tomato") the same as writing a class that inherits from Enum with class attributes like Tomato = "Tomato"? I really need to look at how enums actually work under the hood :)

    • @anthonywritescode
      @anthonywritescode  2 ปีที่แล้ว

      it's a bit magical -- acts as both a metaclass and a callable

  • @teslainvestah5003
    @teslainvestah5003 2 ปีที่แล้ว

    I'd assume dct.popitem(True) would pop (3,4). Because if a method called popitem takes any argument, it should be an integer index.
    In my projects I just have unlimited line length, and never use positional arguments for anything unless some module (often pygame) forces me to.

  • @darrenlefcoe
    @darrenlefcoe ปีที่แล้ว

    great video, but why write code in terminal as opposed to a lightweight editor like vscode which is easier to do ?

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว +3

      "vscode" "lightweight" lmao

    • @darrenlefcoe
      @darrenlefcoe ปีที่แล้ว

      @@anthonywritescode fair enough. Just thought that some linting and intellisense might be helpful.

    • @anthonywritescode
      @anthonywritescode  ปีที่แล้ว

      implying I don't have linting?

    • @darrenlefcoe
      @darrenlefcoe ปีที่แล้ว

      @@anthonywritescode respect to those who can operate without those tools...