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. The Lounge
  3. Where does it end?

Where does it end?

Scheduled Pinned Locked Moved The Lounge
cssalgorithmsquestioncsharpvisual-studio
44 Posts 12 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.
  • Sander RosselS Sander Rossel

    *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

    SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

    Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

    Regards, Sander

    K Offline
    K Offline
    kmoorevs
    wrote on last edited by
    #8

    I say kudos for cleverness! Your solution is hard to read/understand (at least for me) but I like it since it makes me break it apart. If I still don't understand it, I'd have to run it in a debugger. Just leave decent documentation for the next guy. :)

    "Go forth into the source" - Neal Morse

    Sander RosselS 1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      I'm sorting the data that is then displayed by the grid. The DataGridView works pretty well when you have a DataView, but all I'm having is a List<T> which I haven't been able to sort in the past if my life depended on it. So it's easier to just take your original list, sort it (using OrderBy and OrderByDescending) and bind to the result. The same goes for filtering with Where.

      PIEBALDconsult wrote:

      Does your solution allow multi-level sorting?

      Nope, should be a new value in the drop down, like "name then title".

      PIEBALDconsult wrote:

      I'm unclear on the details of what you have, but I would consider having each Item in the DropDown know what it is supposed to do -- perhaps a class with a Name and a Delegate -- so all you need do is add a new Item to the DropDown.

      That's exactly what I have :laugh: The code I posted above could be called as follows:

      comboBoxOrdering.Items.Add(SomeFunction("Name", Queryable.OrderBy, q => q.Name));

      I need all the generics so my IntelliSense (and compiler) knows that q is actually some type that has a Name property :) And I'm having something similar for the filter functionality (which is also a dropdown).

      comboBoxFilters.Items.Add(SomeFunction("Active only", Queryable.Where, q => q.Active));

      So even if you couldn't read the code above, I'm pretty sure you could use it ;)

      Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

      Regards, Sander

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

      Sander Rossel wrote:

      That's exactly what I have

      Oh. Well, alright then.

      Sander RosselS 1 Reply Last reply
      0
      • Sander RosselS Sander Rossel

        Eddy Vluggen wrote:

        I'd be using inheritance to do so. Requires a complete class if you want another switch option.

        Actually, what you're looking at is the constructor of that function :laugh:

        Eddy Vluggen wrote:

        I'd be doing the same for the columns; write a single column and inherit from there, and simply call "sort" on the column

        Have you ever worked with the WinForms DataGridView? It's a beast and if you're going to inherit anything from it you're in for a rough ride :sigh:

        Eddy Vluggen wrote:

        I would recommend to create a special class (yes, another) to keep all the arguments you're passing to the method.

        It already is, but what's the difference between having a function that takes these arguments or a constructor that takes them? Or did you want to pass them to the class someway else? Fun fact, in my code you're mostly looking at the signature of common LINQ functions like OrderBy and Where ;)

        Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

        Regards, Sander

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #10

        Sander Rossel wrote:

        Actually, what you're looking at is the constructor of that function :laugh:

        The fact that you have to explain that is an indication of the readability.

        Sander Rossel wrote:

        Have you ever worked with the WinForms DataGridView?

        Yes, and I love it. Cannot say the same for the XtraGrid or the MSFlexGrid. Which would you recommend, if not the DataGridView?

        Sander Rossel wrote:

        It already is, but what's the difference between having a function that takes these arguments or a constructor that takes them?

        It would work the same for a constructor as well as a method; it is a simply way of reducing the amount of arguments.

        public static Process Start(
        ProcessStartInfo startInfo
        )

        That's a factory-method, with a single argument. I think that is a bit more readable than having all the options listed. There's another advantage in this idea if you're planning to inherit the Process-class; your inherited class could easily extend the parameter list without changing the method-signature.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

        Sander RosselS 1 Reply Last reply
        0
        • L Lost User

          Sander Rossel wrote:

          Actually, what you're looking at is the constructor of that function :laugh:

          The fact that you have to explain that is an indication of the readability.

          Sander Rossel wrote:

          Have you ever worked with the WinForms DataGridView?

          Yes, and I love it. Cannot say the same for the XtraGrid or the MSFlexGrid. Which would you recommend, if not the DataGridView?

          Sander Rossel wrote:

          It already is, but what's the difference between having a function that takes these arguments or a constructor that takes them?

          It would work the same for a constructor as well as a method; it is a simply way of reducing the amount of arguments.

          public static Process Start(
          ProcessStartInfo startInfo
          )

          That's a factory-method, with a single argument. I think that is a bit more readable than having all the options listed. There's another advantage in this idea if you're planning to inherit the Process-class; your inherited class could easily extend the parameter list without changing the method-signature.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

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

          Eddy Vluggen wrote:

          The fact that you have to explain that is an indication of the readability.

          Actually, I shouldn't have called it SomeFunction :)

          Eddy Vluggen wrote:

          Which would you recommend, if not the DataGridView?

          Definitely XtraGrid, all sorting and filtering capabilities you need right out of the box!. Sure, steep learning curve, but it's worth it :)

          Eddy Vluggen wrote:

          That's a factory-method, with a single argument.

          But somewhere there's a class constructor for ProcessStartInfo that has all those parameters, right? ;)

          Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

          Regards, Sander

          L 1 Reply Last reply
          0
          • P PIEBALDconsult

            Sander Rossel wrote:

            That's exactly what I have

            Oh. Well, alright then.

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

            A shame you didn't recognize it. It's an indication that my code is, indeed, 'clever'... Not sure how to make it more readable though (I guess this is the part where it becomes a programming question :laugh: ).

            Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

            Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

            Regards, Sander

            R 1 Reply Last reply
            0
            • K kmoorevs

              I say kudos for cleverness! Your solution is hard to read/understand (at least for me) but I like it since it makes me break it apart. If I still don't understand it, I'd have to run it in a debugger. Just leave decent documentation for the next guy. :)

              "Go forth into the source" - Neal Morse

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

              kmoorevs wrote:

              Just leave decent documentation for the next guy

              If I told you you could call it like this, would you know enough? :)

              SomeFunction("Name", Queryable.OrderBy, q => q.Name);

              Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

              Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

              Regards, Sander

              P 1 Reply Last reply
              0
              • Sander RosselS Sander Rossel

                Sascha Lefévre wrote:

                Can you elaborate?

                Sure. Consider this :)

                // Everything typed with IntelliSense support.
                MyClass result = SomeFunction("Name", Queryable.OrderBy, q => q.Name); // string
                comboBox.Items.Add(result);
                comboBox.Items.Add(SomeFunction("Date", Queryable.OrderBy, q => q.Date)); // DateTime
                comboBox.Items.Add(SomeFunction("Age", Queryable.OrderBy, q => q.Age)); // int

                // later...
                object ordering = comboBox.SelectedItem;
                // How to cast this?
                MyClass result = (MyClass)ordering;

                // This works fine though!
                dynamic ordering = comboBox.SelectedItem;
                // Pseudo-code.
                ordering.LinqFunction(ordering.Predicate);

                Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                Regards, Sander

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #14

                Makes sense. But is there a specific reason why you didn't use a signature like this:

                MyClass ordering = SomeFunction(string caption, Func, IQueryable> applyPredicate) { ... }

                And then use it like this:

                comboBox.Items.Add(SomeFunction("Date", source => source.OrderBy(q => q.Date)));

                MyClass ordering = (MyClass)comboBox.SelectedItem;

                // Pseudo-code:
                ordering.ApplyPredicate();

                Sander RosselS 2 Replies Last reply
                0
                • S Sascha Lefevre

                  Makes sense. But is there a specific reason why you didn't use a signature like this:

                  MyClass ordering = SomeFunction(string caption, Func, IQueryable> applyPredicate) { ... }

                  And then use it like this:

                  comboBox.Items.Add(SomeFunction("Date", source => source.OrderBy(q => q.Date)));

                  MyClass ordering = (MyClass)comboBox.SelectedItem;

                  // Pseudo-code:
                  ordering.ApplyPredicate();

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

                  Yep, I need the q => q.Date part seperately :) Although you do make a good point. I'll see if I can change the code tomorrow so it would work as you propose. It would certainly make that easier.

                  Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                  Regards, Sander

                  1 Reply Last reply
                  0
                  • Sander RosselS Sander Rossel

                    Eddy Vluggen wrote:

                    The fact that you have to explain that is an indication of the readability.

                    Actually, I shouldn't have called it SomeFunction :)

                    Eddy Vluggen wrote:

                    Which would you recommend, if not the DataGridView?

                    Definitely XtraGrid, all sorting and filtering capabilities you need right out of the box!. Sure, steep learning curve, but it's worth it :)

                    Eddy Vluggen wrote:

                    That's a factory-method, with a single argument.

                    But somewhere there's a class constructor for ProcessStartInfo that has all those parameters, right? ;)

                    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                    Regards, Sander

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #16

                    Sander Rossel wrote:

                    Actually, I shouldn't have called it SomeFunction :)

                    I admit I have used the prefix "some" in production code.

                    Sander Rossel wrote:

                    Definitely XtraGrid, all sorting and filtering capabilities you need right out of the box!. Sure, steep learning curve, but it's worth it :)

                    I keep hearing that, yet personally I keep preferring speed and simplicity.

                    Sander Rossel wrote:

                    But somewhere there's a class constructor for ProcessStartInfo that has all those parameters, right? ;)

                    Check for yourself[^]. You'd probably not need them ALL at once, so it would make no sense to provide an overload that lists them all as real arguments. I also do not see any benefit in the extra overload, only a higher LOC - you'd loose aforementioned benefits that bundlig the arguments has. If they are part of the method-signature, then it will be harder to log (instead of simply serializing the arguments-object), and the signature would change if a parameter needs to be added.

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

                    Sander RosselS 1 Reply Last reply
                    0
                    • L Lost User

                      Sander Rossel wrote:

                      Actually, I shouldn't have called it SomeFunction :)

                      I admit I have used the prefix "some" in production code.

                      Sander Rossel wrote:

                      Definitely XtraGrid, all sorting and filtering capabilities you need right out of the box!. Sure, steep learning curve, but it's worth it :)

                      I keep hearing that, yet personally I keep preferring speed and simplicity.

                      Sander Rossel wrote:

                      But somewhere there's a class constructor for ProcessStartInfo that has all those parameters, right? ;)

                      Check for yourself[^]. You'd probably not need them ALL at once, so it would make no sense to provide an overload that lists them all as real arguments. I also do not see any benefit in the extra overload, only a higher LOC - you'd loose aforementioned benefits that bundlig the arguments has. If they are part of the method-signature, then it will be harder to log (instead of simply serializing the arguments-object), and the signature would change if a parameter needs to be added.

                      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

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

                      Eddy Vluggen wrote:

                      You'd probably not need them ALL at once

                      True for the ProcessStartInfo, but my method only has three and I always need all of them. I can see how such a class would make sense though (I probably don't use it far enough).

                      Eddy Vluggen wrote:

                      I admit I have used the prefix "some" in production code.

                      I'll pretend I didn't see that! :laugh:

                      Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                      Regards, Sander

                      1 Reply Last reply
                      0
                      • Sander RosselS Sander Rossel

                        *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

                        SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

                        Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

                        Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                        Regards, Sander

                        M Offline
                        M Offline
                        Mark_Wallace
                        wrote on last edited by
                        #18

                        "Maxthon has found dangerous code on this page. Click OK to stop loading the page, or Ignore if you're an idiot."

                        I wanna be a eunuchs developer! Pass me a bread knife!

                        1 Reply Last reply
                        0
                        • Sander RosselS Sander Rossel

                          *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

                          SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

                          Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

                          Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                          Regards, Sander

                          K Offline
                          K Offline
                          Kirk 10389821
                          wrote on last edited by
                          #19

                          Descending Given that I am not familiar with WHY your UI is the way it is, I am not qualified to recreate it. But I do prefer naturally adaptive code (reading the columns from the dataview). No need to remember anything...

                          1 Reply Last reply
                          0
                          • Sander RosselS Sander Rossel

                            *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

                            SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

                            Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

                            Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                            Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                            Regards, Sander

                            B Offline
                            B Offline
                            BillWoodruff
                            wrote on last edited by
                            #20

                            Looking at your code my subconscious started hearing the song "Simply Irresistible," starting at the verse: "methods are inscrutable" :) cheers, Bill

                            «To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

                            1 Reply Last reply
                            0
                            • Sander RosselS Sander Rossel

                              *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

                              SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

                              Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

                              Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                              Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                              Regards, Sander

                              S Offline
                              S Offline
                              scmtim
                              wrote on last edited by
                              #21

                              Seems like you are trying to rewrite Dynamic Linq which has been around since 2008. Just pass a field name and do a % 2 to figure out if you call OrderBy or OrderByDescending. http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

                              Sander RosselS 1 Reply Last reply
                              0
                              • S scmtim

                                Seems like you are trying to rewrite Dynamic Linq which has been around since 2008. Just pass a field name and do a % 2 to figure out if you call OrderBy or OrderByDescending. http://weblogs.asp.net/scottgu/dynamic-linq-part-1-using-the-linq-dynamic-query-library

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

                                How's that the same? My solution is type safe and has design time error checking ;) I simplified it a bit by the way.

                                Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                Regards, Sander

                                S 1 Reply Last reply
                                0
                                • Sander RosselS Sander Rossel

                                  How's that the same? My solution is type safe and has design time error checking ;) I simplified it a bit by the way.

                                  Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                  Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                  Regards, Sander

                                  S Offline
                                  S Offline
                                  scmtim
                                  wrote on last edited by
                                  #23

                                  It is the same in that it accomplishes the desired functionality in the app. You were asking where to draw the line on readability and simplity. If you give a junior developer an API with .OrderBy(string) and then the one you posted, which will they understand immediately?

                                  Sander RosselS 1 Reply Last reply
                                  0
                                  • Sander RosselS Sander Rossel

                                    *** DISCLAIMER *** This isn't a programming question, because I've solved the programming problem myself. Rather, I'm interested in your opinion regarding the solution, clever or 'clever'? So today I had a nice little challenge. My application has a grid which can be sorted on various columns, asc and desc. Now the sorting isn't applied by clicking the column header, but by selecting a value from a drop down (Name, Name (descending), Title, Title (descending) etc.). I was thinking I could just have a switch statement where I check which ordering the user chose (which would make me extend the switch for each new ordering), or... I could abstract this away. Of course I went for the second option. Now to make this work (and it works nicely, adding a new ordering is a piece of cake) I had to write the following function:

                                    SomeFunction(string caption, Func, Expression>, IQueryable> linqFunction, Expression> expression) { ... }

                                    Since this is a WinForms application though, and WinForms and generics don't go well together I'm more or less forced to use a dynamic later on as it's quite impossible to get the values of TElement and TKey from object. I'm thinking this isn't ideal in terms of complexity and readability, but it is pretty neat because adding a new ordering is really simple (one line of simple code) and guaranteed to work on the first try (you'll never forget to change that switch statement again). And the solution is even re-usable for the filter functionality I need as well (which, of course, becomes a breeze as well)! So judging from that, clever or 'clever'? And where do you draw the line for cleverness vs. readability/simplicity?

                                    Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                    Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                    Regards, Sander

                                    _ Offline
                                    _ Offline
                                    _WinBase_
                                    wrote on last edited by
                                    #24

                                    I wish it ended sooner

                                    Sander RosselS 1 Reply Last reply
                                    0
                                    • S scmtim

                                      It is the same in that it accomplishes the desired functionality in the app. You were asking where to draw the line on readability and simplity. If you give a junior developer an API with .OrderBy(string) and then the one you posted, which will they understand immediately?

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

                                      Put like that you make a good point. The question is are you willing to trade type safety for a simple API? OrderBy(string) may be a simple function, but there's no way in telling what string values are valid and when their validity expires.

                                      Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                      Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                      Regards, Sander

                                      1 Reply Last reply
                                      0
                                      • _ _WinBase_

                                        I wish it ended sooner

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

                                        :thumbsup: :laugh: Me too, me too... Already changed the code :)

                                        Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                        Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                        Regards, Sander

                                        1 Reply Last reply
                                        0
                                        • S Sascha Lefevre

                                          Makes sense. But is there a specific reason why you didn't use a signature like this:

                                          MyClass ordering = SomeFunction(string caption, Func, IQueryable> applyPredicate) { ... }

                                          And then use it like this:

                                          comboBox.Items.Add(SomeFunction("Date", source => source.OrderBy(q => q.Date)));

                                          MyClass ordering = (MyClass)comboBox.SelectedItem;

                                          // Pseudo-code:
                                          ordering.ApplyPredicate();

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

                                          Another part of my app got slightly more complex, but I was able to implement this solution and overall reduce complexity (I hope) :thumbsup:

                                          Visit my blog at Sander's bits - Writing the code you need. Or read my articles at my CodeProject profile.

                                          Simplicity is prerequisite for reliability. — Edsger W. Dijkstra

                                          Regards, Sander

                                          S 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