Downcasting is a Code Smell

by Matt 1. January 2007 17:33

You've got to love Jeremy D. Miller. And not only because Jeremy D. Miller is a name my dad would approve of ("You have a middle name for a reason, son"). No, it's mainly because he continually writes dead good stuff.

Take this post on downcasting.

(Downcasting being the act of casting an object reference from a base class to a derived class.)

Just before the Christmas break, I had to send out an email to my team, berating them for doing just this.

Jeremy's a lot more polite about it that I want to be. It's quite simple.

If you're downcasting from a base class to a derived class in your code, your spidey sense should be tingling. Go and talk to someone you trust.

If you actually have the gall to cast an abstract interface to a concrete class, then your spidey sense should be smacking you round the back of the head with a length of 2 x 4. You're almost certainly doing something wrong.

And yes, there are exceptions. There are always exceptions. Hopefully, it's design flaws in 3rd party software (including the .net framework itself) that you can't work around. Or if you're lucky, using object as a generic type.

Just don't do it in your own code please.

Now go read Jeremy D. Miller's post (might as well subscribe while you're there). It's a good one, and has some good links in it. Especially the Tell, Don't Ask link - a concept (like so many others) that work great on the simple examples in the literature, but are not quite so obvious on Real Life Code.

Tags:

Comments (76) -

suv reviews
suv reviews
7/20/2011 10:37:22 PM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.

Reply

iphone 4 apps
iphone 4 apps
7/23/2011 7:32:21 PM #

Is it ok if I quote your article in my monthly newsletter? I would think this article suits my topic perfectly. Well ya, thanks for posting this article.

Reply

iphone app development
iphone app development
7/24/2011 4:17:01 AM #

Per il tuo bambino scegli Moncler. Una scelta di capi, estivi ed invernali, eccezionali. Tuo figlio sarà sempre alla moda e potrà muoversi in totale comodità.

Reply

Philix
Philix United States
11/21/2015 1:01:38 AM #

Superb Web Site.

Reply

philix
philix United States
11/27/2015 3:54:42 PM #

great website.

Reply

Cornelius Styborski
Cornelius Styborski United States
1/15/2016 6:07:41 AM #

Congratulations, Neil! I love this site and I know I'll love the book. Christmas, birthdays, Father's Day 2010  check!

Reply

Antony Goren
Antony Goren United States
1/15/2016 6:43:17 AM #

Congrats! This is such a great website, I know it will translate well into book form. Which is AWESOME!

Reply

Mac Burkart
Mac Burkart United States
1/15/2016 7:11:44 AM #

Hey, congratulations Neil. This site really is something special and it's been a pleasure to share it with my readership too. Looking forward to future awesome things, and I'll send any suggestions that are sufficiently awesome.

Reply

Lidia Loser
Lidia Loser United States
1/15/2016 7:15:18 AM #

I can't wait to read this masterpiece. Ordering off the menu at fast food restaurants and old dangerous playground equipment have to be in the book!

Reply

Corrinne Uzelac
Corrinne Uzelac United States
8/25/2016 4:00:34 AM #

off topic but I was thinking which blog platform are you using for this website? I’m getting tired of WordPress because I’ve had issues with autopcposters and I’m looking at alternatives for another platform. It would be nice if you could point me in the direction of a secure platform.

Reply

昊奇音樂
昊奇音樂 United States
6/16/2017 6:42:58 AM #

☆☆☆☆☆ Logic Pro X 中文教程视频 ☆☆☆☆☆『超过30小时且持续增加的教程内容,从基本操作丶效果器详细解说丶合成器讲解丶混响後制,到House音乐制作。』★一次付费,无限期下载★您获得的内容将远超过於您的付出

Reply

Matthew C. Kriner
Matthew C. Kriner United States
12/6/2017 5:31:31 AM #

Reply

카지노사이트
카지노사이트 United States
7/1/2018 6:49:12 AM #

Don’t wear seat belts lest you drown in your own urine?

Reply

바카라
바카라 United States
7/4/2018 1:24:50 PM #

What’s up, after reading this awesome piece

Reply

Jamaal Nichol
Jamaal Nichol United States
7/20/2018 1:24:21 AM #

I’m quite certain I’ll learn lots of new stuff right

Reply

Rodger Strem
Rodger Strem United States
7/20/2018 9:46:45 PM #

What’s up, after reading this awesome piece

Reply

Mildred Stroik
Mildred Stroik United States
7/20/2018 10:52:25 PM #

I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly.

Reply

Heike Skolnick
Heike Skolnick United States
8/1/2018 7:35:02 AM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Brett Lacerte
Brett Lacerte United States
8/1/2018 7:35:04 AM #

With thanks! Valuable information!

Reply

Rusty Jeck
Rusty Jeck United States
8/1/2018 7:35:04 AM #

With thanks! Valuable information!

Reply

Sterling Eckstein
Sterling Eckstein United States
8/1/2018 7:44:47 AM #

With thanks! Valuable information!

Reply

Francis Hal
Francis Hal United States
8/1/2018 7:54:03 AM #

With thanks! Valuable information!

Reply

Josef Conejo
Josef Conejo United States
8/1/2018 8:28:55 AM #

With thanks! Valuable information!

Reply

Rosann Vanzant
Rosann Vanzant United States
8/1/2018 8:46:45 AM #

With thanks! Valuable information!

Reply

Bernice Blevans
Bernice Blevans United States
8/1/2018 9:29:33 AM #

With thanks! Valuable information!

Reply

Leonardo Mitchem
Leonardo Mitchem United States
8/1/2018 11:17:47 AM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Nigel Dixey
Nigel Dixey United States
8/1/2018 11:58:57 AM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Mauricio Fellman
Mauricio Fellman United States
8/1/2018 12:02:15 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Jacqualine Ruisi
Jacqualine Ruisi United States
8/1/2018 12:27:48 PM #

With thanks! Valuable information!

Reply

Boris Granzella
Boris Granzella United States
8/1/2018 1:12:25 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Shirlee Delmastro
Shirlee Delmastro United States
8/1/2018 3:02:14 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Cherelle Centrich
Cherelle Centrich United States
8/1/2018 3:02:17 PM #

With thanks! Valuable information!

Reply

Cristi Woodie
Cristi Woodie United States
8/1/2018 3:08:04 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Octavio Taper
Octavio Taper United States
8/1/2018 3:08:04 PM #

With thanks! Valuable information!

Reply

Tayna Felipe
Tayna Felipe United States
8/1/2018 3:18:12 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Casey Micalizzi
Casey Micalizzi United States
8/1/2018 3:34:22 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Lynn Flessner
Lynn Flessner United States
8/1/2018 3:45:52 PM #

Black on black in the Charg I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Micah Lyster
Micah Lyster United States
8/1/2018 4:01:06 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Neely Kannard
Neely Kannard United States
8/1/2018 4:10:56 PM #

With thanks! Valuable information!

Reply

Delores Byers
Delores Byers United States
8/1/2018 4:15:52 PM #

With thanks! Valuable information!

Reply

Lamar Beuchler
Lamar Beuchler United States
8/1/2018 5:36:41 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Lucas Gockerell
Lucas Gockerell United States
8/1/2018 5:54:18 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Cristopher Bailly
Cristopher Bailly United States
8/1/2018 6:22:24 PM #

With thanks! Valuable information!

Reply

Lucas Gockerell
Lucas Gockerell United States
8/1/2018 7:04:35 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Vashti Danza
Vashti Danza United States
8/1/2018 7:11:37 PM #

With thanks! Valuable information!

Reply

Shanon Spanner
Shanon Spanner United States
8/1/2018 7:11:40 PM #

With thanks! Valuable information!

Reply

Gregorio Amormino
Gregorio Amormino United States
8/1/2018 8:42:40 PM #

Black on black in the Charger I'm creepin' Rub me the right way, you might get a genie B.o.B, black Houdini

Reply

