• sugar_in_your_tea@sh.itjust.works
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    5 days ago

    def some_func(*args, kwarg=[])

    Don’t do this:

    def fun(l=[]):
        l.append(len(l))
        return l
    
    fun()  # [0]
    fun()  # [0, 1]
    fun(l=[])  # [0]
    fun()  # [0, 1, 2]
    fun(l=None)  # raise AttributeError or TypeError if len(l) comes first
    

    This can be downright cryptic if you’re passing things dynamically, such as:

    def caller(*args, **kwargs):
        fun(*args, **kwargs)
    

    It’s much safer to do a simple check at the beginning:

    if not l: 
        l = [] 
    
      • logging_strict@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        5 days ago

        Oh no a stray None! Take cover …

        Robust codebase should never fail from a stray None

        Chaos testing is specifically geared towards bullet proofing code against unexpected param types including None.

        The only exception is for private support function for type specific checking functions. Where it’s obviously only for one type ever.

        We live in clownworld, i’m a clown and keep the company of shit throwing monkeys.