Skip to content

Instantly share code, notes, and snippets.

@pydanny
Created July 8, 2020 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pydanny/6cf5f6344a59a655cf885dd86eefd5b2 to your computer and use it in GitHub Desktop.
Save pydanny/6cf5f6344a59a655cf885dd86eefd5b2 to your computer and use it in GitHub Desktop.
Which one is more legible?
def fun_function(name=None):
"""Find working ice cream promo"""
results = Promo.objects.active()
results = results.filter(
Q(name__startswith=name) |
Q(description__icontains=name)
)
results = results.exclude(status='melted')
results = results.select_related('flavors')
return results
def fun_function(name=None):
"""Find working ice cream promo"""
return (
Promo.objects.active()
.filter(
Q(name__startswith=name) |
Q(description__icontains=name)
)
.exclude(status='melted')
.select_related('flavors')
)
@jsparadacelis
Copy link

Method 1

@iokiwi
Copy link

iokiwi commented Jul 8, 2020

First example is more clear - the results assignment as the first part of the statement is a useful hint for whats about to happen.

The second example reads a lot like JS - doesn't seem that pythonic.

Maybe somewhere in between?

def fun_function(name=None):
    """Find working ice cream promo"""
    results = Promo.objects.active()
    results = results.filter(
                Q(name__startswith=name) |
                Q(description__icontains=name)
            )
           .exclude(status='melted')
           .select_related('flavors')
    return results

@angvp
Copy link

angvp commented Jul 9, 2020

I like @iokiwi's approach, although I don't like the double assignation to "results" variable, I would use something like:

def fun_function(name=None):
    """Find working ice cream promo"""
    promos = Promo.objects.active()
    results = promos.filter(
                Q(name__startswith=name) |
                Q(description__icontains=name)
            )
           .exclude(status='melted')
           .select_related('flavors')
    return results

@alb3rto269
Copy link

alb3rto269 commented Jul 9, 2020

In the versions you posted, method 1 seems to be more legible. However, after formatting them with black, both look the same IMO:

def fun_function(name=None):
    """Find working ice cream promo"""
    results = Promo.objects.active()
    results = results.filter(Q(name__startswith=name) | Q(description__icontains=name))
    results = results.exclude(status="melted")
    results = results.select_related("flavors")
    return results


def fun_function(name=None):
    """Find working ice cream promo"""
    return (
        Promo.objects.active()
        .filter(Q(name__startswith=name) | Q(description__icontains=name))
        .exclude(status="melted")
        .select_related("flavors")
    )

@CharlyJazz
Copy link

The first its better for new programmer, more fresh to the eyes

@jmfederico
Copy link

jmfederico commented Jul 9, 2020

Method 2. From the beginning is clear that I will return the the queryset.

Method 1 makes me think about the state of a variable which is never really important. Puts more effort on me. It keeps me thinking that something is going to happen to the variable but never happens.

@jmfederico
Copy link

@CharlyJazz, may I ask, are you a new programmer? If not, why do you think it is clearer for them?

I ask because It is an opinion on what others might think.

@vdboor
Copy link

vdboor commented Jul 9, 2020

Method 2 by far!

It tells line by line what you're querying. With method 1 I have to see how all variables are assigned, whether result remains the same thing, and what you're doing with it in between.

@iokiwi
Copy link

iokiwi commented Jul 9, 2020

I like @iokiwi's approach, although I don't like the double assignation to "results" variable, I would use something like:

Yeah, I second that @angvp! I was contemplating renaming the vars in my example to something a little more descriptive but I didn't want to distract from the main distinction of the number of intermediate assignments I would make.

In terms of variable naming I would personally probably go with active_promos and filtered_promos respectively.

@mitcom
Copy link

mitcom commented Jul 9, 2020

The first one! It is easier to debug with breakpoint() (.set_trace()), debuggers or even "debug printing".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment