r/Python • • Jan 20 '19

Code golfing with Fizzbuzz (Discover a shorter way to write python if else loop)

https://youtu.be/1khdzmUHhC4
0 Upvotes

6 comments sorted by

2

u/neophysik Jan 20 '19

I love the idea of having public code challenges.. very fun!

I feel compelled to point out you're learning bad practices/habits . In python we want verbose (yet efficient) code. It's meant to be readable and easy to inherit. Unlike in other languages (C++, Java, etc), where short code snippits make it more readable.

1

u/pylenin Jan 20 '19

What do you mean bad practices? Do you mean the way I have written the code is unreadable or you think the code golf concept itself diminishes the idea of quality code writing? Can you elaborate?

1

u/neophysik Jan 20 '19

I really like the code golf part. 🤓

Trying to write code in as few characters as possible is the issue. Code is two things at once. It gives the interpretor instructions on how your program works and it's communication to other developers. This approach is heavily skewed towards instruction and does badly with communication. The more you use it the the more complicated the code will become.

The if else pattern is already optimized for form and function. Now instead of being instantly understandable by others, people have to mentally unpack what you've done and keep it in their head. That slows down reading speeds and makes it harder to jump around in the code.

The Zen of python gives the philosophy but doesn't give examples. I think this approach violates the following. I'd encourage you to get a better understanding of what these really mean in real world.. you'll know it instantly when you see beautiful code that follows the philosophy.

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Sparse is better than dense. Readability counts. There should be one-- and preferably only one --obvious way to do it. (This is the main one you're violating, we have one if else pattern and it's obvious what it does)

If you look at the opensource code from Google, Netflix, Facebook, etc you'll see really good examples of pythonic code.

0

u/pylenin Jan 20 '19

I totally get your point... But how can you optimize the characters by following the principles that you mentioned. If you write if elif else loop in the normal way it is meant to be written (which I did in the first implementation) you will obviously have more characters than the short hand notation. Also it wasn't like I implemented the short notation out of the blue... Before implementing it...I explained how it works. Also that wasn't my final implementation. I believe my final code is much more readable compared to the solution with the short hand notation of if else statement.

1

u/neophysik Jan 20 '19

Really the fundamental issue is the mentality that you'd want to produce terse code. Terse is only good when solving for complex.. if you apply terse to something simple like an if/else you're you're adding complexity not removing it.

The pythonic way to do one liners/shorthand is through lambas, list comprehensions, map, reduce and iterators (there a more if you go beyond the standard library). But they can be abused if you use them to often or for complex statements. Think about it this way. If you have 50 lines in a function you should think about how to make it more terse and break it up. If you have 5-10 lines chances are you're just adding complexity if you try to optimize them for line and character count.

That said I've often replaced 10 lines with a one line lambda after I've worked it out. That's good practice.. write it out get it working then refine it for readability and execution speed.

-1

u/[deleted] Jan 20 '19 edited Jan 20 '19

Post crap like that in /r/codegolf That it's implemented in Python does not make it topical here.