A style question regarding multi-signature methods
-
Hello all. Now that I have discovered extension methods, I am revisiting my libraries of handy-dandy routines to see where it makes sense to convert them. In doing so I am also revisiting a question about how to handle methods that work on different data types. I am wondering if anyone out there has any opinions or knows any arguments for/against writing such methods in a particular style. I know there probably isn't any one "right" answer, but I thought it would be informative to hear what other programmers think. For instance, I have a lot of methods that work on strings. I also need the same operations to work on character arrays. So I might end up with something like
public int foo ( char[] SourceData ) { ... } public int foo ( string SourceData ) { ... }
I have also handled this using
public int foo ( char[] SourceData ) { return foo(new string(SourceData)); } public int foo ( string SourceData ) { ... }
and even
public int foo ( object SourceData ) { string source; if ( SourceData.GetType() == typeof(string) ) source = SourceData; else if ( SourceData.GetType() == typeof(char[]) ) source = new string(SourceData); else error_condition; ... }
I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types. Opinions? Critisisms? Other ideas? All are welcome.
Clive Pottinger Victoria, BC
-
Hello all. Now that I have discovered extension methods, I am revisiting my libraries of handy-dandy routines to see where it makes sense to convert them. In doing so I am also revisiting a question about how to handle methods that work on different data types. I am wondering if anyone out there has any opinions or knows any arguments for/against writing such methods in a particular style. I know there probably isn't any one "right" answer, but I thought it would be informative to hear what other programmers think. For instance, I have a lot of methods that work on strings. I also need the same operations to work on character arrays. So I might end up with something like
public int foo ( char[] SourceData ) { ... } public int foo ( string SourceData ) { ... }
I have also handled this using
public int foo ( char[] SourceData ) { return foo(new string(SourceData)); } public int foo ( string SourceData ) { ... }
and even
public int foo ( object SourceData ) { string source; if ( SourceData.GetType() == typeof(string) ) source = SourceData; else if ( SourceData.GetType() == typeof(char[]) ) source = new string(SourceData); else error_condition; ... }
I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types. Opinions? Critisisms? Other ideas? All are welcome.
Clive Pottinger Victoria, BC
cpotting wrote:
I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.
Why would generics be overkill. Just because you are limiting to only 2 or 3 data types, generics are still very useful. Let's face it, generics (in most cases) are often only used against a couple of types only. Don't limit yourself just because your code doesn't use every possible type.
Deja View - the feeling that you've seen this post before.
-
cpotting wrote:
I started to look at generics, but it seems that they may be a bit of overkill for routines that only handle 2 or 3 different data types.
Why would generics be overkill. Just because you are limiting to only 2 or 3 data types, generics are still very useful. Let's face it, generics (in most cases) are often only used against a couple of types only. Don't limit yourself just because your code doesn't use every possible type.
Deja View - the feeling that you've seen this post before.
-
led mike wrote:
He may not be.
:laugh: Smooth. You've definitely mastered the one liner.
Deja View - the feeling that you've seen this post before.
-
led mike wrote:
He may not be.
:laugh: Smooth. You've definitely mastered the one liner.
Deja View - the feeling that you've seen this post before.
Thanks Pete. I will take a look a closer look at generics. I'm not quite sure what led mike meant by "He may not be", though.
Clive Pottinger Victoria, BC
-
led mike wrote:
He may not be.
:laugh: Smooth. You've definitely mastered the one liner.
Deja View - the feeling that you've seen this post before.