Skip to content

Latest commit

 

History

History
868 lines (523 loc) · 11.9 KB

File metadata and controls

868 lines (523 loc) · 11.9 KB

import { CodeSurfer, CodeSurferColumns, Step, } from "code-surfer"; import { comic } from '@mdx-deck/themes' import { nightOwl, github } from "@code-surfer/themes";

export const theme = github;

Programming Paradigm


What is a paradigm?


Object-Oriented Programming

Functional Programming

etc.


But, Why?


<img style={{ height: '80%' }} src="https://firebasestorage.googleapis.com/v0/b/programming-paradigm.appspot.com/o/why-programming-paradigm.png?alt=media&token=0eb5ebf8-72af-4bc7-8e3f-39078e9fa9e2" alt="why-programming-paradigm" />


Good Code


Performance


Maintainable


What are some major paradigms?


Imperative Programming



Imperative Programming

  • follow my commands
  • in the order I give them
  • remember state

Wait, what is state?


Current status of the program

Dynamically change during program execution



clock


Declarative Programming


Declarative Programming

  • these are the facts
  • this is what I want
  • I do not care how you do it

const Button = () => (
  <button
    style={{
      fontSize: 16,
      color: "black"
    }}
  >
    Click Me!
  </button>
);

SELECT isbn,
 title,
 price,
 price * 0.06 AS sales_tax
FROM Book
WHERE price > 100.00
ORDER BY title;

blueprint


Object-Oriented Programming


Main Concepts:

Class


Main Concepts:

Class


Main Concepts:

  • Encapsulation
  • Inheritance
  • Polymorphism

Encapsulation


Information Hiding


Hide the inner workings

Provide an interface


Encapsulation

in OOP refers to binding data and the methods

to manipulate that data together

in a single unit, that is, class/object.






Advantages of Encapsulation

  • Make the code easy to change and maintain.
  • Properties to be hidden can be specified easily.

More Examples

  • MVC
  • Components or Custom Hooks

Inheritance




Advantages of Inheritance

  • Reusability
  • Code modification
  • Extensibility

<CodeSurferColumns themes={[github, nightOwl]}>


Polymorphism


In programming,

polymorphism refers to the same object

exhibiting different forms and behaviors.


<CodeSurferColumns themes={[github, nightOwl]}>


Having specialized implementations

of the same methods for each class.



Messaging


The big idea is "messaging".

Alan Kay


Object Oriented Programming

  • keep your state to yourself
  • receive my messages
  • respond as you see fit

S.O.L.I.D Principles




Robert C. Martin

robert c. martin robert c. martin

  • S — Single Responsibility (單一職責原則)
  • O — Open-Closed (開閉原則)
  • L — Liskov Substitution (里氏替換原則)
  • I — Interface Segregation
  • D — Dependency Inversion

Single Responsibility

Open-Closed

Liskov Substitution

  • Single Responsibility (Separate behaviours)

  • Open-Closed (Extend behaviour without changing behaviour)

  • Liskov Substitution (Enforce consistency)


Quiz Time!


<CodeSurferColumns themes={[github, nightOwl]}>


<CodeSurferColumns themes={[github, nightOwl]}>


Functional Programming


Main Concepts:

  • Functions

Main Concepts:

  • Functions

Main Concepts:

  • Pure functions
  • Immutability
  • Declarative programming

Pure Functions


It depends only on the input provided

It doesn’t inflict changes beyond their scope


No Side Effects




- Changing a variable, property, or data structure globally

- Changing the original value of a function’s argument

- Send network request

- Throwing an exception, unless it’s caught within the same function

- Printing to the screen or logging


<CodeSurferColumns themes={[github, nightOwl]}>


Immutability



Avoiding unintended consequences

Easier maintenance and debugging




Declarative



Easier to reason about

Less prone to errors


Ramda

Lodash


Main Concepts:

  • Pure functions
  • Immutability
  • Declarative programming

Functional Programming

  • mutable state is dangerous
  • pure functions are safe
  • data goes in data comes out

What do they have

in common?


Shared mutable state



Object Oriented Programming

shared mutable state


Functional Programming

shared mutable state


Quiz Time Again!


<CodeSurferColumns themes={[github, nightOwl]}>



Which paradigm is

the best


What can a paradigm

teach me?


Imperative Programming

be explicit understand implementation


Declarative Programming

be abstract understand domain


Object Oriented Programming

encapsulate & communicate


Functional Programming

specialize transform data


no paradigm is best absolutely

each is best for a certain case


What's the Point

OOP vs. FP


What's the Point

OOP vs. FP


What's the Point

  • don’t fight your paradigm, embrace it
  • paradigm is your tool, not your goal

References: