ALLINSIGHT

Home of the AlmostImplementedException

cast vs “as”

Another good example for our categories “difference between a duck” AND “CleanCode” is an ordinary ()-cast against “as”-cast.

Why is it in both categories? Both do roughly the same thing and its more a question of code-style which one you use. But more important both use different ways to approaching the same problem and thats the important part for CleanCode.

But first let me explain the difference between them. A plain old simple cast with () only works when your class is of this type. Otherwise you’ll get an InvalidCastException.
The “as”-keyword on the other hand only works with nullable types, because if the cast failes null is returned instead of an exception.

And thats it! All other things, like performance are not realy important. BTW if you use both ways right, there is no difference in performance.
The most important thing you should think about is readability. Thats why there is such a thing like CleanCode.

And with CleanCode in mind, there is a difference between them! A simple cast throws an exception when it failes. And thats only acceptable when it is not expected. So if you have a method which get an object as parameter and that object can only be one specific type, use the ()-cast.
A good example for this is an event-handler:

Here we have to use object as parameter and we know we only want to use Buttons to call this method. if sender is any other type we’ll get an InvalidCastException and the program stops (or a global handler catches it).
Thats perfect, since no other type is allowed the exception indicates thats something wrong.
What if we would use the “as”-keyword instead?

If sender is a differente object then Button again, we’ll get an exception too. But this time its a NullPointer and thats bad. We don’t know what (and where) the error is. It could be that sender was null all along or its the wrong type.
Just imaging you have to find this kind of error in another developers code. You just don’t know what happens and need to debug to find the error.
Thats the opposit of CleanCode!

So every time you exactly know which type of object you expect, use a ()-cast. And also use it if you’re in doubt what to use, cause the ()-cast ist always good.

But if you are not sure which type to expect or null is a valid value, just use “as”.

Lets see another example with both casts:

Here the “as”-keyword is easier to read and to understand. Now you don’t care if sender is null or its of type Button. And this time “as” is sligthly faster then the “is”-()-cast approche, because there you need to check the type 2 times.

Another thing you can see in the example above, both ways are not that different. So its ok if you don’t use the “as”-keyword at all, but its not ok to use it for the first example and get NPEs instead of InvalidCastException.

And to finish this little post, another example where the “as”-keyword is clearly forbidden. A Method-Call:

Who knows why “as” is a bad idea in this example?

If sender is not of type Button, the first method throws an exception. The second one silently ignores that and send null to the method.
Thats very bad and definitely not clean! We don’t know how the method handle the null-value. Maybe we got another NPE or worst the method does nothing.
Nobody will ever notice there are method-calls that do nothing.

Share :

Leave a Reply

Your email address will not be published. Required fields are marked *