Generics instead of parameters?
-
Assuming that I followed your naming convention in assuming that both IMyInterfaceClass1 and IMyInterfaceClass2 are expected to implement IMyInterface, you both are partially right. Your solution is superior on insertion, while your coworker's solution is superior on removal. It is better to accept an instance of IMyInterfaceClass on insertion, because it does not tie your implementation to classes with default constructors. It is better to use a generic on removal because doing so lets you have the compiler enforce the requirement that IMyInterfaceClass1 and IMyInterfaceClass2 are implementations of IMyInterface, rather than relying on a run-time check.
That sounds good. Although the removing method checks if the given type is in the list and if it is it removes it. If it is not it simply does nothing. Although the generic would make it clear that you can only remove IMyInterfaceClasses, since those are the only ones you can add :) My 5 for the suggestion.
It's an OO world.
-
I had a discussion at work today. I made a
Class
that keeps aPrivate Dictionary(Of String, List(Of IMyInterface))
. I have exposed someMethods
that allow adding to theList(Of IMyInterface)
. The managing of theDictionary
is handled in myClass
. Now to add or delete an item to or from a list, that belongs to aKey
in theDictionary
I have made aMethod
that can be called like:' Adding
MyClass.AddToList("Key", New IMyInterfaceClass1) ' This will create a new Key in the Dictionary and a new List.
MyClass.AddToList("Key", New IMyInterfaceClass2) ' This will simply add a new item to the List.
' etc.' Deleting
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass1)) ' This will delete all instances of this Class from the List.
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass2))
' etc.Now a co-worker said that a beginning programmer could not understand this(?) and that it should look like:
' Adding
MyClass.AddToList(Of IMyInterfaceClass1)("Key") ' Works same as above, different syntax.
MyClass.AddToList(Of IMyInterfaceClass2)("Key")
' etc.' Deleting
MyClass.DeleteFromList(Of IMyInterfaceClass1)("Key")
MyClass.DeleteFromList(Of IMyInterfaceClass2)("Key")
' etc.I can agree with him that his method looks pretty nice, but programming is not about beauty of code (not completely anyway). According to my co-worker my
Class
should be responsible for the creation of theType
that is given to theGeneric Method
. I do not think that creatingClasses
usingReflection
is really best practice if you have other options, and I have never seen MicrosoftClasses
(or anyClass
) that works like this. On top of that it becomes impossible to add aIMyInterfaceClass
that requires parameters in its constructor. Have I really not understood anything about programming or am I really so good that I am the only one who understands how to passClasses
as a parameter to aFunction
? :confused:It's an OO world.
The other posts have already given a good answer to your question, so all I'd like to add is: When first reading the code, your version of the code is clearer (to me). It's very clear that you are adding what's in the parameter. In your co-workers example, I'd think AddToList does something with adding "Key" to a list, and I wouldn't readily be able to figure out what the generic is doing there in that function. Same with the deletion: your code makes it clear *what* you are deleting, whereas the alternative leaves it unclear to me what is actually being deleted. Though it is less confusing with the delete than with the add. So, for first time readability, I'd use your code too. Combining that with the previous comments: either leave your code as it is, or use your code for adding and the co-workers code for removing (so you get the compile-time type checks). Best Regards, MicroVirus
-
That sounds good. Although the removing method checks if the given type is in the list and if it is it removes it. If it is not it simply does nothing. Although the generic would make it clear that you can only remove IMyInterfaceClasses, since those are the only ones you can add :) My 5 for the suggestion.
It's an OO world.
Silently ignoring a call that passes an incompatible type is not a good idea. In 100 cases out of 100, calls like this indicate that the caller has made a typo: he couldn't have possibly added the item that he is trying to remove, so it's a logical error. As the result, whatever he meant to remove does not get removed, and it all happens silently. The piece of code that finds the item that has been left behind may be far away from the code containing the actual error. That's why I'd prefer a generic: it catches the typos at compile time, preventing potentially costly debugging sessions virtually for free.
-
Silently ignoring a call that passes an incompatible type is not a good idea. In 100 cases out of 100, calls like this indicate that the caller has made a typo: he couldn't have possibly added the item that he is trying to remove, so it's a logical error. As the result, whatever he meant to remove does not get removed, and it all happens silently. The piece of code that finds the item that has been left behind may be far away from the code containing the actual error. That's why I'd prefer a generic: it catches the typos at compile time, preventing potentially costly debugging sessions virtually for free.
dasblinkenlight wrote:
In 100 cases out of 100, ...That's why I'd prefer a generic: it catches the typos at compile time, preventing potentially costly debugging sessions virtually for free.
In 40 years of programming I haven't seen 100 errors associated with incorrect type usage. Not even sure I have seen 10. On the other hand I have seen more than 10 and probably as many as 50 logic errors in code in the past 4 years that either were explicit security problems or had the potential for that. And thousands of other types of logic errors. And quite few very obtuse errors some that required hours (at least) to debug involving reflection. So generics are amusing but they are far from vital.
-
I had a discussion at work today. I made a
Class
that keeps aPrivate Dictionary(Of String, List(Of IMyInterface))
. I have exposed someMethods
that allow adding to theList(Of IMyInterface)
. The managing of theDictionary
is handled in myClass
. Now to add or delete an item to or from a list, that belongs to aKey
in theDictionary
I have made aMethod
that can be called like:' Adding
MyClass.AddToList("Key", New IMyInterfaceClass1) ' This will create a new Key in the Dictionary and a new List.
MyClass.AddToList("Key", New IMyInterfaceClass2) ' This will simply add a new item to the List.
' etc.' Deleting
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass1)) ' This will delete all instances of this Class from the List.
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass2))
' etc.Now a co-worker said that a beginning programmer could not understand this(?) and that it should look like:
' Adding
MyClass.AddToList(Of IMyInterfaceClass1)("Key") ' Works same as above, different syntax.
MyClass.AddToList(Of IMyInterfaceClass2)("Key")
' etc.' Deleting
MyClass.DeleteFromList(Of IMyInterfaceClass1)("Key")
MyClass.DeleteFromList(Of IMyInterfaceClass2)("Key")
' etc.I can agree with him that his method looks pretty nice, but programming is not about beauty of code (not completely anyway). According to my co-worker my
Class
should be responsible for the creation of theType
that is given to theGeneric Method
. I do not think that creatingClasses
usingReflection
is really best practice if you have other options, and I have never seen MicrosoftClasses
(or anyClass
) that works like this. On top of that it becomes impossible to add aIMyInterfaceClass
that requires parameters in its constructor. Have I really not understood anything about programming or am I really so good that I am the only one who understands how to passClasses
as a parameter to aFunction
? :confused:It's an OO world.
Generics is about knowing the type at compile-time - you only know the type at execution time. There are two ways of fixing this: * Make the generic code call the non-generic code instead. That's easy, but you won't have any of the benefits of generics. * Use reflection to call the generic code from the non-generic code. That's fiddly and won't perform as well, but you can deprecate the non-generic code and eventually remove it.
-
I had a discussion at work today. I made a
Class
that keeps aPrivate Dictionary(Of String, List(Of IMyInterface))
. I have exposed someMethods
that allow adding to theList(Of IMyInterface)
. The managing of theDictionary
is handled in myClass
. Now to add or delete an item to or from a list, that belongs to aKey
in theDictionary
I have made aMethod
that can be called like:' Adding
MyClass.AddToList("Key", New IMyInterfaceClass1) ' This will create a new Key in the Dictionary and a new List.
MyClass.AddToList("Key", New IMyInterfaceClass2) ' This will simply add a new item to the List.
' etc.' Deleting
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass1)) ' This will delete all instances of this Class from the List.
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass2))
' etc.Now a co-worker said that a beginning programmer could not understand this(?) and that it should look like:
' Adding
MyClass.AddToList(Of IMyInterfaceClass1)("Key") ' Works same as above, different syntax.
MyClass.AddToList(Of IMyInterfaceClass2)("Key")
' etc.' Deleting
MyClass.DeleteFromList(Of IMyInterfaceClass1)("Key")
MyClass.DeleteFromList(Of IMyInterfaceClass2)("Key")
' etc.I can agree with him that his method looks pretty nice, but programming is not about beauty of code (not completely anyway). According to my co-worker my
Class
should be responsible for the creation of theType
that is given to theGeneric Method
. I do not think that creatingClasses
usingReflection
is really best practice if you have other options, and I have never seen MicrosoftClasses
(or anyClass
) that works like this. On top of that it becomes impossible to add aIMyInterfaceClass
that requires parameters in its constructor. Have I really not understood anything about programming or am I really so good that I am the only one who understands how to passClasses
as a parameter to aFunction
? :confused:It's an OO world.
I don't follow the code your coworkers wrote. I understand your code though, it makes perfect sense. If you do any MVVM programming, you have pass not only classes, but delegates (which is even more confusing) to methods in order to do asynchronous programming. Your co worker is all kinds of crazy.
I didn't get any requirements for the signature
-
I don't follow the code your coworkers wrote. I understand your code though, it makes perfect sense. If you do any MVVM programming, you have pass not only classes, but delegates (which is even more confusing) to methods in order to do asynchronous programming. Your co worker is all kinds of crazy.
I didn't get any requirements for the signature
ToddHileHoffer wrote:
Your co worker is all kinds of crazy.
I was just going to bed. After reading this I will sleep very well :laugh: I do not think delegates are even allowed in our company... To confusing, as you mentioned :^)
It's an OO world.
-
I had a discussion at work today. I made a
Class
that keeps aPrivate Dictionary(Of String, List(Of IMyInterface))
. I have exposed someMethods
that allow adding to theList(Of IMyInterface)
. The managing of theDictionary
is handled in myClass
. Now to add or delete an item to or from a list, that belongs to aKey
in theDictionary
I have made aMethod
that can be called like:' Adding
MyClass.AddToList("Key", New IMyInterfaceClass1) ' This will create a new Key in the Dictionary and a new List.
MyClass.AddToList("Key", New IMyInterfaceClass2) ' This will simply add a new item to the List.
' etc.' Deleting
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass1)) ' This will delete all instances of this Class from the List.
MyClass.DeleteFromList("Key", TypeOf(IMyInterfaceClass2))
' etc.Now a co-worker said that a beginning programmer could not understand this(?) and that it should look like:
' Adding
MyClass.AddToList(Of IMyInterfaceClass1)("Key") ' Works same as above, different syntax.
MyClass.AddToList(Of IMyInterfaceClass2)("Key")
' etc.' Deleting
MyClass.DeleteFromList(Of IMyInterfaceClass1)("Key")
MyClass.DeleteFromList(Of IMyInterfaceClass2)("Key")
' etc.I can agree with him that his method looks pretty nice, but programming is not about beauty of code (not completely anyway). According to my co-worker my
Class
should be responsible for the creation of theType
that is given to theGeneric Method
. I do not think that creatingClasses
usingReflection
is really best practice if you have other options, and I have never seen MicrosoftClasses
(or anyClass
) that works like this. On top of that it becomes impossible to add aIMyInterfaceClass
that requires parameters in its constructor. Have I really not understood anything about programming or am I really so good that I am the only one who understands how to passClasses
as a parameter to aFunction
? :confused:It's an OO world.
I read the question and all the responses. All were at a high level. I am a beginning programmer, 1 1/2 quarters to go for my degree. The first implementation makes the most sense to me. Up until now I had never seen parameters sent separatly i.e f()() they have always been f( , ). As I said, I am beginning and may be all wet in the way I read the code, but will use this post to try some new ideas. Thanks, this was interesting. dj
-
I read the question and all the responses. All were at a high level. I am a beginning programmer, 1 1/2 quarters to go for my degree. The first implementation makes the most sense to me. Up until now I had never seen parameters sent separatly i.e f()() they have always been f( , ). As I said, I am beginning and may be all wet in the way I read the code, but will use this post to try some new ideas. Thanks, this was interesting. dj
My example may seem a bit weird to you. Try declaring a List(Of String). You probably have done that sometime? Well, surprise, you can actually say List(Of String)(10), where 10 is the capacity of the list, which is the same syntax as my example. Perhaps it would be clearer in C#, where it would be typed List(10);. You can now clearly see there is a difference between the first and second 'parameters' :) The first specifies a Type (for example String or Integer, which will be used elsewhere in the Class or Method) while the second are actually parameters you pass to the method, usually of the Type you just specified. I think when you try it out you will find it very easy :)
It's an OO world.
-
My example may seem a bit weird to you. Try declaring a List(Of String). You probably have done that sometime? Well, surprise, you can actually say List(Of String)(10), where 10 is the capacity of the list, which is the same syntax as my example. Perhaps it would be clearer in C#, where it would be typed List(10);. You can now clearly see there is a difference between the first and second 'parameters' :) The first specifies a Type (for example String or Integer, which will be used elsewhere in the Class or Method) while the second are actually parameters you pass to the method, usually of the Type you just specified. I think when you try it out you will find it very easy :)
It's an OO world.
Thank you, that clears up the syntax for me. I will have to find a required progam that I have to write to try it out on. DJ
-
Thank you, that clears up the syntax for me. I will have to find a required progam that I have to write to try it out on. DJ
Actually a List(Of T) can be used in almost any program. You could look in the System.Collections.Generic Namespace to find more types of Generic Collections. Or you could write your own Generic Class. I always just write some stupid program that does absolutely nothing, but where I use some techniques I am not yet familiar with to practice. Good luck! :thumbsup:
It's an OO world.