Functions vs Classes: When to Use Which and Why?

preview_player
Показать описание
Functions vs. Classes?! Are you trying to decide which one to use in your code? In this video, I’ll explore how and when to use each of them, so you can make sure your code is as efficient and effective as possible.

🎓 Courses:

👍 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
- Dale Hagglund

🔖 Chapters:
0:00 Intro
1:11 Overview
2:29 Functions
6:02 Classes
9:00 Recap
9:48 Outro

#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!
Рекомендации по теме
Комментарии
Автор

Arjan just wanted to let you know that in the past year your channel played a huge role in helping me get significantly better at programming during my final year of college, and now working as a software developer. You're doing great work!

jordanmungo
Автор

This has been my question for YEARS! Thanks for making it simple, love your videos.

mariacardona
Автор

Last year I made a web scrapper that checked prices of products and used Object Oriented approach "just to keep the code organized". I did not have the need to spawn multiple instances of the scrapper and I was not storing anything in my scrapper instance, so I felt that I was using the wrong tool for the job, because there was no need to use any of the properties of an object, I was just calling its methods. Now it is much clearer to me why to pick one versus the another and I am very thankful that you made this video.

poneis
Автор

@2:35 I love it when Arjan always says: "I have a very simple sample application here"
Me: Eyes squinting: I have no clue what this does

alphenit
Автор

The duration of state is also an important factor. The rule can be formulated as follows: if you have state that needs to be retained for longer than one procedure call (and that state can be meaningfully encapsulated), then use a class. If the state is only needed for one procedure call, use a procedure .

MisterKorihor
Автор

My rule is: If it makes sense to encapsulate something in your own class (so interacting becomes easier and more intuitive), use classes, otherwise stick to functions.

dokick
Автор

I have watched hour long lectures/presentations and read more articles than i care to count, on the pros and cons of OOP and using classes, and this is the first time I've seen someone sum it up so well. Honestly I could never figure out why anyone would ever use classes because they just seem to add a lot of complexity compared to the functionality they provide, so they always looked inferior to just using functions. This makes it so much easier to figure out where to use classes.

Curatiokyte
Автор

I just can not believe how every line of code in your videos is useful and worth to even dive deeper in the logic you create! Best of the best!

ledempire
Автор

It is one the most simple and useful explanations of the difference between function/class usage cases
Lots of thanks!

andriistefanenko
Автор

Classes are used to model nouns (objects): employees, products, customers, etc.
This matches well with relational databases which are also designed around nouns. A product table with a unique ID for each product and a customer table with a unique ID for each customer.The state of the nouns are critical in the database. It is natural to use OOP to represent the entities stored in the database.

marksegall
Автор

I also find classes to be useful ways to hold data and define data structures (in this case, dataclasses), which can be passed in as arguments to functions and instances of classes. Thanks for another fantastic video Arjan.

rossursino
Автор

I also find myself mixing the approaches quite often. In a web backend with routers, for example, I want both states (classes) and pure actions. Pure actions would be a crud module with action-focused SQL statements (or ORM code), while states would be a schema module with classes that hold the state of the request and response.

Additionally, the ease of testing argument can not be said enough. The amount of mocking and pre-test setups you have to do varies significantly by the amount of state in your code.

All in all, really great video, I agree with all points made.

veni_vidi_victorian
Автор

I recently discovered your channel and I can honestly say; the more I watch you and type out the codes you wrote... The better programmer I have become. THANK YOU!!

divine-favouranigbogu
Автор

I taught myself python for tool creation for Maya and recently I was asked why I don't use classes as much as functions ( I barely use em). I didn't know how to answer but now having watched this has helped me wrap by brain around why, since my maya tools are mainly action oriented. Thanks for a great video!

ZGarage
Автор

I feel you can sometimes use classes to making functional programming cleaner. I was developing an automated report for work. The program would calculate some data, then either send a daily report or an invoice at the end of the month. Completely stateless, so a functional approach made sense. Different clients received their own version of the daily or monthly emails so we would need to write the same types of functions for each client. At the end of the day, it was simpler to create an interface which bundled all of these email-sending actions together. For each client, we used a different implementation of this interface. Technically it was an abstract class rather than an interface since Python doesn't have interfaces.

ilangated
Автор

Can't go wrong with functions, its that easy.

Georgggg
Автор

I love your humor. You are literally a meme guy.
Thank you so much for your hard work and contribution to the Python community

YaroslavOliinyk
Автор

Well described. I spent months coming to this conclusion, I wish I had your video when I started. One big reason I like to use functions with separate data structures, is that it can help to keep Inter Process Communication simple. I use a shared memory approach for performance and I think the OOP model (which tends to include exposing data) would be awkward in an IPC Python system.

calum.macleod
Автор

Awesome content! As a self-taught programmer who first learnt functional programming, I often struggled to see the benefit of OOP. This is a super helpful framework for thinking about the differences between the two.

morgankerle
Автор

As a Go developer I face the same challenge. Generally I start with functions, and find that I "step up" to data classes as needed. But truthfully, using both functions and data classes together is the answer. Personally I find data classes easier to test since they can implement interfaces, which allow for the easy creation of mock/fake implementations. Functions, on the other hand, can be harder to test, especially when they're coupled to other parts of the system.

Also, in Go, a type introduces a new namespace/scope for its methods, which is valuable when the package you're working in is already heavily populated with functions. With no overloading of methods or functions, managing the namespace is critical.

simonarcher