The Ultimate Guide to Writing Functions

preview_player
Показать описание
Over the years, I’ve seen a lot of code by different people, and my conclusion is that writing great, well-designed functions is difficult. So, I made this guide, which includes the 7 most important things you should do when writing functions.

👍 If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

👀 Code reviewers:
- Yoriz
- Ryan Laursen
- James Dooley
- Dale Hagglund

🔖 Chapters:
0:00 Intro
1:11 Tip 1: Do one thing and do it well
5:17 Tip 2: Separate Commands from queries
6:53 Tip 3: Only request information you actually need
9:27 Tip 4: Keep the number of parameters minimal
14:55 Tip 5: Don’t create and use an object in the same place
17:24 Tip 6: Don’t use flag arguments
19:59 Tip 7: Remember that functions are objects
22:56 BONUS: Tips for naming functions and parameters

#arjancodes #softwaredesign #python

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!
Рекомендации по теме
Комментарии
Автор

If only I had this when I first started out. The amount of useful information you were able to so succinctly squeeze into a 25 minute video while keeping things clear and accessible is outstanding. Fantastic Job.

Ziggity
Автор

After tulips and cheese, I really thought you were going for windmills, but nope 😂. Great video, as always!

marcioneto
Автор

Crazy value in less than 30 mins! Not only you know what you're talking about, you also know how to deliver it in simple terms. Thanks.

hovikkevork
Автор

Didn't know those partial functions!!!

weistrass
Автор

Guido Van Rossum initially named his language "stroopwafel", but it didn't take off because the name is too long.
While he was visiting a zoo and thinking ideas for a shorter name, a snake ate his stroopwafel.
Guess what type of snake it was. (I cannot give you any hints, because stroopwafel didn't support type hints).

StavrosSachtouris
Автор

You could've added the cc_validation function as a method for the Card class, and even turn it into property, or validate it upon instantiation in the init, will make even more sense.
Great content, keep going ❤️

mohammednasser
Автор

Summary of this video:
== TIPS ==
- Functions should do one thing, and one thing only
- Side effects (commands) or computations (queries), not both
- Request only what you need
- Minimal parameters
- Separate creation and usage
- No flag parameters (split up functions instead)
- Use higher-order functions

== NAMING ==
- Split up functions with `and` in their name
- Shorten
- Names should be verbs, arguments should be nouns
- Always use the same vocabulary
- Use the languages preferred_naming_scheme and notTheWrongOne
(so never touch C#)
- No spelling or grammar mistakes (duh)

angelcaru
Автор

I have followed you for a couple of years and this is my favourite video. You are getting so clear and concrete with your points. Functions did deserve a video like this!

hcubill
Автор

Nice. Very good advice about separation of concerns. Thank you.

wizardfix
Автор

Hello Arjan. I have been following your tips for a while now. It’s completely changed the way I write code and I have received accolades at my work for the significant improvement in code style. I want to thank you very much for it. Please make more videos like these.

shashankbang
Автор

Thanks for the great video.Especially I liked the trick to force keyword arguments. And the closed open principle.

uwegenosdude
Автор

Glad you mentioned there should be a verb in a function name.

def verb(direct_object, indirect_object, ...): ...

Or for methods, subject.verb(objects).

Been trying my best to be consistent with using normal grammar to make my code make sense, like using plurals for collections to make loop variables easy to name.

qwerty
Автор

Really gooood video!!!!

To @GrammarNazi, at work we happen to have a Django model named “Custom”. Because classes should be singular names in Python... and someone thought Customs was a plural 🤦‍♂. We still can not safely refactor that.

_baco
Автор

It's very interesting for me to step into the mainstream Python developer world on YouTube. I'm a scientist and I write tons of "complicated" Python (and Cython and C++) code but the complexity is very different in scientific code - it's in the math and the algorithms rather than the stack of code that manages other code as seems to be common in mainstream codebases. We use abstractions like classes of course but our hierarchy of abstractions is much less branched than here, more like a stack than a web. We like our input files with dozens of parameters (🙂), which flows into some main, which calls some objects and functions, that kind of thing. This comment in itself is not a criticism of common design patterns - I don't feel qualified to criticize them, but I do note how difficult it is often to find the code that actually _does_ something in "professional" codebases. There is so much fluff that is supposed to manage the complexity that it becomes quite complex in itself. That has always frustrated me. Good abstractions are hard to come by, and mainstream code is full of bad abstractions for the sake of abstraction. Sometimes it's better just to _do_ the thing you want to do, rather than erect an entire class of middle managers, usually classes, with abstract-sounding names. If your core algorithm is ten lines or so (as is amazingly often the case by the way), there is no justification for writing 5000 lines across 14 source files to "manage the complexity", but that's often what you see. Bottom line is when you manage complexity you introduce complexity, and that has to be controlled if you're going to make the code friendlier to the developer and user. As you may have guessed, I'm a big fan of Brian Will's diatribe on OOP, not that I think classes are bad (he doesn't either) but that I think most abstractions are bad, and cementing them in with further bad abstraction is a bad idea. Just some thoughts from a scientific programmer - I live in a very different world from all of you, but maybe I have some useful insights because of that. I don't know. You decide.

danielkonstantinovsky
Автор

The Dutch guy said order salad instead of fries Holly crap haha.

The first time I visited Holland my culture shock was how much fries is consumed. I did not connect the Irish historical famine with the current reality.

Love your style and had a blast in Amsterdam. Keep up the good work and thank you for putting these together!

EAenergy
Автор

Nice! You've taken some of the most fundamental clean code practices and distilled them down to a very coherent and concise 25 minutes with no hand waving or fluff. Uncle Bob would be proud =)

Personal-M.I.S.
Автор

Just to say, I've only dabbled in Python and don't know much more than the fundamentals of the language, but I still get A LOT out of watching your videos. I really appreciate how your presentations are so chill and straight to the point. Even when I can't quite tell what's going on with the language, I can get the idea you're trying to teach. Good stuff!

Personal-M.I.S.
Автор

These videos are very necessary, thanks!

mikelancaster
Автор

Very juicy video, and at the same time funny, especially the joke about PyQt 😂. Thanks!

mohammadsharara
Автор

I'm excited for this one! I've always wondered if I was going about functions in the most efficient/optimal way and I'm sure these tips will guide me to be better.

heitortremor