Functions vs Classes: When to Use Which and Why?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
when do you use a class with methods and when do you use functions in a module if you pick wrong your code will be more complicated than needed which makes it harder to maintain leading to bugs that potentially accidentally shoot rockets at other countries leading to World War 3 and in worst case one country develops a weapon that creates a supermassive black hole that swallows up the entire universe and then we'll never get that reboot of Babylon 5. I'm sorry worst case scenario today I'm going to share how I decide between functions versus classes and help you improve your code so I can finally watch the shadow of warlon war with modern CGI so thank you in advance for watching this entire video and actually applying what I'm going to show you if you want to become better overall at fixing problems in your code I have a free workshop on code diagnosis if you want to join go to ariana.com diagnosis it's about half an hour teaches you a three three it teaches you three Factor framework to help you review your code more effectively and I actually show you how it works by using production code so iron.codes slash diagnosis to get X for free the link is also in description of this video before talking about how you decide when to use a function versus when you use a class we need to better understand what the difference is first functions functions take input arguments then do something and then they return a result and you can then take that result and pass it to other functions functional languages like Haskell even allow you to pass functions to functions or let functions return other functions it can get pretty complicated but what it comes down to is that your whole program is organized around how data flows and what you do with it functions are action focused object-oriented programming languages like Java are different at the core it's all about structuring information so you have variables that are grouped into objects which are part of other objects to get a whole hierarchy that way you can even use inheritance to an extend the definition of these object structures and together all the objects represent the state of the applications and methods modify that state so object oriented programs with classes are State focused so how do you decide when to use which well use functions when your code is action focused and use classes when your code is State focused thanks for watching and see you next time okay maybe I should actually explain what that means let's take a look I'm going to start by an example where I think functions make the most sense so I have a very simple example application here that analyzes some data so I have CSV file here that's actually the result of the stack Overflow developer survey and a simple script that analyzes the CSV file we're looking at the most common jobs as well as the most common programming languages that have been used if you take a look at how this has been set up you see that we have a couple of functions so this counts the cumulative appearances of a certain value in a particular column we have a show frequencies function that takes a counter and that prints the frequencies we have a function that shows all the answers all the possible answers that are within the data set we have a function to analyze the frequencies that cause these other functions and then we have a main function that opens the file and then again calls the other functions so here you see this is really action focused we wanted to do things in sequence we don't really care about how everything is structured we really care about the flow so we start by loading the data then we do an analysis then we do a second analysis and we want to print that information so it's really a typical example of a program where we're mainly concerned with actions with the flow of information and you see that functions here are actually a really good way of doing this because well it's a pretty short file it's quite logical to organize the code into functions here if you were to use classes you would probably end up with a gold class that contains everything and then these things methods and we wouldn't really have instance variables because well the example does not really concern itself with state so with classes that this would actually be harder to read I think with functions it's much simpler when I run this group we see quite a few interesting things for example that third of the people is fully remote and about only 11.7 percent is in person and of course that's also due to covet I'd be interested to see what the difference is going to be with these numbers when we ask the same question in 2023 but since it is currently 2023 at least when I'm recording it we're gonna have to wait a while until we get those numbers second thing is the language that people work with so we see at the top we have JavaScript HTML SQL and python is also up there quite a bit but JavaScript is by far the popular so I should probably change my channel to use JavaScript although there's already like a bunch of different channels that are JavaScript focused still though python is one of the most popular languages as well so we have nothing to complain about personally nowadays I tend to go more for functional Solutions than for object oriented Solutions I feel the code is simpler overall also if I write functions I find that they're easier to test especially if I try to turn these functions into pure functions which means that they're working independently of the rest of the code they're not modifying Global state or anything and then it's also quite easy to write unit tests for them so that's one advantage of not going for a state-based approach which is what object oriented programming is but really going for a flow action based approach because in the end that's also sort of what it tests are it feeds something to a program and it observes the result to verify that it's actually doing what it's supposed to be doing so typically if I can I'm going to use functions if I have to I'm going to use classes so here I have another example where actually using classes makes more sense because this is more about the structure of things in not so much about the flow of information so what do we have here well I have a couple of classes so one is an enumerator types not really a class but it's a type representation that in Python we use a class 4 we have a transaction in this case I'm using a tuple so that's another example of using structured information and that's why python is so nice because we have these type of structures like tuples and dictionaries to also help us with structuring information we have a custom balance error which is also a class but most importantly we have a bank account which is a class that gets an initial balance and that maintains a list of transactions it's the transaction history and then we have methods like deposits and withdrawal that modify the bank account and there's a couple of other things like we can transfer amounts between bank accounts we can check whether the balance is sufficient and a couple of properties that are helpful when you're when we're dealing with bank accounts so on the main function I create a couple of bank accounts I withdraw some money and deposit some money I do a transfer and then I print some information and you can see the result of that right here and the reason why classes are useful here is that bank accounts represents State and because we have a class we can create objects of that class and we could have for example multiple bank accounts if you were to solve this with functions it would be way more complicated because well bank accounts are State focused they're not action focused so that's one case where object oriented programming makes a lot of sense you'll also find quite often that an object-oriented approach works well if you're dealing with modeling real world Concepts and you need some representation of real world things in your program like let's say you build a real estate system you might have houses and buyers and mortgages and other mortgages are not really real like not physical but they're like things that exist well that doesn't make any sense anyway this type of program is more more suitable for a state-based approach for an object-oriented approach I mean imagine if you wanted to create like 10 or 20 bank accounts you would have needed like tons of variables if you were to follow a functional approach so this makes way more sense and then of course you could decide to extend on these classes by using inheritance although typically I would avoid using very deep in her inheritance hierarchy's like I also mentioned in lots of my other videos and you want to be careful especially with object oriented programming to really judiciously judiciously apply design patterns and principles and I did a bunch of videos about that for example here's a video in the top where I talk about the solid design principles it's already quite an old video but it's still quite relevant especially for object-oriented programming now like I said that's the nice thing about python is that we don't have to pick between functions and classes you already see that in the classes exam example that well we do have classes here like this class on and particularly the bank account class but we're also using tuples for simple structures like a transaction which is just a type a date and an amount and we also use functions like the main function here so using a careful combination of these things makes a lot of sense in most cases and that's what's nice about python is that we have some flexibility to do so we're not like with Java tied to everything having to be a class we can introduce functions whenever we feel that is necessary or most useful it's one of the reasons why I like using python a lot so it's a brief overview of the difference between functions and classes I hope it helps you pick the one that fits better with what you need so if the program that you're writing is more focused with the flow of information like processing data in sequence then using a more functional approach is going to be better and it's also going to make it easier to write tests for it if it's really more focused on state like the bank accounts example or you have collections of objects that need to be structured in some way and you have a few methods that modify those objects that the class-based approach will typically be better but don't hesitate to experiment with this and try different approaches try different combinations of approaches because ultimately that's going to help you understand these topics better and will lead to a better result in the end so if you're using classes in your program then I highly recommend that you start using data classes as they're awesome and I did a video recently about data class you can watch that right here thanks for watching and see you next week
Info
Channel: ArjanCodes
Views: 138,555
Rating: undefined out of 5
Keywords: functions vs classes, data classes python, writing functions, dataclass, dataclass python, functions, programming functions, python functions, functions in python, function vs class, function vs classes, functions and classes, functions and classes in python, functions or classes, python programming, python tutorial, python classes, python functions 101, functions in python language, python classes and objects, oop python programming, object oriented programming in python
Id: txRTzljmV0Q
Channel Id: undefined
Length: 10min 48sec (648 seconds)
Published: Fri Apr 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.