OO Programming vs Functional Programming

In all programs, there are two primary components: the data (the stuff a program knows) and the behaviors (the stuff a program can do to/with that data).

Object-Oriented Programming: programming paradigm that brings together data and its associated behaviors (functions) in a single location called an “object”.

  • data structure is usually an array of objects.  (Data is converted into objects and methods are called on them).

Example:

class Employee
  def initialize(name, salary)
    @name = name
    @salary = salary
  end

  def change_salary(amt)
    @salary = @salary + amt
  end

  def description
    "#{@name} makes #{@salary}"
  end
end
employees = [
  Employee.new("Bob", 100000.0),
  Employee.new("Jane", 125000.0)
]

employees.each do |emp|
  emp.change_salary(10000.0)
end

employees.each do |emp|
  puts emp.description
end

Functional Programming: programming paradigm that says data and behavior are distinctively different things and should be kept separate for clarity.

  • data structure is an array of arrays and/or hashes to not *complicate* data by mixing it with behavior.
  • FP leans very heavily on tiny methods that do one small part of a larger job, delegating the details to other tiny methods. This combining of small methods into a larger task is call “composition”.
  • This idea of not changing the contents (or “state”) of a variable once it’s been created is called immutability and is another key aspect of FP.

Example:

employees = [
  [ "Bob",  100000.0 ],
  [ "Jane", 125000.0 ]
]
happier_employees = change_salaries(employees, 10000.0)
happier_employees.each do |emp|
  puts "#{emp[0]} makes #{emp[1]}"
end
Advertisements
OO Programming vs Functional Programming

JavaScript Objects

Object.keys(obj) returns an array of keys in a hash object.

Examples:

var arr = ['a', 'b', 'c'];
console.log(Object.keys(arr)); // console: ['0', '1', '2']

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']

// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.keys(an_obj)); // console: ['2', '7', '100']

Object.values(obj) returns an array of values in a hash object.

Examples:

var obj = { foo: 'bar', baz: 42 };
console.log(Object.values(obj)); // ['bar', 42]

// array like object
var obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.values(obj)); // ['a', 'b', 'c']

// array like object with random key ordering
var an_obj = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.values(an_obj)); // ['b', 'c', 'a']

obj.hasOwnProperty(prop) returns true/false indicating if an object has the specified property (key, NOT value).

Examples:

var object = {cat: 'meow'};
object.hasOwnProperty('cat'); // true
object.hasOwnProperty('meow'); // false

To access a property’s value:  object[key]

Example:  object['cat']; // 'meow'

JavaScript Objects

FCC: Testing Objects for Properties

Instructions:

Modify the function checkObj to test myObj for checkProp. If the property is found, return that property’s value. If not, return "Not Found".

Solution: 

screen-shot-2017-01-11-at-10-27-40-pm

Notes:

  • .hasOwnProperty(propname)method of objects to determine if that object has the given property name.
  • .hasOwnProperty()returnstrueorfalseif the property is found or not.
  • use ‘===’ to check for true or false.
  • To return value of a property in a lookup table:  lookupTable[property];
FCC: Testing Objects for Properties

self in Ruby

self is a special variable that points to the object that “owns” the currently executing code.

self is the “current object” and the default receiver of messages (method calls) for which no explicit receiver is specified. (Which object plays the role of self depends on which context).

Inside an instance method:

class Ghost
  def reflect
    self
  end
end

g = Ghost.new
g.reflect == g # => true

Inside a class or module method:

class Ghost
  def self.reflect
    self
  end
end

Ghost.reflect == Ghost # => true

Inside a class or module definition:

class Ghost
  self == Ghost # => true
end 

module Mummy
  self == Mummy # => true
end 

Source: [www]

The Golden Three Rules of Self

  1. Use self when setting/getting instance attributes inside a class definition.
  2. Use self to denote a method within the class definition as a class method.
  3. Use self to reference the calling object within an instance method definition.
self in Ruby