Monique Deller
Monique Deller United States
8/1/2018 9:05:33 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Donn Evanoski
Donn Evanoski United States
8/1/2018 9:32:54 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Les Cotroneo
Les Cotroneo United States
8/1/2018 9:34:20 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Neil Rundstrom
Neil Rundstrom United States
8/1/2018 9:36:01 PM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Vivian Aughenbaugh
Vivian Aughenbaugh United States
8/1/2018 10:23:02 PM #

Don’t wear seat belts lest you drown in your own urine?

Reply

Gregorio Lapete
Gregorio Lapete United States
8/2/2018 12:06:02 AM #

Don’t wear seat belts lest you drown in you own urine?

Reply

Boris Granzella
Boris Granzella United States
8/2/2018 1:46:51 AM #

With thanks! Valuable information!

Reply

Luciano Emilio
Luciano Emilio United States
8/9/2018 5:01:23 PM #

Don’t wear seat belts lest you drown in your own urine? https://bzp65.com/

Reply

Margherita Politis
Margherita Politis United States
8/9/2018 5:01:24 PM #

of writing i am as well delighted to share my know-how here with colleagues. https://miso7700.com/

Reply

Domonique Read
Domonique Read United States
8/9/2018 5:57:12 PM #

Hello. impressive job. I did not anticipate this. http://bnuphoto.com

Reply

Russel Irimata
Russel Irimata United States
8/9/2018 5:57:13 PM #

good stuff. I will make sure to bookmark your blog. https://yong79.com/

Reply

Irina Vanhorne
Irina Vanhorne United States
8/9/2018 6:14:33 PM #

What’s up, after reading this awesome piece https://newone2017.com/

Reply

Pamela Legard
Pamela Legard United States
8/9/2018 6:26:14 PM #

It?s hard to come by knowledgeable people on this subject, however, you https://php665.com/

Reply

Marlon Kincer
Marlon Kincer United States
8/9/2018 8:00:23 PM #

It?s hard to come by knowledgeable people on this subject, however, you http://casinossir.com

Reply

China Lakes
China Lakes United States
8/11/2018 2:37:11 PM #

I conceive this web site has got some real good information for everyone http://bambam365.com/

Reply

Elfrieda Kawelo
Elfrieda Kawelo United States
8/13/2018 1:19:40 PM #

ng steps Henry Flower comes forward to left front centre. He wears a dark mantle and drooping plumed sombrero. He carries a silverstringed inlaid dulcimer and a longstemmed bamboo Jacob's pipe, its clay bowl fashioned as a female head. He wears dark velvet hose and silverbuckled pumps. He has the romantic Saviour's fac<br>Please visit my website : 우리카지노 https://gam77.xyz/우리카지노-2

Reply

Rosalba Viale
Rosalba Viale United States
8/13/2018 5:10:42 PM #

I got what you mean,saved to fav, very decent site. https://bzp65.com/

Reply

Helaine Cooperman
Helaine Cooperman United States
8/13/2018 6:11:23 PM #

It is amusing to view the unpar one ar alleled embarra two ars is it? double ess ment of a harassed pedlar while gauging au the symmetry with a y of a peeled pear under a cemetery wall. Silly, isn't it? Cemetery put in of course on account of the symmetry. I should have said when he clapped on his topper. Thank you<br> my site : 온라인카지노 https://gam77.xyz/온라인카지노

Reply

Annette Mcclinsey
Annette Mcclinsey United States
8/13/2018 6:30:56 PM #

o knows is that true about the woman he keeps? Not pleasant for the wife. Yet they say, who was it told me, there is no carnal. You would imagine that would get played out pretty quick. Yes, it was Crofton met him one evening bringing her a pound of rumpsteak. What is this she was? Barmaid in Jury's. Or the Moira, was <br>Also visit my site : 온라인카지노 https://gam77.xyz/온라인카지노

Reply

Evelia Machinsky
Evelia Machinsky United States
8/13/2018 10:04:48 PM #

