Transcript for:
Understanding Abstraction in Programming

hi in this video we're gonna talk about one of the most important concepts in computer science and that is abstraction so what is abstraction well let me show you an example let's say I asked you what happened during last night's game well you could answer oh the Warriors won by 10 points boom great job is done I have my information I know what happened during last night's game but you're leaving out a lot of detail with this answer there's a lot of steps that went into the Warriors winning by 10 points that are being left out so you could go deeper and provide a lot more detail with your answer you could say something like Oh what happened during last night's game well first Steph Curry jumped up and grabbed the ball then he passed it to klay Thompson who dribbled it three times and then but at this rate you're providing a lot of detail and all I asked about is what happened during last night's game if everyone talked with this level of detail then we would never get anything done we'd be talking about games for hours on end I might be saying well to much information just tell me who won but to be honest we could go deeper things like jumped up and grabbed the ball those are pretty complicated processes in and of themselves that we could go deeper and explain so really you could say oh what happened during last night's game well first Steph Curry flexed his quad muscles and calf muscles in his left and right legs simultaneously while flexing his left and right triceps and squeezed his hands around the ball and then but this is this is insane you're providing so much detail about the the muscles flexing in his body when all I asked about is what happened during last night's game this level of detail is not appropriate for the conversation but honestly we could go even deeper because flexing quad muscles that is a complicated process and we could talk about the biochemistry and the reactions happening in muscles to make this happen but that is again just way too much detail TMI so by choosing to respond with the top answer the Warriors won by 10 points you have successfully used abstraction you've pretty much left out the unnecessary details so what is abstraction exactly abstraction is managing the complexity of a situation by abstracting away information in detail in order to focus only on the relevant concepts the world is pretty complex we could be talking at a lot of different levels of complexity but we abstract away those really low levels we don't want to about the muscles firing and the biochemistry reactions going on we just want to talk about the high-level concepts that happen during the game so this conversation is one example of abstraction and we do this we build out what we call layers of abstraction so in this conversation the high level of abstraction is the Warriors won by 10 points whereas the low level of abstraction is Steph Curry flexes quad muscles and we can keep going lower and lower and we can keep going higher and higher so we want to find the appropriate level of abstraction for the conversation how does this relate to computer science well in programming we'll use a type of abstraction called procedural abstraction and that is defining the steps of a program without worrying about exactly how each step is going to work under the hood the idea here is that imagine we're writing a program to hokey-pokey we could write the program as follows well you flex your left tricep then you flex your left pinky muscle then you flex your left and you talk about every single muscle you need to flex on the left side of your body but the problem here is that this is so complicated this program is going to be so long and it's going to be full of this nonsensical information about flexing each muscle instead what we want to do is write a high-level program like put your left hand in take your left hand out put your left hand in shake it all about this is much more simple and we can go in later and define exactly how you put your left hand in and you take your left hand out but at a high level this program is done we've solved it so this is procedural abstraction not worrying about exactly how each of these steps is going to work under the hood we'll go in and define that later if we have to so the idea here is that computers are very complicated and we need to manage that complexity if we're ever going to be able to accomplish anything with them so programming languages abstract away all of the complex details so when you're programming there's a lot going on under the hood inside the computer that we don't need to worry about the computer is processing millions of numbers per second and putting lights on a screen and flashing pixels and electricity is running through the circuit and we don't have to worry about that when we're programming because the programming language is abstract away those low-level details so let's look at the levels of abstraction in a Karel program let's say we wanted to make Karel move allow high-level we could just say move boom done we've writ caryl program but lots going on behind the scenes when we type move that move command is actually getting translated into a language called assembly that the computer understands and this is really complicated we don't want to be writing our programs in assembly that's that's a very low level but to be honest we can go even lower each of these assembly commands is translated into what's called machine code that's only zeros and ones so each command has a sequence of ones and zeros that it turns into and that's what the computer actually understands it's crunching those numbers to make everything on your screen happen but if we had to worry about all those ones and zeros when we're writing a carol program we would never get anything done no one would ever want to program because it'd be miserable and what's funny is assembly used to be the standard programming language there's a video game rollercoaster tycoon that was built 100% in assembly so we're getting better and better at making these abstractions so that we don't have to worry about these low-level things so programming languages give us the ability to speak to the computer at a high level we can give computer high-level instructions and all these low-level things are abstracted away so I think John goo tog who's a computer science professor at MIT said it best I love this quote he said the essence of abstractions is preserving information that is relevant in a given context and forgetting information that is irrelevant in the context that is perfect if it's not relevant for this context we don't need to worry about it we only need to worry about the information relevant here we only care that their word is one by ten points or we only care that Carol needs to move and turn left we don't worry about all the low-level things going on so how does this relate to us when we're programming well we can make our own abstractions functions are a great example of abstraction for example take building a tower there's a lot of different ways to build a tower and there's a lot of nitpicky details that go into building a tower we could build the bottom ball in the middle then the top we could do top than middle than bottom we could do middles and bottom then top so a lot of different ways to do this and each of these ways is going to involve a lot of moves and turn left sand turnarounds and turn rights so building a tower is a relatively complicated process would it be nice if we could just type out build Tower and Carol knew exactly what to do we can write that function and then we abstract away those picky details then we have this high level building block of build tower and that's what we use we don't have to worry about what's going on under the hood that way if we ever want to build another tower in the future we don't have to worry about those little details anymore they've been abstracted away we have this high level building block that's much more relevant to the program at hand so when you write functions you are using abstraction and functions are an example of abstraction so we say that a function is an abstraction abstraction is both a strategy we use abstraction and it is the result so a function is an abstraction so that is a crash course introduction to abstraction and it really just boils down to forgetting about the nitpicky details that are not relevant at the current context we don't want to talk about the muscles and Steph Curry's legs we don't want to talk about the binary instructions in the computer we just want to talk about the high-level ideas to get the job done so let's see some examples of abstraction so here we have our tower program from earlier that has Carol build a tower now if we look at my program it's nice and simple Carol moves Carol builds a tower and then Carol turns right so build tower and turn right are both great examples of abstraction we're using these commands in the program without worrying exactly how they work under the hood so if we run this we see that works great now to prove a point we could be using several different versions of build tower and the program would still work so we really don't have to worry about what's going on underneath so let's comment out this version of build tower and let's try this one so they're completely different functions that solve the program at different ways but it doesn't matter because either way the program still works so writing functions is a practice of abstraction talking in conversation is a practice of abstraction we're constantly abstracting away the low-level details and focusing on the topics at hand so we'll be seeing a lot more of this as we go through the course but abstraction is one of the most important skills in computer science we're constantly on a journey to find the right level of abstraction