FP vs OOP | For Dummies

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
functional programming or object-oriented programming what are they what are the differences and which one is better let's start off with functional programming take the function f of x is equal to x plus five something commonly seen in classes like algebra or even high level calculus the function f x takes in x as an input parameter and returns x 5. f of 10 returns 15 f of 20 returns 25 and so on if desired these functions could have multiple parameters the function sum which takes in x plus y takes in the two parameters and just returns their sum sum of 30 and 50 returns 80 and so on this same concept of functions is widely used in programming except its abilities in scope are heavily expanded functions can be named almost anything and an infinite amount of parameters can be passed into them they can also return anything imaginable for example functions can be made to accept a number as a parameter and then take that number and search for it within a database and return the result we could also have a function that takes in a piece of text as a parameter and returns it encrypted but let's get back to functional programming a number of requirements need to be met when programming in this way these requirements are almost all functions only no side effects and a completely fixed control flow to explain some of these parameters let's look at a simple problem let's say we want to write a function that takes two numbers and just returns their sum squared we'll take two approaches to this problem one functional and one less functional or imperative let's start by defining a function called squaredsum which takes in two parameters x and y within the function we define a variable called sum which stores the sum of x and y then define another variable called square which holds the value of the sum raised to the second power finally the function returns the variable squared so if we were to pass 2 and 3 within the function squared sum the variable sum within the function would be 5 and the variable squared would be 25. finally the function would just return 25. so the value squared sum 2 3 would be equal to 25. let's rewrite squared sum now down to a single line all we do is return x plus y raised to the second power without storing any variables can you see the difference between these two functions in the first approach we decided to store intermediate steps to the final solution within variables before returning the answer the problem with using variables like this is the production of quote-unquote side effects when looking at how a computer would solve the first version of squared sum the variables created sum and square have to be remembered by it and stored somewhere in the system's memory this means that the function when evaluating a given set of numbers produces events outside of its own body and affects something else in another location which in this case would be computer memory these are called side effects events that are caused by a system within a limited scope whose effects are felt outside of that scope when systems grow and expand the occurrence of side effects like these can be problematic due to the sheer scale and complexity some of these projects can get to which would contain countless different functions producing side effects it would become increasingly difficult to debug systems and find exactly the source of some given problem this is one of the benefits of using functional programming the side effects are eliminated completely allowing for weird and unexpected bugs to be minimized and for developers to more easily understand the operations of a given system now let's look at the second version of squared sum in the function no variables are used and the solution is directly returned without causing any side effects no extra elements are created that would need to be managed by system memory outside of the function scope and no other state or object is changed outside of the function with this approach input is directly mapped to output the parameters x and y are taken in and their squared sum is directly returned without any adverse effect or event taking place in other words this side effectless approach has a strict control flow which is a critical part of functional programming to recap fp is a way in which a developer can solve a problem this approach includes pure functions meaning functions that don't include side effects and a strict control flow meaning directly mapping input to output this style of programming is great for projects that don't tend to scale out which would make them easier to maintain test debug and refactor however there are cases where object-oriented programming would be a better style to approach certain problems let's say i want to create a game involving a farm that has a bunch of animals what would be a good way to go about programming them well for each individual animal i could write specific code over and over again manually typing out all the redundancies between them and continuously repeating the process for every new animal i would like to add into the farm this approach is not ideal for every animal i have to start off from scratch and continuously program every feature manually if for instance i wanted to add 100 animals to my farm this approach would take a vast amount of time and wouldn't be considered efficient additionally after i have programmed all my animals what if i discovered some bug in the way i designed them i would have to go back through each animal and rewrite their code wasting even more time what if instead of individually programming each animal from scratch i create a generalized blueprint of what an animal typically is and then apply that blueprint to every animal i create meaning i only have to program what an animal is once and apply that set of rules to each new animal automatically this is a more object oriented approach the basic idea in this style of programming is the creation and use of these blueprints which developers call classes a class is like an abstract blueprint that is used to describe some data with attributes and methods once a class is constructed instances that inherit from these classes can be created called objects each instance of a class can pass in data that the object stores and manipulates according to the blueprint and so one can have multiple entities inheriting from the same class but holding different data so going back to the farm i can create an animal class that can accept certain attributes like fur name age sex number of legs and so on we can start off by making a dog class this blueprint will accept the following parameters breed name age sex and favorite treat with this basic template we can now create objects of dogs inheriting from our dog class by passing in values as parameters for the attributes of a dog each different instance of the dog class created here is held in the three variables dog one dog two and dog three dog one's name is rusty and he's a terrier dog two's name is milo and he really likes bacon dog three's name is bella she's only two years old as can be seen instead of having to write a bunch of code for each dog it only took one line of code for each new dog to be created inheriting from our dog class within each instance i pass in attributes and they automatically get instantiated as the properties of a dog another feature classes have besides storing attributes is methods methods are functions within a class that can change the state of an object let's say do not like dog one's favorite treat anymore how could i change it well i can add a method to our dog class called change treat that takes in a new treat and updates the dog's favorite treat attribute to the one given methods are like procedures within a class like change tree that upon being called can change the state of an object by modifying the object's attributes with the line dog1.changetreat we update the dog's favorite treat attribute of the object dog 1 to stake note that the act of calling this function produced a side effect because the function change treat changes something that is outside of its own body this isn't necessarily a bad thing however it's just something that's avoided in functional programming for the rest of the farm i can now make a cow pig and go class with similar attributes like the dog class now i can produce as many of these animals as i want with minimal effort if i wanted to change something about the animals or fix some bug i simply edit the classes i created and the new changes will automatically get inherited by all the existing objects now that we've seen examples of each paradigm which one is actually better well it depends object-oriented programs are good when you have a fixed set of operations on things while functional languages are good when you have a fixed set of things and as your code evolves you primarily add new operations on those existing things so to conclude there is no objective winner to the debate we cannot simply say one is better than the other both styles of programming excel in their own ways and each one has its advantages and disadvantages over the other
Info
Channel: BasicOverflow
Views: 11,444
Rating: 4.772296 out of 5
Keywords: FPvsOOP, FP, OOP, ProgrammingConcepts
Id: 08CWw_VD45w
Channel Id: undefined
Length: 8min 42sec (522 seconds)
Published: Sat Apr 24 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.