Introduction to Typed Languages — Static vs. Dynamic typing
Overview
What is a Typed Language?
In a typed language, we define the type of all variables that are known at either compile time or run time. Typed languages use a type system to check where the actions are applied to the correct types.
Type Checking
Type checking is the process of verifying and enforcing constraints of types in values. Type checking in compiler design can detect type errors which means the errors that occur when a variable is assigned an irrelevant type.
The compiler must check whether the source code is following the type rules of the language or not, and report the errors in case of a violation.
Strongly typed vs. weakly typed languages
A strongly typed language has stricter typing rules at compile time. So exceptions are more likely to happen during compilation. Most of the errors happen because of wrong variable assignment, inaccurate function calling, and incorrect function-return values.
On the other hand, a weakly typed language has looser typing rules; It may produce bugs in code and perform implicit type conversion at runtime, but they are usually less in code and the developer has more control over the code.
What is static typing?
Static typing is a programming language characteristic in which types of variables are defined at compile time.
In static typing, the variables are known at compile time where they perform type checking.
Statically typed languages
Statically typed languages require you to define the data type of a variable before you use them.
//Java
String name = 'Harry';
int age = 25;
double price = 92.34;
boolean is_student = true;
Dynamic Typing
Dynamic typing is a programming language characteristic in which its type-checking is performed at run time.
Dynamically Typed Languages
These languages do not require any pre-defined data type for variables. So, its system is able to detect the type. Dynamically typed languages usually produce less code and runtime errors are quite possible.
#Python
name = 'Harry'
age = 25
is_student = True
price = 92.34
ex: Python, Ruby, JavaScript, PHP
Duck Typing :
Duck typing is also often used term when talking about dynamic typing. It is a type system used in dynamic languages where the type/class of an object is less important than its method. Here, we do not check types.
So in duck typing, the type or class of an object is determined by its behavior rather than the type declaration; which means we focus on what the object does rather than what it is.
Here is an example to understand what is duck typing :
class Circle:
def area(self):
radius = 12
return 3.14 * radius * radius
class Rectangle:
def area(self):
length = 8
width = 14
return length * width
class Triangle:
def area(self):
base =7
height = 8
return 0.5 * base * height
def calculate_area(shape):
return shape.area()
# Creating instances of different shape classes
circle = Circle()
rectangle = Rectangle()
triangle = Triangle()
# Using duck typing to calculate the area of different shapes
print("Circle area:", calculate_area(circle)) # Outputs: "Circle area: 452.1599"
print("Rectangle area:", calculate_area(rectangle)) # Outputs: "Rectangle area: 112"
print("Triangle area:", calculate_area(triangle)) # Outputs: "Triangle area: 28"
Here, each of our objects Circle, Rectangle, and Triangle has a method which is named ‘area’. The calculate_area function takes the object as an argument and calls its method. As every object has the ‘area’ method, they all are treated as instances of the ‘shape’ type.
Pros and Cons
Differences
Conclusion
The ideology of “What is the best - static or dynamic typing?” should be determined by the type of your project and its requirements. As programming languages continue to develop by leaps and bounds, the debate about ‘choosing what is the best?’ is getting more obscure.
In conclusion, one should determine ‘which typing approach is better ’ for them by considering whether the characteristics of the programming language match the requirements, objectives, and maintenance process of their project.