First of all, I’ll talk about what I mean by types in coding. Any valid variable you have in your code has a type, some of the most common ones being String, Integer or Number, and Object. Each coding language has different ways of treating types; for example, some allow you to add two things of different types, but only certain types can be combined. What I am going to talk about is how, in some languages, you have to explicitly declare the type of a variable, while others have variable with implicit types.
ruby_variable = "I'm a Ruby variable!"
And if you want to reassign that variable to something with a different type, it’s very easy to do:
=> Stringruby_variable = 6
From this you can see that the type of a variable is implicitly decided by what the variable is assigned to. As for my understanding of why Ruby works this way, Ruby was designed to be friendly to coders using it. In an effort to make things easier, it allows you to declare things without requiring you to go to too much trouble, and if you want to reassign a variable, you don’t need to worry about what type the variable was before. There is a lot of freedom in Ruby to use variables however you want.
// doesn't work without using one of the keywords when first
// declaring the variable
jsVariable = "I don't work" // invalid// uses 'let' so it works
let jsVariable = "I work!" // valid
=> stringjsVariable = 28
javaVariable = "I won't work" // causes an error
You need add the type for the before the variable name. That’s because, in Java, you have to declare a variable with a type when you first define it and you can only assign it to something with that type:
// because javaVariable has the type of 'int',
// you can't assign it to a string
int javaVariable = "I'm not an integer"; // invalid// declared with the type 'String', so it works
String javaVariable = "I'm a Java variable!"; // valid
Since each variable in Java has a type associated with it, you can’t ever reassign it to something with a different type:
javaVariable = 52; // causes an error
Once you’ve declared a variable in Java, anything you reassign it to must have the same type as what you declared it with:
javaVariable = "52"; // this is valid
Java is very rigid when it comes to types of things. This is probably because Java has to be compiled before it is run, and it will not compile if it finds for any sort of errors. Assigning types to variables can ensure there are less errors in your code.
And that brings me to my conclusion on the benefits of declaring variable types in code. In my opinion, assigning types can, as I said, make errors less likely or easier to catch. When you have to declare a type, it makes you think more about how your variable is going to be used, which can decrease your chances of using it incorrectly. Declaring variable types can also make it easier to decipher your code, either when you look back at it after a long time, or when someone else looks at it, because variables can have a clearer purpose when they have a clear type.
In conclusion, both typed and untyped coding languages have their own advantages and detriments. Different people have a preference for one or the other depending on who they are and what they like about code. And ultimately, I think it is important to know about each way of writing code because most languages use one or the other, and knowing about this will make it easier when learning a new language.