Programming code has evolved quite a bit since the early days of computers. In this video, explore a high-level overview of procedural and object-oriented programming.
we can create our classes and our objects using darts but why do we actually need them anyways? What purpose do they serve? Well in order to understand this, we first have to understand how computers actually work. Behind the metal shell and the pretty keyboard and the nice screen, a computer is just composed of a large number of switches. I have to write zero one zero zero zero zero zero one and that is very painful and very time-consuming. This is what we would say is machine code. So we came up with all sorts of ways of making it easier to write programs that sound and look more like english. So that we can actually interact with the computer without having to get down to the machine level and start writing ones and zeros. In the early days a lot of the programming languages were what we would call procedural. They would go from top to bottom. So do this, then this, then this, then this, then this until forever basically. And this is kind of easy to understand but as your code gets more and more complex then it gets a bit hairy 'cause it's hard to know when something goes wrong what exactly went wrong? Because you have to follow the logic from top to bottom and you have to really untangle it and you can really shoot yourself in the foot quite easily. So in a lot of places complexity is always the enemy. As humans we have pretty capable brains but we can only hold so much information in it at one time. The more complex a program gets, the more likely it is that it will crash and it will have problems and it won't perform very well. So along came this concept of Object Oriented Programming. And a lot of people really love the concept. So languages that became really big such as Oracle's Java or Apple Swift or in fact Dart which we're working with are all Object Oriented Programming languages. So what does that mean? Well it means that we're working with objects to do everything that our App or our program needs to do. And for those guys who are looking for a job as a software engineer, one of the most common questions you get asked is, what are the four pillars of Object Oriented Programming? what is Object Oriented Programming all about? And the four pillars you'll very often hear people reel off or abstraction encapsulation inheritance and polymorphism. Now it's a mouthful but very few people actually fully understand what these big words actually mean. So I want to break it down and show you in the context of our App why OOP is so great and how does it apply to Dart. One of the first pillars is Abstraction. How can we make something that's complex into smaller pieces to make it more Abstract? Now if you've taken my courses before you know how much I love food. So in order to understand Abstraction we're going to talk about you guessed it food. One of my favorite restaurants in Japan are these really tiny kind of hole-in-the-wall places where there's only maybe four or five seats but there's also just one guy who is the waiter, the chef, the cleaning staff and he just does everything from behind the bar counter. When you make an order you do it on a vending machine and you give him the order and then he'll cook your order he'll put it on the table for you because it's so easy to reach and then once you're done he'll take your bowl and he'll put it in the dishwasher. Now this is really cool but there are some limitations. He can't have more than say ten customers right? Because it will be complete nightmare to try and do all of those jobs himself. And that's kind of the downside of that. If we have a large piece of code that is trying to do many many things then it's kind of like just having one employee in your restaurant who's cooking, who's waiting on staff who's taking payment at the cash register who's also taking reservations and bookings and firstly good luck finding somebody with the CV that can do all of those things and if you hire them you're going to completely destroy them because one person shouldn't have to do so many things but I find that this is much easier to read once we start building up a large bank of questions. Speaking of a large bank of questions it's not much of a quiz App with just three questions right? So if you open up your README file you'll notice that we've already included some pre typed questions. There's 13 of them and if you copy everything between the backticks all of those questions, you can paste it and replace what you have in your question Bank at the moment. And they're already formatted so that we create a new question object with a question text and an answer. Now all we have to do is to go into our main.dart file and fix the errors that we have here. Because we deleted our question Bank from our main.dart file this no longer exists and it tells you as much. But how can we refer to our quizBrain? Well we have to create a new quizBrain object. So first we have to go ahead and import our Quizbrain.dart so let's put that in there and now we're able to create a new QuizBrain object which is going to be called quizBrain and notice that we name objects they start out with a lowercase. And we're going to set it to equal a new QuizBrain object. So that construct our new object which we can now tap into by referring to quizBrain. Down here where we have our questionBank, instead of using just the questionBank which should exist locally, we're going to write quizBrain.questionBank because our questionBank is now a property of our quizBrain object and it's right here it's a list of questions. Now we no longer need to know about questions in our main.dart file and we can delete this unused import. Notice that every time you stop requiring some code in an import it'll tell you by making it gray and giving it a squiggly line. So let's delete that and we now need to only know about quizBrain inside our main.dart. Now we've still got a few more errors down here so my challenge to you is to try and fix everything and run it to make sure that it still works in the same way. Pause the video and give that a go. Alright so down here we're going to do exactly the same as what we did before because our questionBank is now a property of our quizBrain we have to use the dart notation instead. We have the right quizBrain.questionBank and similarly down here we have to write quizBrain.questionBank. So that we're actually tapping into the actual questionBank. Now even though we had to add a little bit more code in our main.dart at least our questionBank which is now pretty massive is separated into a separate class and this now abstracts some of the functionality namely the Brain for our quiz into a separate object. And now we've abstracted our code just a little bit more and the other benefit is that say if we were to create a different quiz, so if we were to create a sports quiz for example we don't have to change anything about our main.dart we just have to provide a new file say a sport quizBrain and everything in here will still work the same and we just have to change the class that we're using to construct the quizBrain. So you could have a quiz that has lots of different types of quizBrain sports and news and general knowledge and every time you want to switch between one to the other, you just change the object that you're actually using for the current quiz. So we've made our code base more reusable, more modular and we've separated different jobs into different classes. Now if we run our App, you can see that we now have more questions. 13 to be precise but here's a problem in our quiz App, let's say that we really hate losing. Well we can actually change the answer to our questions. So for example here we're trying to get the correct answer by tapping into our quizBrain looking at the questionBank property and picking out the current question, out of the questionBank then tapping into the answer and this is what we would call getting. We're getting the value of that correct answer. But equally I can also change it I can simply write quizBrain, questionBank, correctAnswer and I can set it to a new value. I can set it to whatever it is that I picked which in this case would be true right? Well, if this happens before I check my answer then if we hit save and we take a look at the actual correct answers and what I get as a result here, you can see that we're on this question about the loudest sound produced by an animal and the correct answer should be false but I'm going to press true anyways. And it tells me that I got it right. So how is it possible? I shouldn't be able to just change that even if I want to be right all the time. This is not very safe for our App. Let's comment that out and in the next lesson, we're going to learn how we can make our code a bit better and conform with the second pillar of OOP which is encapsulation. So for all of that and more I'll see you on the next lesson.
This course was created by London App Brewery. We are pleased to host this content in our library.