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. Other Discussions
  3. The Weird and The Wonderful
  4. Weird, and definitely not wonderful

Weird, and definitely not wonderful

Scheduled Pinned Locked Moved The Weird and The Wonderful
comfunctional
9 Posts 5 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.
  • M Offline
    M Offline
    Marc Clifton
    wrote on last edited by
    #1

    if LEN(@accountnumber) >= 4
    begin
    select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
    end
    else
    begin
    select * from dbo.departments where catid = @catid order by deptname;
    end

    I want to NEVER work with the guy (and I know his name, BTW) who wrote this crap. Marc

    Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

    Sander RosselS L P 4 Replies Last reply
    0
    • M Marc Clifton

      if LEN(@accountnumber) >= 4
      begin
      select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
      end
      else
      begin
      select * from dbo.departments where catid = @catid order by deptname;
      end

      I want to NEVER work with the guy (and I know his name, BTW) who wrote this crap. Marc

      Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

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

      select *
      from dbo.Departments
      where (accountnumber = @accountnumber and LEN(@accountnumber) >= 4)
      or catid = @catid
      order by deptname

      Better? :D I'm wondering what the problem is exactly... Except for the weird IF statement, use of * in production code and horrible formatting.

      My blog[^]

      public class SanderRossel : Lazy<Person>
      {
      public void DoWork()
      {
      throw new NotSupportedException();
      }
      }

      M 1 Reply Last reply
      0
      • M Marc Clifton

        if LEN(@accountnumber) >= 4
        begin
        select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
        end
        else
        begin
        select * from dbo.departments where catid = @catid order by deptname;
        end

        I want to NEVER work with the guy (and I know his name, BTW) who wrote this crap. Marc

        Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

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

        don't open it[^] Hopefully not your new client/employer? :laugh: - Sebastian

        M 1 Reply Last reply
        0
        • Sander RosselS Sander Rossel

          select *
          from dbo.Departments
          where (accountnumber = @accountnumber and LEN(@accountnumber) >= 4)
          or catid = @catid
          order by deptname

          Better? :D I'm wondering what the problem is exactly... Except for the weird IF statement, use of * in production code and horrible formatting.

          My blog[^]

          public class SanderRossel : Lazy<Person>
          {
          public void DoWork()
          {
          throw new NotSupportedException();
          }
          }

          M Offline
          M Offline
          Marc Clifton
          wrote on last edited by
          #4

          Sander Rossel wrote:

          I'm wondering what the problem is exactly...

          The problem is that he's looking up the department by either account number or category ID. This is just bad practice and made worse by the fact (which I forgot to mention) that the PROC's name is "GetClientDepartments" which implies the list is being filtered by clients, not my accounts or categories. There should be at least two PROC's, one called "GetDepartmentsByAccountNumber" and the other "GetDepartmentsByCategory". It's just plain laziness that he put the functionality for two very different things into one proc. Now get this -- on the client side, he's always populating the department and catid values! Marc

          Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

          Sander RosselS 1 Reply Last reply
          0
          • L Lost User

            don't open it[^] Hopefully not your new client/employer? :laugh: - Sebastian

            M Offline
            M Offline
            Marc Clifton
            wrote on last edited by
            #5

            manchanx wrote:

            Hopefully not your new client/employer?

            No, thankfully. Haven't heard from them yet, this is code I'm cleaning up before (hopefully) I come on board with the other client. Marc

            Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

            1 Reply Last reply
            0
            • M Marc Clifton

              Sander Rossel wrote:

              I'm wondering what the problem is exactly...

              The problem is that he's looking up the department by either account number or category ID. This is just bad practice and made worse by the fact (which I forgot to mention) that the PROC's name is "GetClientDepartments" which implies the list is being filtered by clients, not my accounts or categories. There should be at least two PROC's, one called "GetDepartmentsByAccountNumber" and the other "GetDepartmentsByCategory". It's just plain laziness that he put the functionality for two very different things into one proc. Now get this -- on the client side, he's always populating the department and catid values! Marc

              Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

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

              Well in that case it's A BIT weird. I've worked with such procedures a lot. They usually were part of some bigger procedure. This, for example, could be part of getting a product, but depending on the department and available data and some customer setting and lots of other stuff you would ultimately get 1 out of maybe 10 possible outcomes. Anyway, if you worked where I worked (notice past tense!) this wouldn't bother you as much as it does :laugh:

              My blog[^]

              public class SanderRossel : Lazy<Person>
              {
              public void DoWork()
              {
              throw new NotSupportedException();
              }
              }

              1 Reply Last reply
              0
              • M Marc Clifton

                if LEN(@accountnumber) >= 4
                begin
                select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
                end
                else
                begin
                select * from dbo.departments where catid = @catid order by deptname;
                end

                I want to NEVER work with the guy (and I know his name, BTW) who wrote this crap. Marc

                Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

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

                Yeah, needs a CASE. :rolleyes: IF has no place in SQL. He must not have been trained in set-based thinking.

                1 Reply Last reply
                0
                • M Marc Clifton

                  if LEN(@accountnumber) >= 4
                  begin
                  select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
                  end
                  else
                  begin
                  select * from dbo.departments where catid = @catid order by deptname;
                  end

                  I want to NEVER work with the guy (and I know his name, BTW) who wrote this crap. Marc

                  Imperative to Functional Programming Succinctly Contributors Wanted for Higher Order Programming Project!

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

                  I'd like to propose a simple extra switch to test Sanders code, while still be able to switch back to the old one.

                  if (select COUNT(*) from tblSettings where 'test' = 'true') = 0
                  begin
                  if LEN(@accountnumber) >= 4
                  begin
                  select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
                  end
                  else
                  begin
                  select * from dbo.departments where catid = @catid order by deptname;
                  end
                  end
                  else
                  begin
                  select *
                  from dbo.Departments
                  where (accountnumber = @accountnumber and LEN(@accountnumber) >= 4)
                  or catid = @catid
                  order by deptname
                  end;

                  You should replace the "4" with a value that you get from a settings-table ofcourse. You don't want to recompile your sproc simply because the length of catids' change. Better yet, replace it with the MIN(LEN(accountnumber)).

                  Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                  R 1 Reply Last reply
                  0
                  • L Lost User

                    I'd like to propose a simple extra switch to test Sanders code, while still be able to switch back to the old one.

                    if (select COUNT(*) from tblSettings where 'test' = 'true') = 0
                    begin
                    if LEN(@accountnumber) >= 4
                    begin
                    select * from dbo.Departments where accountnumber = @accountnumber order by deptname;
                    end
                    else
                    begin
                    select * from dbo.departments where catid = @catid order by deptname;
                    end
                    end
                    else
                    begin
                    select *
                    from dbo.Departments
                    where (accountnumber = @accountnumber and LEN(@accountnumber) >= 4)
                    or catid = @catid
                    order by deptname
                    end;

                    You should replace the "4" with a value that you get from a settings-table ofcourse. You don't want to recompile your sproc simply because the length of catids' change. Better yet, replace it with the MIN(LEN(accountnumber)).

                    Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

                    R Offline
                    R Offline
                    robocodeboy
                    wrote on last edited by
                    #9

                    Well, having one stored procedure for each possible search parameter would be a REAL horror, IMHO. You can get to the hundreds of procedures really fast. I would likely have more than one parameter in the sproc, and default values, so I could write:

                    EXEC GetClientDepartments @accountNumber = 'xxxxxxxx';
                    -- or...
                    EXEC GetClientDepartments @catId = 'xxxx';

                    t-SQL code can get really, really messy if you don't pay attention. But, other than that, the code is not really an horror (t-SQL code quality is a lot lower than what you're expecting). My 2c.

                    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