CleanCode logo
NAVIGATION: first page in sectionprevious pageup one levelnext pagefinal page in section

Guideline VR3: Weak and Strong Typing

There is a lot of debate between which is better: strongly typed languages (like Java) or weakly typed languages (like JavaScript and Perl). Sure, a good developer can write clean code in a weakly typed language. Just like the circus knife-thrower can wield blades without maiming the assistant. But I wouldn't want a merely competent knife-thrower, I would want an expert. In the programming world, there are a lot of programmers who are not experts. Thus, I strongly advocate strongly typed languages.

variable declarations
my $stuff; # perl
$stuff = 25.3;
$stuff = "abc";

var stuff; // javascript
stuff = 25.3;
stuff = "abc";

double stuff; // java
stuff = 25.3;
// stuff = "abc"; // illegal

In Perl or JavaScript, you can create a variable stuff as shown. When you declare it, you do not specify the type. It assumes a type when something is stored in it. But you are also free to assign something else of a different type at any time. Then it assumes that type. And so forth. In Java, on the other hand, you must specify the type with the declaration, and you may only assign values to it which have that type. You do have a limited ability to assign objects of compatible types and convertible types. Compatible types are types derived from the base class of the object's type. Convertible types, allow you to convert from say integer to double; these generally must explicitly show the conversion with a type cast. A type cast clearly and succinctly indicates to the reader (and to the compiler) that you've got a value of one type, you're storing it into a variable of another type, and you know that you are doing it.

Clean code is all about clarity. In a strongly typed language, you can only assign compatible values to variables. If you need to waver from that, you must state your intentions explicitly. If you waver from that accidentally, the compiler will let you know quite promptly.

Some people disdain languages like Java because a lot of the work is spent converting one thing to another. But isn't that what most programming is anyway? When you click search at any web site, most of the work involves converting an entire database into a selective, formatted listing on your screen. When you use a word processor, what you type is converted into nicely formatted, paginated text. When you run a virus checker, it compares its encyclopedia of virus information with the contents of your disk and converts it all into (one hopes) a single line of output -- "No viruses found.".

Here's an example. In Perl, one can open a file for reading via:

	open(READER, $fname);
... but in Java, there are potentially several steps to get to the object which can actually act on your file the way you wish:
	BufferedReader r = new BufferedReader(new FileReader(new File(fname)));

In the Java code, you can pull out one component and replace it with another quite readily. And you can see the steps involved. Plus, you can always convert it to a simpler style by hiding it inside a subroutine. So, yes, the Java code is more complex, but it also gives you more flexibility.

Valid XHTML 1.0!Valid CSS!Get CleanCode at Fast, secure and Free Open Source software downloads
Copyright © 2001-2013 Michael Sorens • Contact usPrivacy Policy
Usage governed by Mozilla Public License 1.1 and CleanCode Courtesy License
CleanCode -- The Website for Clean DesignRevised 2013.06.30