D
Deckarep
I recently got into somewhat of a debate with a fellow co-worker.
First I asked him if he new of a way to add a method to an existing
class like enhancing a class but without using Inheritance. He asked,
"Why on earth would you want to do this? Just inherit." I told him
other dynamic languages can do this such as JavaScript where you can
just add method to say the Array classes prototype chain. I told him I
found this technique often useful and elegant vs. Inheritance.
But, that's how we do this in JavaScript and it doesn't necessarily
mean it's the right way to do things. Now I hear C# is offering
"Extension Methods" where you can add your own methods to the String
class without having to create your own SuperString class and then
using composition (cause String is sealed) to get what you need. I
know the C#3.0 extension methods are probably just compiler magic
making you *think* that you are actually adding methods to these
existing classes but in my opinion it's how you use the technique. I
can still see an added benefit just for mere convention. I mean, most
programmers will happily use native classes like the String class and
expecting them to use SuperString every where in your program to me
seams kinda presumptious especially if you are really only adding a few
methods to it.
So my question as it stands: Is this a good technique? Are there
other reasons to do this technique because I don't really have a leg to
stand on with my co-worker...he sees the idea as pointless and stupid
when you can just inherit. I brought up the JavaScript example...but
then again...JavaScript is just a browser scripting language which
doesn't nearly have the power of C#.
But then again, I kinda feel maybe I'm right seeing Microsoft is adding
this support to C#3.0.
So what do ya'll think?
-Ralph
First I asked him if he new of a way to add a method to an existing
class like enhancing a class but without using Inheritance. He asked,
"Why on earth would you want to do this? Just inherit." I told him
other dynamic languages can do this such as JavaScript where you can
just add method to say the Array classes prototype chain. I told him I
found this technique often useful and elegant vs. Inheritance.
But, that's how we do this in JavaScript and it doesn't necessarily
mean it's the right way to do things. Now I hear C# is offering
"Extension Methods" where you can add your own methods to the String
class without having to create your own SuperString class and then
using composition (cause String is sealed) to get what you need. I
know the C#3.0 extension methods are probably just compiler magic
making you *think* that you are actually adding methods to these
existing classes but in my opinion it's how you use the technique. I
can still see an added benefit just for mere convention. I mean, most
programmers will happily use native classes like the String class and
expecting them to use SuperString every where in your program to me
seams kinda presumptious especially if you are really only adding a few
methods to it.
So my question as it stands: Is this a good technique? Are there
other reasons to do this technique because I don't really have a leg to
stand on with my co-worker...he sees the idea as pointless and stupid
when you can just inherit. I brought up the JavaScript example...but
then again...JavaScript is just a browser scripting language which
doesn't nearly have the power of C#.
But then again, I kinda feel maybe I'm right seeing Microsoft is adding
this support to C#3.0.
So what do ya'll think?
-Ralph