What Is Typing?

Typing, in a programming language, is the act of specifying what kind of data a variable can hold. In other words, what “data type.”

Here are some common data types in most programming languages.

Data Type

Description

Examples

Integer

A whole number

1, 5, 20

Floating Point

A decimal number

0.04, 23.1, 5.0

Character

A single symbol

‘a,’ ‘4,’ ‘B’

String

A sequence of characters, i.e. text

“Hello world”

Boolean

A true or false value

true, false

Array

A list of any data type

[1, 2, 4], [true, false]

In a programming language, we also have functions. Functions allow us to specify some input and output. A function definition for a function that will multiply two integers and return an integer will look like so.

This specifies a function named “multiply” that takes in an integer x and an integer y, and should return (or output) an integer. The “int” before “multiply” tells the language what it should output.

As you can see from the implementation of the function, it uses the “return” statement to return the value of x * y which will also be an integer.

So, if we call this function with x = 3 and y = 4, what should we expect?

We get 12. Great! That is what we expected.

Why Typing Matters

In the programming language Java (as shown in the examples above) typing is required. This means that when you call “multiply(),” it will expect the input of two integers.

Let’s try to break it! What if we give it a string and an integer?

What do we get? An error! This is important. It tells us that we made a mistake and does not let us run this program because it doesn’t make sense to multiply text and a number.

Typing is really helpful, so all languages must use it right? Let’s look at some python code that does the same thing.

What do you notice about this function definition? First, we don’t specify what type x and y are. They could be integers, strings, or whatever. As well, you may think that we are returning a new data type “def.” This isn’t the case though. The “def” keyword is used to create a new function. That means we aren’t specifying any type to return! Again, that means we could return whatever.

Let’s act as a good programmer and run this as intended.

Hey! We get the right answer! That means this code is perfect and there must me no errors, right? Well lets try to break this one as well.

Given x = “Hello” and y = 4, let's see what happens.

What!? It turns out that Python decided that a string times an integer just adds the same text that many times.

So we can see, this is definitely unintended. Imagine a user is putting info into a form. They put their hourly wage as $15 and their hours worked as Forty. The form will multiply these values to give us our result.

Using Java, we may see this error.

But using Python, we would see no error!

This is what we would consider a bug, when the program is working, but not working how we expected. Imagine if it were the other way around! “Fifteen” 40 times would fill up your screen!

Benefits and Drawbacks

So, as you can see, typing does have an effect on code and how it is used. Python does have a system to label data types and outputs of functions, but it is not enforced. Some languages, like JavaScript, don’t even have the option to type variables.

Loose typing, like Python, is great for functions with multiple inputs. If you wanted to multiply an integer and a floating point, you would need a new function in Java, but you can use the one we already built in Python. That also leads to sloppy code that can have wild side effects, as we’ve seen.

Having an enforced rule about what can go in and out of a function may be more cumbersome to write and may require multiple different functions, but in the end, it leads to more maintainable and fixable code. While one function was easy enough to spot the error in, real life projects have hundreds, thousands, or even millions of functions, each with a long list of inputs.

Keeping code clean and maintainable is a key part of the software engineering cycle. It helps everyone working on the code, and those that will work on it in the future, be able to add new features, fix issues, and trust the code that is currently written.

Resources

For more information about typing, here is a short article called “Typing in the programming language” by Nikhith Sunil.

https://medium.com/@nikhithsunil/typing-in-the-programming-language-ee6f6ebf9cbb


As well, typing is just one way to make code maintainable. See these articles of testing and input validation for a more in-depth view.

“Unit Testing,” Geeks4Geeks https://www.geeksforgeeks.org/software-testing/unit-testing-software-testing/

“Input Validation: Definition, Purpose, and Examples,” Mimo, https://mimo.org/glossary/programming-concepts/input-validaiton