Thursday, 24 February 2011

Polymorphism, really... what's that...? omg you need a slap in the face!

Well it begins...
Before I start I have to admit I am also guilty of this but it needs to come out.

Why the hell do I have to see code with 4 million if and switch statements, especially when its switching or if'ing on an enum called <some name>Type and of course to get me more mad, yes its in the base class too, what a genius, what the hell are you doing!

Yes that's right, it even has the postfix of "Type", surely that has to give some kind of clue, that hmmmm, maybe this enum your switching on, shouldn't be there, and yes you can do it without having to switch, that's what polymorphism helps with, let me try to demonstrate...

Its not a superb example, but its the first thing I could think of, hopefully I can change it to something better when I can think of something, but for now I am hoping I can get some of the point of polymorphism across.

not so nice way...
    8     public enum CarType
    9     {
   10         Ferrari,
   11         MorrisMinor,
   12         SomethingElse
   13     }
   14 
   15     public class Car
   16     {
   17         public CarType CarType { get; set; }
   18 
   19         public void StartCar()
   20         {
   21             switch (CarType)
   22             {
   23                 case CarType.Ferrari:
   24                     //Car Senses key fob.
   25                     //Car starts.
   26                     break;
   27 
   28                 case CarType.MorrisMinor:
   29                     //Key fob turned.
   30                     //Car starts.
   31                     break;
   32 
   33                 case CarType.SomethingElse:
   34                     //Some random stuff to get this car to start
   35                     break;
   36 
   37                 default:
   38                     //Do some default stuff to start all other cars.
   39                     break;
   40             }
   41         }
   42     }
   43 
   44     public class Ferrari : Car
   45     {
   46         public Ferrari()
   47         {
   48             CarType = CarType.Ferrari;
   49         }
   50     }
   51 
   52     public class MorrisMinor : Car
   53     {
   54         public MorrisMinor()
   55         {
   56             CarType = CarType.MorrisMinor;
   57         }
   58 
   59     }
   60 
   61     public class SomethingElse : Car
   62     {
   63         public SomethingElse()
   64         {
   65             CarType = CarType.SomethingElse;
   66         }
   67 
   68     }
   70 
   71     //Some code somehwere to start the car you have.
   72     var ferrari = new Ferrari();
   73     var morrisMinor = new MorrisMinor();
   74     var somethingElse = new SomethingElse();
   75     ferrari.StartCar();
   76     morrisMinor.StartCar();
   77     somethingElse.StartCar();

So this has the 3 types of cars, and they all should start in a different way. When you create an instance of these cars and call StartCar they all call the same method, which then switches on some dodgy enum to distinguish the types, pffff.

OK it works but a car doesn't need to ask itself if its a ferrari, or a morris minor. What happens if a new car comes out does that mean all the other cars then need to be upgraded to then ask itself if its the new car on the market? Therefore, increasing the switch/if statement every time. Sounds like that could be a nightmare to me.

a nicer way...

    8     public class Car
    9     {
   10         public virtual void StartCar()
   11         {
   12             //Do some default stuff to start all other cars.
   13         }
   14     }
   15 
   16     public class Ferrari : Car
   17     {
   18         public override void StartCar()
   19         {
   20             //Car Senses key fob.
   21             //Car starts.
   22         }
   23     }
   24 
   25     public class MorrisMinor : Car
   26     {
   27         public override void  StartCar()
   28         {
   29             //Key fob turned.
   30             //Car starts.
   31         }
   32     }
   33 
   34     public class SomethingElse : Car
   35     {
   36         public override void  StartCar()
   37         {
   38             //Some random stuff to get this car to start
   39         }
   40     }
   41 
   42 
   43     //Some code somehwere to start the car you have.
   44     var ferrari = new Ferrari();
   45     var morrisMinor = new MorrisMinor();
   46     var somethingElse = new SomethingElse();
   47     ferrari.StartCar();
   48     morrisMinor.StartCar();
   49     somethingElse.StartCar();        


Ok here I have completely removed the enum, as its completely not needed. We already have the type, its a Ferrari, or MorrisMinor, the base class (Car) doesn't care.

I have made the StartCar method a virtual method so all the base classes can now implement their own implementation, they have the knowledge on how to start themselves. So if a new car comes along they can then implement their own StartCar method, and no other car needs to know about it. If StartCar isn't implemented then the default one in the base class will be used.

Look no more switch or if, its all gone. You now don't need to read complex code, well it wasn't complex as it was but when new types start appearing it will creep in there.

I hope my explanation makes some sense, if you like, don't like, or would like to suggest something, let me know either way, or of course just slap me! I am learning here too :-) I think I can probably go into some more depth on how it works but at the moment I think I will leave that for now, hopefully there is enough there to understand the basics of getting rid of them nasty if's and switches.

I am not sure if there is any kind of excuse to have to do the "not so nice way...", maybe I am missing something, but I know its easily done, done it myself. Maybe its a lack of time and pressure and your brain only thinks like a 'C' coder in straight lines (sorry 'C' coders out there I used to be one too, very long time ago, and if I am wrong there, please tell me and give me a slap!). I really don't know as I am sure the second way is actually less code and thinking.

