Geeks With Blogs

News My Blog has been MOVED to https://mfreidge.wordpress.com
Michael Freidgeim's OLD Blog My Blog has been MOVED to https://mfreidge.wordpress.com

 

We have a debate with one of my collegues, is it agood style to check, if the object of particular style, and then cast  as this  type. The perfect answer of Jon Skeet   and answers in Cast then check or check then cast? confirmed my point.

//good
    var coke = cola as CocaCola;
    if (coke != null)
    {
        // some unique coca-cola only code
    }
    //worse
    if (cola is CocaCola)
    {
        var coke =  cola as CocaCola;
        // some unique coca-cola only code here.
    }

Posted on Saturday, June 9, 2012 7:35 PM .Net Framework , General Tips | Back to top


Comments on this post: C# tip: do not use “is” type, if you will need cast “as” later

# re: C# tip: do not use “is” type, if you will need cast “as” later
Requesting Gravatar...
They are both bad, because they both mean you are doing things "on" the object, or "with" the object, instead of encapsulation of the logic "in" your object.
Just call a method on the object (for example: "Open"), for all normal drinks you can implement it in the base class, for CocaCola you can override this class and implement "return 'tssssssss'" (the sound of a coke opening).
Better yet, don't use inheritance, use a behavioral design pattern.

I cover some of these lessons life has thought me in this blog post: "http://janvanderhaegen.wordpress.com/2011/11/04/coding-is-optional-a-random-walk-down-oo-road-less-is-more/"
Left by Jan Van der Haegen on Jun 13, 2012 12:21 AM

# re: C# tip: do not use “is” type, if you will need cast “as” later
Requesting Gravatar...
I disagree. I think "as" should only be used when you have a default value.
The problem with as is
1. you can't use it on structs, so if you use "as" you'll have to occasionally use "is" anyhow. so you code becomes inconsistent.
2. Correct exception thrown. Sometimes you won't bother with the is. a (Foo)Bar throws an invalid cast. Using as will throw null reference.
3. Minor, but better scope. declaring the varable inside the if limits the scope more.

As should only be used if you have a default value. In this case you can say
var myString = myObject as string ?? "Some default message";
Left by Chris on Jun 14, 2012 1:07 PM

# re: C# tip: do not use “is” type, if you will need cast “as” later
Requesting Gravatar...
Jan van,
If you start to create your hierarchy from the scratch, using the inheritance or behavioural design pattern is a good approach. However in the created a few years ago architecture redesign objects hierarchy usually too risky.
Chris,
1. Structs are different to classes, so inconsistency is expected.
2. Using as will not throw exception, but will allow safely use only if reference is not null(regardless if the object is null or just different type.
3. Yes, scope is better, but often for reporting in try-catch block it's convenient to have declared objects in outer scope.
Left by Michael.freidgeim on Jun 14, 2012 9:10 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net