child born out of her ear because her bumgut fell out a nice word for any priest to write and her ae as if any fool wouldnt know what that meant I hate that pretending of all things with that old blackguards face on him anybody can see its not true and that Ruby and Fair Tyrants he brought me that twice I remember whe<br>Also visit my site : 바다이야기 https://bxx100.com

Reply

Erma Krows
Erma Krows United States
8/13/2018 10:49:08 PM #

.O.U. Do you mean to fly in the face of the tradition of three centuries? John Eglinton's carping voice asked. Her ghost at least has been laid for ever. She died, for literature at least, before she was born. She died, Stephen retorted, sixtyseven years after she was born. She saw him into and out of the world. <br> my site : 소라넷 https://gam77.xyz

Reply

Claudia Liska
Claudia Liska United States
8/14/2018 4:50:51 AM #

corns afraid hed get bloodpoisoning but if it was a thing I was sick then wed see what attention only of course the woman hides it not to give all the trouble they do yes he came somewhere Im sure by his appetite anyway love its not or hed be off his feed thinking of her so either it was one of those night women if it <br>and visit my page : 인터넷바카라 https://akram37.com

Reply

Ellsworth Pizzico
Ellsworth Pizzico United States
8/14/2018 7:39:48 AM #

old rag of velveteen, and I'll lay you what you like she did it on purpose... MRS BREEN: She did, of course, the cat! Don't tell me! Nice adviser! BLOOM: Because it didn't suit you one quarter as well as the other ducky little tammy toque with the bird of paradise wing in it that I admired on you and you honest<br>and visit my page : 인터넷바카라 https://akram37.com

Reply

Elisha Helland
Elisha Helland United States
8/14/2018 7:44:08 AM #

And the sergeant grinning up. She had that cream gown on with the rip she never stitched. Give us a touch, Poldy. God, I'm dying for it. How life begins. Got big then. Had to refuse the Greystones concert. My son inside her. I could have helped him on in life. I could. Make him independent. Learn German too. A<br>Please visit my webpage : 바다이야기 https://bxx100.com

Reply

Norbert Lanteigne
Norbert Lanteigne United States
8/14/2018 7:54:15 AM #

Hello. impressive job. I did not anticipate this. https://bzp65.com/

Reply

Frankie Tuell
Frankie Tuell United States
8/14/2018 3:24:58 PM #

arket waiting for the poleaxe to split their skulls open. Moo. Poor trembling calves. Meh. Staggering bob. Bubble and squeak. Butchers' buckets wobbly lights. Give us that brisket off the hook. Plup. Rawhead and bloody bones. Flayed glasseyed sheep hung from their haunches, sheepsnouts bloodypapered snivelling nosejam <br>Please visit my website : 우리카지노 https://gam77.xyz

Reply

Numbers Lauren
Numbers Lauren United States
8/14/2018 3:33:32 PM #

remarked to his protégé in an audible tone of voice a propos of the battle royal in the street which was still raging fast and furious: A beautiful language. I mean for singing purposes. Why do you not write your poetry in that language? Bella Poetria! It is so melodious and full. Belladonna. Voglio. Stephen<br>Also visit my webpage : 온라인카지노 https://gam77.xyz

Reply

Tamekia Posten
Tamekia Posten United States
8/14/2018 4:31:52 PM #

d by mothers of memory. PRIVATE CARR: (To Cissy Caffrey) Was he insulting you? STEPHEN: Addressed her in vocative feminine. Probably neuter. Ungenitive. VOICES: No, he didn't. I seen him. The girl there. He was in Mrs Cohen's. What's up? Soldier and civilian. CISSY CAFFREY: I was in company with the sol<br>Please visit my website : 카지노 https://bxx100.com

Reply

Lance Heinzle
Lance Heinzle United States
8/14/2018 6:14:07 PM #

I like the helpful info you provide in your articles. I will bookmark your weblog and check again here regularly. https://bzp65.com/

Reply

Add comment

biuquote
  • Comment
  • Preview
Loading

Rel=Me

Month List

RecentComments

Comment RSS