Skip to content

Instantly share code, notes, and snippets.

@lilpolymath
Last active May 6, 2020 22:05
Show Gist options
  • Select an option

  • Save lilpolymath/32b3fe8c72cb5ad668bbabc0e502cc04 to your computer and use it in GitHub Desktop.

Select an option

Save lilpolymath/32b3fe8c72cb5ad668bbabc0e502cc04 to your computer and use it in GitHub Desktop.
Functional Programming vs Object Oriented programming.

Functional Programming

Functions are small pieces of reusable and modular code that solve a specific problem or perform a specific set of actions. They are used to make code readable and eliminates redundant logic.

Functional programming is a concept in computer science that decomposes a problem into simpler problems in such a way that you can by reading a specific block of code - a pure function, know what that code block is solving.

And as such, you are only concerned with inputs and the outputs that it returns. It doesn't have a way to directly affect the output produced for a given input. A function also has no way to affect the state of its input. It also doesn't depend on a variable outside of its scope that can cause a side-effect. These are the properties of a pure function. Examples are map, reduce and filter.

An example

first = [1,2,3,4]
second = first.map(num => num * 2)
console.log(second)	//  [2, 4, 6, 8]
console.log(first)	//	[1, 2, 3, 4]

The map function is a pure function because;

  • The return value depends only on the input. If you change the content of the array you get a different result.
  • It has no side-effects. That is a network request for example can't affect its return value.
  • It doesn't affect the value of the input. As we can see array first remains the same. This is referred to as Immutability.

Object Oriented Programming.

The bedrock for OOP is what we call classes and objects. A class, functions as a template that defines the basic characteristics of a particular object. For example, let say we have class called Dog which describes how all the dogs in this world should be. These dogs have some information - data which are called attributes which can be changed by functions called methods defined in class.

An important perk of an object is that you can directly change the state or value of its attributes - state mutation.

Let's say we have three dogs, King, Mark and Luis. If I decide to change one attribute, in this case the number of legs to 5, I would have to . Attributes that apply to the whole class are called class attributes. Attributes that apply to just an instance are called instance attributes.

class Dog(object):
    species = "Canus Domestica"		# Class attribute

    def __init__(self, name):
        self.name = name			# Instance attribute
        self.numberOfLegs = 4

    def __str__(self):
	return self.name

    def changeNumberOfLegs(self, newNumber):
	self.numberOfLegs = newNumber


Dogs = [Dog("King"), Dog("Mark"), Dog("Luis")]
print(Dogs[0].numberOfLegs)         # Prints 4

for dog in Dogs:
	dog.changeNumberOfLegs(5)

print(Dogs[0].numberOfLegs)         # Prints 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment