Programming Logic and Design
Instructions: Identify at least two (2) advantages to using OOP as compared to using only PP..
2.Create one (1) original example of a class with at least one (1) attribute and one (1) method. Identify what the class in question represents, the attributes the class stores, and the purpose of the related method. Next, examine the relationship between the class, attributes, and methods that you have identified..
3.Describe at least one (1) feature of object-oriented programming that Visual Logic lacks..
4.Identify at least one (1) advantage to using event-driven programming, as compared to using purely procedural programming..
5.Use at least three (3) quality resources in this assignment. Note: Wikipedia and similar Websites do not qualify as quality resources.
Programming Logic and Design
Advantages of using Object-oriented Programming over Procedural Programming
The use of Object-oriented programming (OOP) over procedural programming offers four main benefits:
- Modularity – This is when a piece of code is independent of other pieces of code used within a program. The modularity feature poses an advantage over procedural programming since it allows the splitting up of a program into functions, and at a much higher level in objects. Hence, modularity facilitates the use of different abstraction levels meaning that components, subsystems and packages can be utilized as building blocks for other systems.
- Code reusability – this refers to when a code can be used unchanged, to perform an individual service regardless of the application that uses the code. OOP facilitates this code reusability since complex tasks can be broken down into generic classes (modules) and separate files, which contain classes from the main script. This is an advantage over procedural programming since the code in procedural programming cannot be reused, and is as such confined to the specific program for which it was coded.
- Information-hiding – This refers to when details of the internal implementation of a module (class) remain obscured from the outside world. Since data is not directly exposed, this acts as a safety mechanism. Procedural programming does not have an information hiding feature, making it vulnerable to end-users and alterations.
- Ease of debugging – Whenever a problem occurs in OOP, it is much easier to repair because the class (module) is independent of other pieces of code. Separate classes (modules) permit easier maintenance because the code is modular by nature (Fedor & Paper, 2015). The modification of one piece of code does not have an impact on other pieces of code within the application. Furthermore, reliability is also enhanced since the modularity of OOP enhances the consistency of the code. Once a class (module) has been tested, it may be thought of as a ‘black box’ and relied upon to yield consistent results. This consistency limits the range explored during code repair since a coding issue only has to be fixed in one place.
An example of a class that we can create is the class Point. This class is based on the mathematical concept of a point on a Cartesian plane. In two dimensions, the point will comprise of two coordinates (numbers) that collectively, are treated as one single object. These points are often written in parentheses with a comma separating the new/coordinates. Thus, while (0, 0) will represent the origin, (x, y) will represent the point that is ‘x’ units to the right and ‘y’ units above the origin.
By defining a new class, one can group these values into a compound object although it involves more effort than using a tuple.
Our definition of the class will look like:
“””Point class represents and manipulates y, x cords. “””
“””Create a new point at the origin “””
self.y = 0
self.x = 0
Even though class definitions can appear anywhere within a program, they typically appear near the beginning. The indentation level informs us of where the class ends. Every class ought to have a method with the special name __init__, which is an initializer method automatically called upon whenever a new instance of Point is created.
Just like objects in the real world, object instances in programming have both methods and attributes. Attributes can be modified in an instance using dot notation, for example:
>>> p.x = 3
>>> p.y = 3
Both instances and modules create their own namespaces. The syntax for accessing the names contained in each attribute is the same.
Features of OOP lacking in Visual Logic
One feature of object-oriented programming that is absent in visual logic is inheritance. Inheritance is a method of reusing the code of existing objects. Hence, an object can inherit traits from another object (Yevick, 2005). This feature is absent in Visual Logic, as it is not possible for objects and classes to share common attributes or features.
Another feature lacking in Visual Logic is that of polymorphism. This refers to the ability to create a method or property of an object that has multiple forms. While inheritance allows one to pass the traits of a parent class to a child class, polymorphism enables one to override existing methods and properties. In this case, one can use the same method or property names, but change them to suit their needs
Advantages of Event-driven programming over Procedural Programming
programming offers several advantages compared to purely procedural programming,
such as the level of flexibility and speed. Such speed and flexibility are
observed in the difference between the two programming paradigms. Procedural
programming comprises of a list of commands written on a procedural level, meaning that the actions will be executed in
a stepwise manner. Event-driven programming, on the other hand, is based on a response to events. An
event could be anything from clicking a mouse button to pressing a particular
key on the keyboard. Hence, there is no pre-set flow or distinct start or end
of a program since there is no way to know which function will be called in at
what time (Farrell, 2015). This results in a higher adaptability of
event-driven programs since one can have an object for just about anything.
Farrell, J. (2015). Programming Logic and Design. Stanford, Connecticut: Cengage Learning.
Fedor, J., & Paper, D. (2015). Web Programming for Business: PHP Object-Oriented Programming with Oracle. New York: Routledge. Yevick, D. (2005). A First Course in Computational Physics and Object-Oriented Programming with C++. Cambridge: Cambridge University Press.