Loading...

Statically vs Dynamically Typed Programming Languages

Statically vs Dynamically Typed Programming Languages

What is a type of programming

Type or data type in programming defines the classification of data that can be stored in a variable and accordingly the kind of operations that could be applied to it. There are various types of data available and it is important to classify them so they can be used properly.

For example, Your name is a word and your age is a number, although both are data values, they function in very different manners. With each passing year, you can add one to your age but that is not the case with your name. Likewise to get your full name you can just join your first and last name, but that can’t be done with your age.

That’s why the classification of data is important.  you can learn more about data types here.

Type checking in programming languages

The process of evaluating and safeguarding the data types in a program. According to the language, there can be two types of type-checking performed by the compiler, Runtime, and compile-time type checking.

In compile time type checking, the compiler determines the data type of each variable, object, list, etc, and makes sure everything is type-safe while compiling and before execution of the code

And in runtime type checking, the compiler first compiles the code and while running the program it determines the data type while it is being used. 

The difference in these executions gives rise to the two types of programming languages. Statically typed and Dynamically typed.

Statically typed languages

Statically typed languages follow type checking while the code compiles, i.e. compile-time type checking. 

The rules for defining the variables and assigning a data type to them are very strict and rigid. 

Once a data type is assigned to a variable then it needs to be the same throughout the program and can never be changed.

Some statically typed languages like Java, C++, etc.

Pros

  • Since the type check is done at compile time, the execution of code is much more smother
  • The chances of error are reduced as the data type is fixed and constant
  • Static languages are faster
  • The IDE knows more about the code and provides better and faster assistance
  • Static languages are much more expressive and predictable

Cons

  • Because of its strict rules difficult to pick up as a beginner
  • You need an extra initial effort while defining variables as you need to know the data type before actually using it
  • The development process is slower
  • If while development you realize that the data type you have used for a variable needs to be changed then you have to make changes to the whole code, which can be both difficult and time taking

Dynamically typed languages

Dynamic languages have much more flexible rules for declaring variables and assigning data types to a variable. You can even change the data types dynamically between the code as variable data types in them are checked while running the code i.e. during runtime.

Some dynamically typed languages are  python, javascript, etc

Pros

  • Due to their flexible rule, these languages are much easier and beginner friendly to learn
  • Quicker development process
  • Less compile time so you can test your code faster
  • Much more tolerant to code changes and refactoring. Changes that need to be made in between of development process can be done easier and faster
  • Can be used in scenarios where the data has no constant data type and results in having a much more flexible input potential
  • Don’t need prior planning for defining variables

Cons

  • The chances of error are very high
  • As the data type is not fixed the code is less expressive and gives unpredictable outputs like in javascript                                                                 
  • While programming you need to keep track of the dynamically changing data types of different variables 
  • Runtime is slower
  • Difficult to debug
  • Increases the complexity

Statically typed and dynamically typed programming are just two different ways to write code. Both of them have their fair share of positives and negatives. And saying one is better than the other without context would be wrong. 

Which type of programming language to choose just depends on your requirements.

If you want to develop any project quickly or you are working on simpler projects then dynamically typed languages would be a good option, but if you are creating a project that needs a more rigid and structured code base then statically typed languages are the way to go.

There are also languages like Rascal being developed that take a hybrid approach to the type checking of data, trying to get the best of both worlds.

Sharing is caring

Did you like what Shivam Pande wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far