Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Generics instead of parameters?

Generics instead of parameters?

Scheduled Pinned Locked Moved .NET (Core and Framework)
discussiontutorialquestion
15 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dasblinkenlight

    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.

    Sander RosselS Offline
    Sander RosselS Offline
    Sander Rossel
    wrote on last edited by
    #5

    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.

    D 1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      I had a discussion at work today. I made a Class that keeps a Private Dictionary(Of String, List(Of IMyInterface)). I have exposed some Methods that allow adding to the List(Of IMyInterface). The managing of the Dictionary is handled in my Class. Now to add or delete an item to or from a list, that belongs to a Key in the Dictionary I have made a Method 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 the Type that is given to the Generic Method. I do not think that creating Classes using Reflection is really best practice if you have other options, and I have never seen Microsoft Classes (or any Class) that works like this. On top of that it becomes impossible to add a IMyInterfaceClass 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 pass Classes as a parameter to a Function? :confused:

      It's an OO world.

      M Offline
      M Offline
      MicroVirus
      wrote on last edited by
      #6

      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

      1 Reply Last reply
      0
      • Sander RosselS Sander Rossel

        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.

        D Offline
        D Offline
        dasblinkenlight
        wrote on last edited by
        #7

        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.

        J 1 Reply Last reply
        0
        • D dasblinkenlight

          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.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #8

          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.

          1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            I had a discussion at work today. I made a Class that keeps a Private Dictionary(Of String, List(Of IMyInterface)). I have exposed some Methods that allow adding to the List(Of IMyInterface). The managing of the Dictionary is handled in my Class. Now to add or delete an item to or from a list, that belongs to a Key in the Dictionary I have made a Method 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 the Type that is given to the Generic Method. I do not think that creating Classes using Reflection is really best practice if you have other options, and I have never seen Microsoft Classes (or any Class) that works like this. On top of that it becomes impossible to add a IMyInterfaceClass 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 pass Classes as a parameter to a Function? :confused:

            It's an OO world.

            P Offline
            P Offline
            Prasanta_Prince
            wrote on last edited by
            #9

            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.

            1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              I had a discussion at work today. I made a Class that keeps a Private Dictionary(Of String, List(Of IMyInterface)). I have exposed some Methods that allow adding to the List(Of IMyInterface). The managing of the Dictionary is handled in my Class. Now to add or delete an item to or from a list, that belongs to a Key in the Dictionary I have made a Method 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 the Type that is given to the Generic Method. I do not think that creating Classes using Reflection is really best practice if you have other options, and I have never seen Microsoft Classes (or any Class) that works like this. On top of that it becomes impossible to add a IMyInterfaceClass 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 pass Classes as a parameter to a Function? :confused:

              It's an OO world.

              T Offline
              T Offline
              ToddHileHoffer
              wrote on last edited by
              #10

              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

              Sander RosselS 1 Reply Last reply
              0
              • T ToddHileHoffer

                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

                Sander RosselS Offline
                Sander RosselS Offline
                Sander Rossel
                wrote on last edited by
                #11

                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.

                1 Reply Last reply
                0
                • Sander RosselS Sander Rossel

                  I had a discussion at work today. I made a Class that keeps a Private Dictionary(Of String, List(Of IMyInterface)). I have exposed some Methods that allow adding to the List(Of IMyInterface). The managing of the Dictionary is handled in my Class. Now to add or delete an item to or from a list, that belongs to a Key in the Dictionary I have made a Method 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 the Type that is given to the Generic Method. I do not think that creating Classes using Reflection is really best practice if you have other options, and I have never seen Microsoft Classes (or any Class) that works like this. On top of that it becomes impossible to add a IMyInterfaceClass 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 pass Classes as a parameter to a Function? :confused:

                  It's an OO world.

                  D Offline
                  D Offline
                  David Farrow
                  wrote on last edited by
                  #12

                  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

                  Sander RosselS 1 Reply Last reply
                  0
                  • D David Farrow

                    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

                    Sander RosselS Offline
                    Sander RosselS Offline
                    Sander Rossel
                    wrote on last edited by
                    #13

                    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.

                    D 1 Reply Last reply
                    0
                    • Sander RosselS Sander Rossel

                      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.

                      D Offline
                      D Offline
                      David Farrow
                      wrote on last edited by
                      #14

                      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

                      Sander RosselS 1 Reply Last reply
                      0
                      • D David Farrow

                        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

                        Sander RosselS Offline
                        Sander RosselS Offline
                        Sander Rossel
                        wrote on last edited by
                        #15

                        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.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups