Introduction To Programming
In programming, there are three primary paradigms: procedural, functional, and object-oriented. Each paradigm has its approach, uses, and characteristics, which are explained below:
Procedural Programming:
It is a programming paradigm that involves breaking down a computer program into procedures that call each other, resulting in a series of steps that form a hierarchy of calls.Procedural programming languages, such as FORTRAN, ALGOL, COBOL, BASIC, Pascal, and C, are examples of procedural programming languages
Functional Programming
Functional programming is a declarative programming paradigm style where one applies pure functions in sequence to solve complex problems.Functional programming languages, such as Common Lisp, Scheme, Clojure, OCaml, Haskell, and F#.
Key features:
1.Functions are treated as first-class citizens, meaning they can be passed as arguments to other functions, returned as values from functions, and stored in data structures.
2.Code reusability is high
Object-Oriented Programming (OOPs)
Object-oriented programming System(OOPs) is a programming paradigm [model / pattern] based on the concept of “objects” and “classes” that contain data and methods. The primary purpose of OOP is to increase the flexibility and maintainability of programs. It is used to structure a software program into simple, reusable pieces of code blueprints (called classes) which are used to create individual instances of objects.
Key Feature of OOPs: Inheritance, Encapsulation, Abstraction and Polymorphism
Static vs dynamic languages
Static languages
In statically typed languages, once a variable is created, its type cannot be changed. Once a variable is assigned a type, it remains static and cannot be altered. This means that if we define a variable as an integer, for example, we can only change its value to another integer, and it cannot be reassigned to a different data type.
int oldVariable = 2406; // oldVariable is assigned an integer data type
oldVariable = 108; ✅ // We can change the value of the same variable with the same data type
String oldVariable = "Hello World!" ; ❌ // Can't change data type or reuse variable with the same name for different data type
This characteristic enhances code robustness by ensuring type-checking is performed during compilation. However, the development cycle in statically typed languages may be slower due to the additional step of compilation. Nonetheless, because the data type of a variable is known during compilation, there is no need for extra checks when accessing the variable.Errors in Static code occur during Compilation.
C++, Java, Go, and C# are examples of statically typed languages.
Dynamic languages
In dynamically typed languages, both the types and values of variables can be changed dynamically. This means that a variable, initially assigned an integer, can later be assigned a string. Type checking occurs at Runtime in dynamic languages.
let oldVariable = 2406; // oldVariable is assigned an integer data type
oldVariable = 108; // We can change the value of the same variable with the same data type
oldVariable = "Hello World!"; // We can change the value and data type of the same variable dynamically
Dynamic languages are typically interpreted rather than compiled. While dynamic typing allows for flexibility, it also increases the likelihood of type errors. However, the absence of a compilation step speeds up development.
Errors in Dynamic code occur during Runtime.
JavaScript, Ruby, and PHP are examples of dynamically typed languages.
Interpretation Vs Compilation
Interpretation:
- An interpreter reads the program line by line and executes each line immediately.
- If the interpreter encounters an error in any line, it stops and reports the error.
- Interpreted languages are executed directly by the interpreter, without an intermediate step of generating machine code.
- Examples of interpreted languages include Python, Ruby, JavaScript, and PHP.
Compilation:
- A compiler reads the entire program at once and converts it into machine code that can be directly executed by the computer’s processor.
- The compiler checks the entire program for syntax and semantic errors before generating the machine code.
- Compiled languages require a separate compilation step before the program can be executed.
- Examples of compiled languages include C, C++, Java, and Rust.