Strong typed vs weak typed languages
This semester at MATC, I'm taking what is basically an introduction to web development. I've dabbled with HTML and CSS before, and now I'm learning Javascript to go along with it. Now, the only experience I've had with development so far with with C# and Java, which are both strongly typed languages (also known as static typing). When I began dipping my toes into Javascript, I recoiled in… well, I reckon disgust!
In Java, let's say you want to keep track of how many apples you have. First, you'll want to make a variable to store the apple count. Since you just want to keep track of whole apples, you'll make it an integer. We'll be doing math with it, so no Strings here. We know exactly what we're working with here - a numerical count of whole apples.
In Javascript, I figured it would be pretty much the same right? And then I saw "var". Whoa, hold the phone, where's the int? Is it a number, a String, or a character? What's going on here?! I read into Javascript a bit more, and learned more about what "typing" is. Getting your hands dirty with code teaches you a lot more than just writing up a little report on what strongly typed languages are, right?
Java is a strongly typed language, whereas Javascript is a weakly typed language. For a refresher, typing is where each type of data defined by the language you're working. If you're wanting to work with numbers in a strongly typed language, you'll need to specify that your piece of data is a number. Restricting even further, you might have to specify what kind of number it is - is a whole number? Does it have decimals? Is it used for money? If you're wanting to work with letters, you might have to say the piece of data is a string of letters, or maybe just one single character.
Now that you have a type in mind for your data, you'll need to make sure you use proper actions on it. You can't just say "x = potatoe + 3". The compiler looks up at you and tells you to get out, and take your crappy public university degree with you. It doesn't care that you're drowning in student loans, or that your dog requires a grain-free diet. Everything is tightly controlled, which prevents your data from getting manipulated in ways that you weren't expecting.
With a weakly typed language, such as Javascript, you gather your potatoes, your numbers, and your booleans. You hold them high, and say "X = POTATOE + 3 AND TRUE!". The compiler doesn't really know what you're doing, but you sound like you know what you're doing, so he says "You're hired!". The problem is though, do you really know what you're doing? The compiler will take whatever you give it, and figure something out in the end. But you really, really need to know what you're doing.
I've read that it comes down to control versus *fairy dust* creativity (I'm trying not to be biased, I swear). With strongly typed languages, you'll have fewer errors make it past the compiler, which means fewer bugs in the live product. With a weakly typed language, you can pull off some really cool stuff, with much less work! If you don't understand just how things are getting converted though, which the compiler is doing for you, then you're going to have some bugs down the road!
So far, I've found that I'm a bigger fan of strongly typed languages over weakly typed. I've found it a bit more sterile, and I know exactly what I'm working with. I'm not far enough into programming to have hardcore beliefs that I'll die with, but the strict typing of C# or Java feel a bit more natural to me. I've talked with folks at work about this, and two of them laughed saying, "Wait til you try PHP!" One put it this way, "Weak typing gives you all the tools you need to cut off your arm."
In Java, let's say you want to keep track of how many apples you have. First, you'll want to make a variable to store the apple count. Since you just want to keep track of whole apples, you'll make it an integer. We'll be doing math with it, so no Strings here. We know exactly what we're working with here - a numerical count of whole apples.
In Javascript, I figured it would be pretty much the same right? And then I saw "var". Whoa, hold the phone, where's the int? Is it a number, a String, or a character? What's going on here?! I read into Javascript a bit more, and learned more about what "typing" is. Getting your hands dirty with code teaches you a lot more than just writing up a little report on what strongly typed languages are, right?
Java is a strongly typed language, whereas Javascript is a weakly typed language. For a refresher, typing is where each type of data defined by the language you're working. If you're wanting to work with numbers in a strongly typed language, you'll need to specify that your piece of data is a number. Restricting even further, you might have to specify what kind of number it is - is a whole number? Does it have decimals? Is it used for money? If you're wanting to work with letters, you might have to say the piece of data is a string of letters, or maybe just one single character.
Now that you have a type in mind for your data, you'll need to make sure you use proper actions on it. You can't just say "x = potatoe + 3". The compiler looks up at you and tells you to get out, and take your crappy public university degree with you. It doesn't care that you're drowning in student loans, or that your dog requires a grain-free diet. Everything is tightly controlled, which prevents your data from getting manipulated in ways that you weren't expecting.
With a weakly typed language, such as Javascript, you gather your potatoes, your numbers, and your booleans. You hold them high, and say "X = POTATOE + 3 AND TRUE!". The compiler doesn't really know what you're doing, but you sound like you know what you're doing, so he says "You're hired!". The problem is though, do you really know what you're doing? The compiler will take whatever you give it, and figure something out in the end. But you really, really need to know what you're doing.
I've read that it comes down to control versus *fairy dust* creativity (I'm trying not to be biased, I swear). With strongly typed languages, you'll have fewer errors make it past the compiler, which means fewer bugs in the live product. With a weakly typed language, you can pull off some really cool stuff, with much less work! If you don't understand just how things are getting converted though, which the compiler is doing for you, then you're going to have some bugs down the road!
So far, I've found that I'm a bigger fan of strongly typed languages over weakly typed. I've found it a bit more sterile, and I know exactly what I'm working with. I'm not far enough into programming to have hardcore beliefs that I'll die with, but the strict typing of C# or Java feel a bit more natural to me. I've talked with folks at work about this, and two of them laughed saying, "Wait til you try PHP!" One put it this way, "Weak typing gives you all the tools you need to cut off your arm."
Comments
Post a Comment