We’ll have no casting here…

Spam, Spam, Spam, Spam, Spam, Spam, Spam.. Java’s Casts of Thousands More than any other programming language Java forces programmers to embed hard-coded knowledge throughout their code about the types of data items. The numerous explicit cast [Small Values of Cool]

Generally speaking, numerous explicit casts are a sign your design is a bit suspect. Chances are there’s some duplication to be removed (so numerous casts become just one), or the design is attempting to be too generic ie. ‘built for flexibility’, which often (ironically) results in a very brittle and inflexible codebase. I tend to find myself using casts when (a) dealing with the collections framework, and (b) writing framework code. Be suspicious if there are lots in ‘normal’ code.

Added later: having followed the post to its source, here, it looks like the code is suffering from from the ‘too generic’ problem. The Additive interface appears to be far too general to be useful. This is just my opinion of course, but I find that very specific and well defined interfaces lend themselves to flexible code far more than vague ones do.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s