That's another thing what gets on my tits too is seeing OO code written like 'C' (before I explode screaming, I will have to save that for another blog)

Hmmmm, I wonder if its possible to write code without if's, yes ban them, maybe bring a flag out in the compiler you can turn them off! I think that's another post rant too.

I can't believe I keep generating new ideas for a blog post without having even thought about it before. This bloging/ranting is superb! I recommend you try it.

Hopefully I will get to some more complex things in the future, but I guess I am learning to write too at the moment.

Tuesday, 22 February 2011

More why's and how's...

Well it has to be said a big thanks to a couple of new colleagues I have worked with recently as to why I am doing this. That is to a Justin Blackwell (I am sure Justin will have something somewhere to link) and a Simon Lovely

With the introduction to Skills Matter and heading off to only a couple of free talks, this started to get my taste for really understanding things and bettering my skills, thought nothing of it really as it was only a little tingling.
Only been to 2 of them, that was

  • with Udi Dahan and a talk on CQRS, was bloody brilliant! really got me thinking about a few things, really interesting.
  • and the other was with Ayende Rahien with a kind of talk on how to go about making software that people want, the WOW factor!
Both of them superb! But also makes you realise the ton of crap that is out there!

Then talking to Simon and Justin more they introduced me to a lot of new blogs (well I say 'new' what I really mean is my first blogs and people to start following and read up about). 


Unbelievable I was in this cloud of denial for so long, the stuff I started to learn from a few of these posts... well... that tingle became a bit more than a tingle, now my passion started to grow more for learning and picking up new things, what people were up to out there and what was about and around the corner.
I still can't find something these guys don't know about yet, will do one day ;-) lol

They then mentioned to me DDD9 was coming up, well of course me sitting there in my cloud "What the hell does that mean?". Of course found out off them what it was and I think I actually managed to register before them, in the end I had to remind them to apply ;-) (Check me out!) Well went along and absolutely thoroughly enjoyed what I saw and the guys delivering the talks. (Probably will try and talk about this day in another post if I remember between my angry rants)

Unbelievably I think I have learnt more in the last several months than in the last 10 or so years career in  software development. I noticed with these guys they seemed to know hell of a lot about all sorts of fields of programming and there I am, well I know C++, I know .NET C# (I thought I did at least) I know databases and to layer stuff nicely so code is grouped nicely, all the kind of OO around it all to make it lovely and maintainable. (well it was a bit more than that I hope...).

OMG when I started to have words thrown at me like:

  • ORM's (I actually started doing my own ORM as I thought it would be an awesome idea, but I didn't know that it was an ORM I was actually creating), what a nugget...!
  • DI, IOC, 
  • Document DB's, 
  • Automapper, 
  • Linq, 
  • CQRS, 
  • patterns(which I used without realising they were patterns already), 
  • I am sure there is more, but can't think of any at the moment
then all the names of software that can handle all this stuff, my brain pretty much exploded and I was blocked with all this information overload. I was then curious on how the hell these guys knew it, done it, learned about it, etc..

I found out... well I think I have... could be different for everyone, following blogs, posts, trying a couple of things out, and now to actually to write down what I think, will hopefully imprint it into my brain more.

Inspired and a hunger for knowledge is an understatement, I want to write about so much stuff, so I can get my understanding across to who ever may want to look at it. Knowing deep down I am learning hardcore style. Maybe someone else will learn off me, now that would be awesome feedback!
Maybe I will get the balls and start posting on Stack Overflow but one thing at a time hey ;-)

Actually I have already learned from my first blog, maybe only a small thing but its the fact now I remember who that guy was who wrote that article about something, Jeff Atwood

Well thanks for reading again... who ever is out there
Rants incoming...

Monday, 21 February 2011

My First EVER Post!

Well here it is...

Its about time I have started this, I've been putting it off far too long, well I say putting it off, I probably mean ignoring, denying, refusing that these things exist, and that I have actually anything important to say. I then realised maybe its not that I may not have anything important to say, I may do, its more about practising to write better, formulate ideas, maybe be able to provide solutions to problems too, and I guess most importantly if possible to get feedback of any sort, what a great way to learn ;-). 

Two things kicked me over the edge to crack on and start writing:
  • this post by Jeff Atwood, great post! Anyone who is half thinking of or maybe denying the existence of bloging, writing like I was, take a read... let me know how you feel afterwards ;-)
  • and starting to write an angry email about the absolute school boy error code that I seem to have to put up with, and realising maybe I should blog.
...then the idea was formed, AngryWelshCoder, because I was spitting feathers at my computer, I am Welsh and a coder too hopefully.

I am still learning these kind of gadgets and also how to write too, so hopefully my practice will give me a better knowledge on these things and also how to write, I can't wait...

This blog may change about too, as I am still getting used to how all this stuff works. How much shall I write about? Am I allowed to swear? Should I apologise now up front for all my future posts that I will be so angry in? maybe... So much to think and write...
My brain is kind of running away with me at the moment in trying to get all this down...

Should I stop my post now or shall I continue, I have no idea! I actually have plenty below this already that I am going to cut out and stick it another post... again about more why's and how's I got inspired to do this, but I promise after that I will start to get angry!