Created
July 8, 2020 23:17
-
-
Save pydanny/6cf5f6344a59a655cf885dd86eefd5b2 to your computer and use it in GitHub Desktop.
Which one is more legible?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
) |
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.
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
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.