1. Programming Paradigms
1. Programming Paradigms
LANGUAGES
Programming Paradigms
&
Language Characteristics
PROGRAMMING PARADIGM
• Paradigm: A pattern or model
• Programming Paradigm: A Pattern or model of
Programming
• A fundamental Style of Computer
Programming
TYPES OF PROGRAMMING PARADIGMS
IMPERATIVE PARADIGM
• One of the oldest programming paradigm
• Works by changing the program state step by step through
assignment statements
• Consists of a set of commands for the computer to perform
• Describes how the inputs are given and outputs are obtained
• Example: C,C++, Java, Python, Ruby etc.
Advantage Disadvantage
Simple to implement Complex problems are difficult to
implement
Contains loops, variables etc. Less efficient and less productive
PROCEDURAL PROGRAMMING PARADIGM
A code written in a compiled language has to be The program is executed directly with any
converted to a machine code for execution compilation
Faster execution speed and better performance The speed of execution is slow but more portable
code
Errors prevent the compiling of codes All debugging has to be done at run time
Examples of compiled languages are C, C++, C#, Examples of interpreted languages are JavaScript,
COBOL, etc. Perl, BASIC, Python, etc.
The compiler is used only once if no changes are The interpreter runs each time to execute the code
made to the code
RAM is not needed to execute the code RAM is needed to execute the code
TYPE SYSTEMS – STATIC AND DYNAMIC TYPE
CHECKING
• Typing:
o When is data type is checked?
Static – Types are checked before run-time
Dynamic – Types are checked on the fly, during execution
o For example "3" + 5 will raise a type error in strongly typed languages
(E.g., Python and Go)
o Above languages do not allow for "type coercion" - ability to change
types of values based on context (e.g., merging two types using +).
EXAMPLE – PYTHON (DYNAMIC, INTERPRETED)
def foo(a):
if(a>0):
print ('Hi')
else:
print ("3"+5)
foo(2)
WHAT IF COMPILED?
• Type error would be thrown even before the code is run, at compilation
time.
EXAMPLE – GO (STATIC, COMPILED)
package main
import "fmt"
func foo(a int) { /tmp/rC7FHazS50/main.go:8:21: invalid
if(a>0){ operation: "3" + 5 (mismatched types
fmt.Println("Hi") untyped string and untyped int) === Code
} else { Exited With Errors ===
fmt.Println("3"+5)
}
}
func main() {
foo(2)
}
• Types are checked before running (static) and type errors are caught
immediately
• If it was interpreted, type errors would still be checked before run-time
• If dynamic, no errors thrown, although the code is checked during compilation
PERFORMANCE
• A compiled language will have better performance at run-time if it's
statically typed as the knowledge of types allows for machine code
optimization
• Statically typed languages have better performance at runtime as they
need not check types dynamically at runtime
• Similarly compiled languages are faster at runtime as the code is already
translated instead of interpretation/translation at runtime
• Both compiled and translated languages will have a delay before running
for translation and type-checking respectively.
TYPECHECKING - SUMMARY
• Static typing catches errors early (useful for long programs) instead of at
execution time
• More strict
• Doesn’t allow type errors anywhere in your program
• Prevents variables from changing types
• Defends against unintended errors
• e.g., num=2
• num='3' //error
• Dynamic typing is more flexible but allows variables to change types,
sometimes creating unexpected errors.
INFORMATION HIDING
• Hiding internal object details, i.e., data members
• Restrict the data access to class members
• Maintains data integrity
Data Hiding vs Encapsulation
• S.No Data Hiding Encapsulation
1 Hides class data components only Hides the private methods and class data parts
2 Restricts the data use to assure data Wraps up the complex data to present a simpler
security view to the user
Access Specifiers:
• Private members/methods: Functions and variables declared as private can be accessed only
within the same class, and they cannot be accessed outside the class they are declared.
• Public members/methods: Functions and variables declared under public can be accessed from
anywhere.
• Protected members/methods: Functions and variables declared as protected cannot be accessed
outside the class except a child class. This specifier is generally used in inheritance.
GROUPING DATA AND OPERATIONS - ENCAPSULATION
• The user of data type needs to know what a data type can do and does not need to know
how that data type is implemented
• E.g., Primitive types such as int, float, char data types are used only with the knowledge
of the operation that can be performed on these types, without any idea of how they are
implemented
• Think of ADT as a black box which hides the inner structure and design of the data type.
EXAMPLE 1 – LIST ADT
• The data is generally stored in key sequence in a list which has a head structure
consisting of count, pointers and address of compare function needed to compare the
data in the list.
• The data node contains the pointer to a data structure and a self-referential pointer which
points to the next node in the list.
• Encapsulation: Hide the internal details of the data and provide a public interface for
users to interact with the data. Hence allows easier maintenance and modification of
the data structure
• Data Abstraction: Provides a level of abstraction from the implementation details of
the data. Users only need to know the operations that can be performed on the data,
not how those operations are implemented.
• Data Structure Independence: ADTs can be implemented using different data
structures, such as arrays or linked lists, without affecting the functionality.
• Information Hiding: ADTs can protect the integrity of the data by allowing access only
to authorized users and operations thereby preventing errors and misuse of data.
• Modularity: ADTs can be combined with other ADTs to form larger, more complex data
structures, which allows for greater flexibility and modularity in programming
ADVANTAGES AND DISADVANTAGES OF ADT
S.No Advantages Disadvantages
1 Encapsulation: Data and operations Overhead: Implementing ADTs can add
encapsulated into a single unit, making it easier overhead in terms of memory and processing,
to manage and modify the data structure. which can affect performance.
2 Abstraction: Allow users to work with data Complexity: ADTs can be complex to
structures without having to know the implement, especially for large and complex data
implementation details, which can simplify structures
programming and reduce errors.
4 Information Hiding: Can protect the integrity of Limited Flexibility: Some ADTs may be limited
data by controlling access and preventing in their functionality or may not be suitable for all
unauthorized modifications. types of data structures.
5 Modularity: Can be combined with other ADTs Cost: Implementing ADTs may require additional
to form more complex data structures, which resources and investment, which can increase
can increase flexibility and modularity in the cost of development.
programming.