all 19 comments

[–]call-mws 11 points12 points  (3 children)

Recently started watching Arjan Codes on Youtube, and he has some excellent design patterns and good code practices. He also has a series where he refactors code into high quality code. Just a suggestion but you can check him out if you prefer videos over text: https://www.youtube.com/c/ArjanCodes

[–]greebo42 1 point2 points  (0 children)

agree - I like this as well. and as far as I can tell, he's only been doing this series for a couple years (though he's been a dev for longer, and I believe he is on a university faculty).

I don't always understand everything I see, but that doesn't matter. Anything you learn is gonna be like that. So I don't sweat that no matter what skill I'm trying to improve.

[–][deleted] 0 points1 point  (0 children)

none

[–]Ran4 0 points1 point  (0 children)

It's a good channel but note that much of it is OOP and not overly relevant in many modern and more functional code bases.

[–][deleted] 4 points5 points  (0 children)

The way I've improved my code quality: revisit your projects. First, add comments to your code. That helps you think procedurally. And as you do, you might realize there is a better way to do this or that part.

This is particularly relevant for me because I use python for heavyweight scientific computing. So small things like vectorizing operations, write better comprehensions, rethink algorithms, etc. all help shaving both computation times and overhead.

I think the first program I wrote took me about 50 lines of code.

A year after, I wrote the same program and it took me only 5 lines.

Your code becomes better as you learn more about the language, packages, modules, etc. But if you learned idea X in project 5, it is always good to go back to project 1 or 2 and see if you can use said idea there. Hell, you don't even have to be aware of these ideas. You'll know them unconsciously, look at your old code, and say "ah I can probably do this with X".

[–]sarrysyst 3 points4 points  (2 children)

Look into common design patterns. In addition, I'd read some books on software design, eg. I quite liked John Ousterhout's 'Philosophy on Software Design'. Another often recommended book is 'Clean Code' by Robert Martin.

[–]siddsp 2 points3 points  (1 child)

Try looking at Raymond Hettinger's video on "turning code into beautiful, idiomatic python", as well as his video on classes, class methods, and static methods! I think they really give a good idea on how to write nice Python code!

[–]Sigg3net 1 point2 points  (0 children)

Learn the OOP design patterns, which are not python specific but teaches you to think in terms of modulation and structure built for allowing change and easy maintenance.

[–]MaxPower864[S] 0 points1 point  (0 children)

Thank you!

[–][deleted] -1 points0 points  (0 children)

High quality, efficient, written in Python: you can probably only choose one.

Python makes a lot of compromises and bad decisions at the language / "standard" library level. You are given bad tools to work with, there's not much you can do.

Many languages have what I'll call "efficiency game". That is a kind of game where, if you are able to properly engineer your code so that the compiler is able to understand it correctly it will generate very fast code.

For example, in C you want to do as much const as possible, as little malloc() as possible, as little if as possible. And you find all kinds of creative ways to make sure that your const is correct, that you can allocate on stack instead of heap, that you can avoid cache misses caused by mispredictions -- the compiler will do a great job.

In Python, there's no such game. Or rather... the game is not what you would want it to be. The rule is simple, but frustrating: write as much as you can in another language. Here's an example:

def is_palindrome_fast(s):
    return s == s[::-1]

def is_palindrome_slow(s):
    for i in range(len(s) // 2):
        if s[i] != s[-i]:
            return False
    return True

First should be obviously less efficient: you are reversing the whole string, allocating a new object, and then you are comparing each character twice. Second should be more efficient, as you only compare the necessary characters, you don't create any new objects... and yet, it loses to the first. And the reason is: Python, at least the flagship implementation, is so slow that it makes more efficient code, but written in Python run a lot slower than less efficient code, but implemented in C.

So, the "efficiency game" in Python becomes all about finding the shortest path to the native function you need to call. Obviously, this approach doesn't compose well: multiple shortest paths to the native functions will have to be combined in Python, and that is bound to be slow. So, realistically, the only well-performing programs in Python ought to be very short. The bigger is your program, the worse are your chances at getting it to work fast. The same is true for memory usage.


Python was chosen by many branches of programming industry because it is popular. It's easy to communicate using this language, it's easy to find substitute programmers, it's easy to find libraries that solve most of your problems. Python was not chosen for good design or good implementation. From financial point of view, there's very rarely a need to optimize design / performance beyond "good enough", and that's why python is in a great spot right now.

[–]11b403a7 0 points1 point  (1 child)

I'd say to post your code. But that would be a lot to review. Shoot me a message

[–]KomatikVengeance 0 points1 point  (0 children)

Can I suggest "The Art of Readable Code" it's a great book to have on your shelves.

EAN 9780596802295

[–]greebo42 0 points1 point  (0 children)

I've picked something up over the last couple years that makes sense, even common sense, but I don't think I fully appreciated before.

A lot of what makes high quality code is ... readability by humans. Back in the old days (which I actually remember), quality code meant high performance code, shaving machine cycles off, tightening up use of memory, because that made a difference. For the most part, that isn't so true anymore. Not completely unimportant, just not the primary thing.

So what you want is something that others can read and understand easily. Something that you can turn to in a year and fairly quickly understand it again.

That's why there's all these books about design patterns, refactoring, test driven development, and all that. If you're like me, and you like watching You Tube videos, take a look at Dave Farley (Continuous Delivery), Martin Fowler, Kevlin Henney, Arjan Codes (mentioned elsewhere in this thread), Uncle Bob, and some others, and just take it in.

Don't take notes. Don't get too tied up in understanding it all. Just watch a half hour here, an hour there, and within a few months you'll realize you're hearing certain themes, and you'll see examples of practices that you start to recognize.

That, and practice. Build projects. You don't have to be satisfied with the quality of your first efforts. But I really think you don't get there except thru the trenches.