DEV Community

Cover image for What's your type? A short guide to data typing.
Lost Semicolon ๐Ÿ’ป๐Ÿ–ฑ
Lost Semicolon ๐Ÿ’ป๐Ÿ–ฑ

Posted on

What's your type? A short guide to data typing.


Typing - way in which computers determines different data formats.

Static Typing - Type defined at compile time
Dynamic Typing - Type defined at run time
Weakly Typed - Variables can change type at run time
Strongly Typed - Variables cannot change type at run time

Read on....

When I first stated learning development, I used to think of "typing" as "typing on a keyboard" rather than "data typing"
which is why everything was a little bit more confusing at the start. You live and learn tho.

What is typing?

Data typing is a way of describing the type of data we are using. For example, we know that a boolean can only be True or False, or 0 and 1.
This process allows the compilers to determine the required memory to hold data and ensure that the program can be run with no type errors.

Types, types, types

Static and Dynamic typing

If we define the type of data when we declare a new variable such as :

boolean isValid = true;
int number; 
Enter fullscreen mode Exit fullscreen mode

then this is known as Static Typing where type is explicitly typed before the compiler. Languages such Java or C# as do this.
Attempts to re-assign the variable type will result in an error:

boolean isValid = "naw, mate"; //compilation error
Enter fullscreen mode Exit fullscreen mode

The type checking in this case, is done at Compile Time ( although many IDE's will let you know ahead of time, they are good that way).

If we do not need to specify the type which we want our data to be, it is determined at run time. The language then has Dynamic Typing and examples include JavaScript or Python.

isValid = true;
Enter fullscreen mode Exit fullscreen mode

Strongly and Weakly typed languages

Some languages allow you to re-assign the type a variable can hold at runtime - for example Javascript. This a quality of weakly typed languages.

isValid = true;
isValid = "I am";
Enter fullscreen mode Exit fullscreen mode

Where as in others, this is not the case:

boolean hello = true; 
hello = "hello";
//error: incompatible types: String cannot be converted to boolean
Enter fullscreen mode Exit fullscreen mode

Some flexible languages

Sometimes, languages are super flexible, like Go. Whilst being usually considered a strongly typed static language, it allows the compiler to determine the type, but once its set, you cannot change it:

isValid := true
isValid = 1

//cannot use 1 (type untyped int) as type bool in assignment
Enter fullscreen mode Exit fullscreen mode

Is static faster than dynamic?

In theory, yes, because the compiler doesn't need to work out what data type it needs.

In practice, this is not the only way to measure performance. In Java vs Go comparison, both languages are statically and strongly typed and yet the performance varies due to a multitude of other factors.
In addition, different languages have different use cases, and have been developed with different purposes in mind
(whether they have kept to it 10yrs down the line, thats a different issue) and so sometimes its like comparing Apples and Oranges.

Should I change the type during the running of my code?

As always, it depends.

But in majority of the cases, I would argue that this is a code smell and show be avoided where possible. Typescript was created in the big part, to help JS devs eliminate type bugs!

In certain cases it does make sense.

Happy coding!

Top comments (1)

stefannibrasil profile image
Stefanni Brasil

Today was the first day of #RubyConf2020 and Matz, Ruby's creator, was sharing that Ruby 3.0 is gonna have more Static Type checking support! Typing won't be required, but more features will be added to allow people that want to introduce type checking in Ruby can use.

I understand the cases where using a Strongly Typing tool is adequate but I hate having to define types all the time :D