A programmer can follow different approaches, also known as programming paradigms, when writing code. Two of the most important programming paradigms are the procedural and the Object-Oriented ones.
Usually a programming language falls under one paradigm like Ruby , that is mainly an OOP language. But some other languages as JavaScript can be multi-paradigm and support procedural, object-oriented (prototype-based) and functional programming styles.
The main difference between them is how you organise your code:
-In a procedural approach your code is a list of instructions or steps to be carried out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
board = Array.new(9, " ") puts "Welcome to Tic Tac Toe!" display_board(board) play(board) WIN_COMBINATIONS = [ [0,1,2], # Top row [3,4,5], # Middle row [6,7,8], # Lower row [0,3,6], # Left column [1,4,7], # Middle column [2,5,8], # Rigth column [0,4,8], # Left to Rigth diagonal [2,4,6] # Rigth to Left diagonal ] # Display_board accepts a board and prints # out the current state. def display_board (board) puts " #{board[0]} | #{board[1]} | #{board[2]} " puts "-----------" puts " #{board[3]} | #{board[4]} | #{board[5]} " puts "-----------" puts " #{board[6]} | #{board[7]} | #{board[8]} " end def input_to_index(user_input) user_input.to_i - 1 end |
-In an Object-Oriented approach your code is organised using objects and classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class TicTacToe def initialize(board = nil) @board = board || Array.new(9, " ") end WIN_COMBINATIONS = [ [0,1,2], # Top row [3,4,5], # Middle row [6,7,8], # Lower row [0,3,6], # Left column [1,4,7], # Middle column [2,5,8], # Rigth column [0,4,8], # Left to Rigth diagonal [2,4,6] # Rigth to Left diagonal ] def display_board puts " #{@board[0]} | #{@board[1]} | #{@board[2]} " puts "-----------" puts " #{@board[3]} | #{@board[4]} | #{@board[5]} " puts "-----------" puts " #{@board[6]} | #{@board[7]} | #{@board[8]} " end def input_to_index(user_input) user_input.to_i - 1 end |
Reading about the different paradigms I found lots of new and very complicated concepts as: modularisation as a main trait of the procedural paradigm, and data abstraction, encapsulation, polymorphism, inheritance and serialisation-marshalling as traits for the Object-Oriented paradigm.
Apart from the desire to look the other way, I decided create some short and easy to understand definitions. Maybe the next time I find these concepts I don’t feel so uncomfortable!
- Modularisation: It is used for reducing the complexity of a system subdividing a computer program into modules or separate software components.
- Data abstraction: It is the reduction of data to a simplified representation of the whole. In OOP the programmer would just keep the relevant data inside the object hiding everything else. Data hiding is a software development technique.
- Encapsulation : It is the inclusion within an object of all the data and methods need for the object to function.
- Polymorphism: It is the ability to present the same interface (
Shape
) for differing data types (square, circle, etc). - Inheritance: It enables new objects (subclasses or derived classes) to take on the properties of existing objects (superclasses or base classes).
- Serialisation and marshalling: Both are for transforming objects into series of bits. In particular, marshalling is about getting parameters from here to there, while serialisation is about copying structured data to or from a primitive form such as a byte stream. What is byte stream? A sequence of bits. What is a bit? It is the smallest unit of data in a computer. And what am I doing right now? Yak shaving!