Why Use Object-Oriented Programming?6:02 with Alena Holligan
Ok, you probably already know you should learn OOP, but in this video we’ll introduce you to some of the things you’ll be able to do once you understand OOP and how that can help you to become a better developer.
A programming paradigm is a style, or “way”, of approaching a problem to come up with a solution. The two main paradigms in PHP are procedural and object-oriented.
No one programming paradigm solves every programming problems, but the more you know about different paradigms, the better prepared you’ll be to choose the right tool for the job, and write cleaner and more organized code.
Bad / “spaghetti code” that weaves all over your application and is complicated to follow, can be written in any paradigm, including OOP.
Tenets of Object-Oriented Programming
The DRY principal or Don’t Repeat Yourself. When you repeat the same code in multiple locations, updating becomes a nightmare. Maximize reusability, minimize repeating code.
The KISS principle: Keep It Simple Stupid. Don't use fancy OOP features just because you can. Use fancy OOP features because they have specific, demonstrable benefits to the problem you're trying to solve.
- Sooner rather than later, you’ll have to work with object-oriented code.
- Contributing to open source software.
- Gives you various ways to think and solve problems.
- More easily translate your programming skills into other Object-Oriented languages.
- Become a more valuable developer.
There are three things I want to clarify as we begin this section 0:00 on why to use object-oriented programming. 0:03 Number one, a programming paradigm is a style or 0:06 way of approaching a problem to come up with a solution. 0:11 The two main paradigms in PHP are procedural and object-oriented. 0:15 There are other paradigms, such as functional programming, 0:20 that we won't be covering, because they're not as prevalent in PHP. 0:23 Number two, no one programming paradigm solves every programming problem. 0:27 But the more you know about different paradigms, the better prepared you'll be 0:33 to choose the right tool for the job, and write cleaner and more organized code. 0:38 Which brings us to number three, 0:44 bad spaghetti code that weaves all over your application and 0:46 is complicated to follow can be written in any paradigm, including OOP. 0:51 So no matter which style of code you're using at the time, 0:57 keep in mind these two tenets of programming. 1:01 The first being the dry principle, or don't repeat yourself. 1:03 I already mentioned this once before. 1:07 While repeating oneself can be a good tool for 1:10 emphasizing an important topic, repeating code is not a good way to program. 1:13 When you repeat the same code in multiple locations, updating becomes a nightmare. 1:19 Maximize reuseability, minimize repeating code. 1:24 This can be done successfully, whether reusing a function or reusing a class. 1:28 The second important tenet of programming is the KISS principle. 1:34 Keep it simple, stupid. 1:38 Don't use fancy OOP features just because you can use fancy OOP features, because 1:40 they have specific demonstrable benefits to the problem you're trying to solve. 1:47 Now that we've clarified these important distinctions, we're ready to talk about 1:53 the many reasons for actually learning object-oriented programming. 1:57 First and foremost, in the real world, sooner rather than later, 2:01 you'll have to work with object-oriented code. 2:06 Even in the procedural courses on Treehouse, we've used objects, 2:09 and the same will be true for other projects you work on. 2:12 The better you understand object-oriented programming, 2:16 the more effective you'll be as a developer. 2:19 One of the main benefits of object-oriented programming 2:23 is its modularity. 2:25 By keeping different sections of a code base separate, you are able to change 2:27 one section of the application without affecting other sections. 2:31 This can be extremely helpful when working on a large project or on large teams. 2:35 Because it allows one programmer, or 2:41 group of programmers, to be responsible for one section of the code 2:43 without needing to have a comprehensive understanding of the entire project. 2:48 This can also increase the reusability of the code. 2:53 Because a well designed object has only one specific function or responsibility 2:56 and is, therefore, completely independent of the environment it is placed in. 3:01 Thus, it can be replaced and reused easily. 3:06 This modularity also allows large applications to be more easily maintained. 3:11 These separate pieces can be tested independently, so 3:16 less bugs make it into production. 3:19 When all the code dealing with one specific feature is in one place, 3:21 the code is more maintainable. 3:26 When a bug is found, generally, the issue takes less code to fix. 3:28 Modularity can also help with scale ability, 3:33 both in terms of personnel and hardware resources. 3:36 With a smaller codebase to learn, a team can be scaled up more quickly and 3:40 be more effective. 3:45 Having separate sections can also allow an application to allocate hardware 3:46 resources more precisely and maximize the return on investment. 3:51 Because of these benefits, most modern PHP frameworks, such as Laravel, 3:56 Symphony, and Zend, are written using object-oriented methodology. 4:00 Working with a framework is like having a large portion of a puzzle done for 4:05 you, so that all you need to do is fit those chunks together, 4:09 instead of having to place each piece individually. 4:14 Using a framework can help you launch a project more quickly 4:17 than developing from scratch. 4:20 Frameworks are not the only projects using object-oriented programming. 4:22 In fact, most modern PHP projects are written using OOP. 4:26 By adding OOP to your skill set, 4:31 you'll be able to work directly with the many great projects out there. 4:33 Even Drupal went object-oriented with a major rewrite in version 8. 4:37 Your object-oriented skills will give you a stronger ability 4:42 to use external packages in your own projects. 4:45 You'll also be prepared to write plugins for 4:49 other projects and contribute to open source software. 4:51 Besides the benefits that object-oriented programming can bring to your projects, 4:56 there are many benefits to you as a developer. 4:59 To start with, contributing to open source software is a great way to learn and 5:03 get feedback on your code. 5:08 Contributing is also a great way to get exposure and demonstrate your skills. 5:09 Learning a new language, or a new paradigm, can be one of the best ways 5:15 to increase your skills as a developer by giving you various ways to think and 5:19 solve problems. 5:24 By learning object-oriented programming, you'll be able to more 5:25 easily translate your programming skills into other object-oriented languages. 5:29 Having a wider range of skills makes you a more valuable developer. 5:34 I could go on, but I'm getting hungry. 5:38 Hopefully, you can start to see the benefits of using object-oriented 5:40 programming and are ready to dive into the syntax involved in setting that up. 5:43 Along with programming, I also enjoy cooking. 5:49 I've included some of my own recipes in this project. 5:52 So when you're done, I hope you enjoy some of our family favorites. 5:55 We can't start cooking until we finish our coding. 5:58 So let's get started. 6:01
You need to sign up for Treehouse in order to download course files.Sign up