Monday, December 23, 2019
4 Benefits of Object-Oriented Programming
4 Benefits of Object-Oriented Programming4 Benefits of Object-Oriented ProgrammingLike parachute pants and Pac-Man, OOP (object-oriented programming) got its start in the 1980s. But unlike trendy fashions and video game characters, this programming model is still going strong after these many years.OOP has become a fundamental part of software development. Thanks to the ubiquity of languages like Java and C++, you cant develop software for mobile unless you understand the object-oriented approach. The same goes for serious web development, given the popularity of OOP languages like Python, PHP and Ruby.Getting your head around the idea of object-oriented programming can be challenging for some IT professionals. You may be wondering why you even need objects when you could use the top-down approach of traditional structured programming in languages like Visual Basic.If youve written this kind of software, youre probably used to breaking down large problems into sub-problems and solvin g them in separate units of code. Or you may have experience with functional programming, which treats elements of code as precise mathematical functions, and prevents them from affecting other elements, i.e., no side effects.Get to grips with OOP, however, and youll see that its a whole new way of solving problems. The basic concept is that instead of writing a program, you create a class, which is a kind of template containing variables and functions. Objects are self-contained instances of that class, and you can get them to interact in fun and exciting ways.The advantages of object-oriented programming lie in this kind of encapsulation. Heres a detailed look at some of OOPs top benefits1. Modularity for easier troubleshootingSomething has gone wrong, and you have no idea where to look. Is the problem in the Widget file, or is it the WhaleFlumper? Will you have to trudge through that sewage.c file? Hope you commented your codeWhen working with object-oriented programming language s, you know exactly where to look. Oh, the car object broke down? The problem must be in the Car class You dont have to muck through anything else.Thats the beauty of encapsulation. Objects are self-contained, and each bit of functionality does its own thing while leaving the other bits alone. Also, this modality allows an IT team to work on multiple objects simultaneously while minimizing the weg that one person might duplicate someone elses functionality.We can help you find your next programming jobSEARCH JOBS2. Reuse of code through inheritanceSuppose that in addition to your Car object, one colleague needs a RaceCar object, and another needs a Limousine object. Everyone builds their objects separately but discover commonalities between them. In fact, each object is really just a different kind of Car. This is where the inheritance technique saves time Create one generic class (Car), and then define the subclasses (RaceCar and Limousine) that are to inherit the generic classs tr aits.Of course, Limousine and RaceCar still have their unique attributes and functions. If the RaceCar object needs a method to fireAfterBurners and the Limousine object requires a Chauffeur, each class could implement separate functions just for itself. However, because both classes inherit key aspects from the Car class, for example the drive or fillUpGas methods, your inheriting classes can simply reuse existing code instead of writing these functions all over again.What if you want to make a change to all Car objects, regardless of type? This is another advantage of the OO approach. Simply make a change to your Car class, and all car objects will simply inherit the new code.3. Flexibility through polymorphismRiffing on this example, you now need just a few drivers, or functions, like driveCar, driveRaceCar and DriveLimousine. RaceCarDrivers share some traits with LimousineDrivers, but other things, like RaceHelmets and BeverageSponsorships, are unique.This is where object-orient ed programmings sweet polymorphism comes into play. Because a single function can shape-shift to adapt to whichever class its in, you could create one function in the parent Car class called drive - not driveCar or driveRaceCar, but just drive. This one function would work with the RaceCarDriver, LimousineDriver, etc. In fact, you could even have raceCar.drive(myRaceCarDriver) or limo.drive(myChauffeur).4. Effective problem solvingA language like C has an amazing legacy in programming history, but writing software in a top-down language is like playing Jenga while wearing mittens. The more complex it gets, the greater the chance it will collapse. Meanwhile, writing a functional-style program in a language like Haskell or ML can be a chore.Object-oriented programming is often the most natural and pragmatic approach, once you get the hang of it. OOP languages allows you to break down your software into bite-sized problems that you then can solve - one object at a time.This isnt to s ay that OOP is the One True Way. However, the advantages of object-oriented programming are many. When you need to solve complex programming challenges and want to add code tools to your skill set, OOP is your friend - and has much greater longevity and utility than Pac-Man or parachute pants. Tags
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.