r/learnprogramming • u/sw1sh • Jun 09 '12
Types of programming
So i have been teaching myself Java programming for the last two months,and I understand that it's an Object-Oriented Programming language. But from my time of stalking these forums I've read a lot about functional programming,and other types that I don't really understand. I get that I shouldn't expect to know much outside Java after only 2 months,but I'm just interested in how other languages differ from Java.
I've also read about Haskel,Scala and other seemingly unusual languages,and so my question is:
TLDR - "What are the differences between the programming types?"
215
Upvotes
413
u/kqr Jun 09 '12 edited Jun 10 '12
COMPARING TYPE SYSTEMS
As far as your computer is concerned, everything is just numbers. Everything can be converted to numbers, and reasoned about as numbers. Text is numbers, images are numbers, music is numbers, films are numbers. Everything is just numbers man.
Now, think about the following snippet of code (in no particular language):
(Snippet 1)
What should be returned? If we pretend our computer can only store integers (which is as close to the truth as you'll get), how does it store
b? And how does it know that it should convert bothaandbto the same format before doing the addition?This is where type systems come in. In Java, the previous code would look something like this:
(Snippet 2)
Now, we have included extra information. Now, Java will know that
ais an integer andbis a floating point number. Java knows how to deal with the addition now, and to avoid losing precision, it will first convertato a floating-point number and then perform the addition.This is essentially what a type system does at its core. It provides the computer with a way of doing the right thing for the right kind of value. There are a few different approaches to type systems though. Let's first look at what kind of problems can occur.
Pretend you have the following snippet (in no particular language):
(Snippet 3)
Now this doesn't make sense. At all. As a human, we quickly see that you never subtract a name from an age. It doesn't mean anything at all. This is wrong. This is an error.
The type system can catch this. If the type system knows that age is a number and name is a string, the type system can say, "Hey, you're trying to subtract a string from a number. You can't do that, fool!"
How do different kinds of type systems deal with this?
Static type systems
Examples:
C
Java
Haskell
C#
C++
Scala
F#
Objective-C
Go
(List 4)
As you remember, static languages were all about translating your code into machine code, or compiling your code. As a part of this compilation, you might want to look into conflicts among the types. In a language with a static type system, the compiler will say, "Hey! Stop! I can't do this!" if it encounters the code in Snippet 3. It will refuse to compile the program until you fix the error.
When you program in languages with static type systems, the compiler will refuse to compile your code until you have fixed possible type errors. Depending on how you view this, it might be either a benefit (you can't compile programs which are wrong somewhere) or a drawback (you can't compile programs which you as a human know are right, even though they look wrong to the computer.)
Dynamic type systems
Examples:
Python
Ruby
Common Lisp
Scheme
Clojure
JavaScript
PHP
Erlang
Prolog
(List 5)
As you might be able to guess, a dynamic type system is the counterpart to a static type system. When the type system is dynamic, it essentially means that type errors will not be caught by any kind of compiler. If you were to run Snippet 3 in a language with a dynamic type system, it will happily chug along until it hits the offending line.
As with a static type system, if this is good or not depends on how you view it. Some people like being able to run a program which contains some dubious code because they know the code is really correct, or because they know they won't execute that bit of code anyway. Other people prefer that the compiler tells them immediately where the code looks suspicious.
Strong vs. weak type systems
It is easy to think that a "weak" type system is bad, and that a "strong" type system is good. This is absolutely not the case. The strength of a type system simply indicates how minor errors it will trip up on. Technically, the following is wrong, from a type system perspective:
(Snippet 4)
It is "wrong" because you're trying to apply the addition operator to values of two different types. It is a very minor "error" so only a very, very strong type system will refuse to compile/crash at that line. A slightly weaker type system will do what is probably "the Right Thing" in that situation and produce the floating point value
14.3.However, even a weaker type system might trip up on something like what you say in Snippet 3. There are however really, really weak type systems that would allow that thing to go through as well.
Examples of languages with very weak type systems:
JavaScript
PHP (Thanks to /u/TimMensch and /u/mkantor for pointing this out independently)
TCL
Perl
C
Assembly (Thanks to /u/masklinn for the last four)
Examples of languages with pretty strong or very strong type systems:
All the others.
JavaScript is notorious for its weak type system. If you haven't seen it yet, I recommond you watch The Wat Talk which makes fun of the weak typing of JavaScript. I'm not saying weak typing is bad, mind you. To each their own.
(More to come.)