Software development has really come a long way over the past few decades.
Programming used to always be about doing things in a very procedural way, this means that each line of code is executed in the order that it is written (or placed) in a source code document – we refer to a file where programming code is stored as “source code”.
This is still very much a core basis when writing code, except as software applications have gotten infinitely more expansive and complex, there has been more of a need to reuse sections of code and make it more “modular”.
This is where Object-Oriented Programming (OOP) comes in. OOP provides the programmer with a way to create a blueprint of what is seen to represent an entity and be able to use it independently of other code; in essence, creating a “module” of a section of code to be able to use wherever needed in the overarching software eco-system.
A great definition comparing Object-Oriented Programming to Procedural Programming is that “programs are made up of modules, which are parts of a program that can be coded and tested separately and then assembled to form a complete program. In procedural languages (i.e. C) these modules are procedures, where a procedure is a sequence of statements”.
For example, if you had a program where you had to create both a dog and a cat; they would clearly share various features, characteristics and traits which could fundamentally be inherited from a base “animal” class – in OOP an “object” is an instance of an “class”.
To illustrate the above scenario, we will first show the procedural method followed by the OOP alternative to make the code more modular and shared.
Procedural implementation:
// create a function for when dog_say is called
function dog_say() {
print "woof"
}
// create a function for when cat_say is called
function cat_say() {
print "meow"
}
// make the two calls defined above
dog_say()
cat_say()
OOP implementation:
// create an animal object/class
var animal = new Object(type):{
this.type = type
this.say = function() {
switch (this.type) {
case "dog": print "woof"
case "cat": print "meow"
}
}
}
// create instances of the animal class for ?cat? and ?dog?
var dog = new animal("dog")
var cat = new animal("cat")
// make the two calls from the object/class above
dog.say()
cat.say()
As you will notice, in cases that do not contain a lot of complexity or many lines of code, using OOP is often overkill and not required. However, it comes in extremely handy when you need to extend many tens or hundreds of objects, patterns or security implementations across a larger scale application.
With the surge of OOP based computer programming languages such as C++, C#, Java, Ruby and the likes, it has become almost impossible to not take OOP seriously in the industry as it is so heavily implemented and required in most software architectures over the past decade in particular.
I think OOP better reflects the human way of thinking and conceptualising to the point of where we as humans categorise objects, entities and more.
At the same time it is also often not desirable to cordon everything we perceive into inheritance and
In OOP there are a few key fundamental definitions that are important to know about:
**Encapsulation:
** Encapsulation is the “ability of an object to hide its data and methods from the rest of the world”. Using private variables or methods a class (or an object instance) can only be accessible from within itself or a controlled nested group of reference objects.
**Polymorphism:
** Polymorphism is derived from a Greek word which means “many, much, form, shape”. With the ability to “appear in many forms”.
Polymorphism refers to the “ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes”.
**Classes and Objects:
** A class is a collection of objects that have common properties, operations and behaviours. “A class is a combination of state (data) and behaviour (methods)”.
An object is an instance of a class. Objects are units of abstraction. An object can communicate with other objects using messages. An object passes a message to another object, which results in the invocation of a method. Objects then perform the actions that are required to get a response from the system.
Classes can be seen as data types, which are prototypes from which objects can be created.
**Inheritance:
** Inheritance means that when a class is defined, any of it’s subclasses can inherit properties and methods from it.
**Abstraction:
** Abstraction is a technique for arranging the complexity of computer systems. It works by establishing a level of complexity on which a person interacts with the system, suppressing the more complex details below the current level.
Object-Oriented Programming has brought about a much more flexible way of creating modular code by comparison to the more traditional Functional and Procedural development patterns. However, it should not be used all the time as it is often overkill for smaller projects or in specific architectural decisions.
Often developers who are tasked with creating small scripts or command-line scripts for automation of tasks should think twice before endeavouring into a world of potentially harmful “over thought programming”.
When creating software, it is very important to remember the acronym “KISS”, noted by the U.S. Navy in 1960. It stands for “Keep it simple, stupid” and should be a principle that every software developer employs when attempting to resolve a technical problem.
References:
Object Oriented Programming (Unknown) – Available from: http://www.ctp.bilkent.edu.tr/~russell/java/LectureNotes/1_OOConcepts.htm
Difference between Structured Programming and Object Oriented Programming | Structured Programming vs. Object Oriented Programming (2014) – Available from: http://freefeast.info/difference-between/difference-between-structured-programming-and-object-oriented-programming-structured-programming-vs-object-oriented-programming/
Polymorphism (computer science) (2017) – Available from: https://en.wikipedia.org/wiki/Polymorphism_(computer_science)
Inheritance (2004) – Available from: http://whatis.techtarget.com/definition/inheritance
Abstraction (software engineering) (2017) – Available from: https://en.wikipedia.org/wiki/Abstraction_(software_engineering)