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. Its always good to check for nothing.....

Its always good to check for nothing.....

Scheduled Pinned Locked Moved The Weird and The Wonderful
26 Posts 17 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.
  • T Thanks7872

    foreach (var thisObjective in objectivesLst)
    {
    if (thisObjective.EndDate != null)
    {

    }
    else
    {

    }
    }

    And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

    "When you don't know what you're doing it's best to do it quickly"- SoMad

    D Offline
    D Offline
    dan sh
    wrote on last edited by
    #10

    It took me a while to realize that you haven't removed any code in if/else block.

    My CP workspace: Incredibly trivial and probably useless code samples[^]

    1 Reply Last reply
    0
    • T Thanks7872

      foreach (var thisObjective in objectivesLst)
      {
      if (thisObjective.EndDate != null)
      {

      }
      else
      {

      }
      }

      And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

      "When you don't know what you're doing it's best to do it quickly"- SoMad

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #11

      And as soon as you remove the obviously-meaningless loop, you'll learn two things: 1) objectivesLst is actually an IEnumerable that, when enumerated, writes pieces of its contents to ten different parts of the application for later use. Suddenly, you have NullReferenceExceptions coming out of your ears... 2) Accessing the EndDate property sets a global variable that's later used to prevent the application from reformatting your hard drive. Unfortunately, you'll learn these too late to save yourself, and will instead begin an epic journey to find whoever wrote the code and beat him to a pulp :)

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      Z P 2 Replies Last reply
      0
      • I Ian Shlasko

        And as soon as you remove the obviously-meaningless loop, you'll learn two things: 1) objectivesLst is actually an IEnumerable that, when enumerated, writes pieces of its contents to ten different parts of the application for later use. Suddenly, you have NullReferenceExceptions coming out of your ears... 2) Accessing the EndDate property sets a global variable that's later used to prevent the application from reformatting your hard drive. Unfortunately, you'll learn these too late to save yourself, and will instead begin an epic journey to find whoever wrote the code and beat him to a pulp :)

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        Z Offline
        Z Offline
        ZurdoDev
        wrote on last edited by
        #12

        Funny, but possibly true. I've had similar things happen. :-D

        There are only 10 types of people in the world, those who understand binary and those who don't.

        1 Reply Last reply
        0
        • I Ian Shlasko

          And as soon as you remove the obviously-meaningless loop, you'll learn two things: 1) objectivesLst is actually an IEnumerable that, when enumerated, writes pieces of its contents to ten different parts of the application for later use. Suddenly, you have NullReferenceExceptions coming out of your ears... 2) Accessing the EndDate property sets a global variable that's later used to prevent the application from reformatting your hard drive. Unfortunately, you'll learn these too late to save yourself, and will instead begin an epic journey to find whoever wrote the code and beat him to a pulp :)

          Proud to have finally moved to the A-Ark. Which one are you in?
          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

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

          Alas, that reminds me of some code I work with now wherein a User object has a Save method to persist it to the database -- with the result that when a user "logs in" to the app the last login date is set on the object and then we call Save. OK. Buuuut the Save also goes out and hits Active Directory to see whether or not other things have changed. :sigh: So every once in a while, we have to iterate all the User records from the database, make them User objects, and call Save. :doh: Add to that that it used to work just fine and took a few minutes to process 4000 or so Users. But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour. :mad:

          You'll never get very far if all you do is follow instructions.

          I 1 Reply Last reply
          0
          • P PIEBALDconsult

            Alas, that reminds me of some code I work with now wherein a User object has a Save method to persist it to the database -- with the result that when a user "logs in" to the app the last login date is set on the object and then we call Save. OK. Buuuut the Save also goes out and hits Active Directory to see whether or not other things have changed. :sigh: So every once in a while, we have to iterate all the User records from the database, make them User objects, and call Save. :doh: Add to that that it used to work just fine and took a few minutes to process 4000 or so Users. But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour. :mad:

            You'll never get very far if all you do is follow instructions.

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #14

            PIEBALDconsult wrote:

            But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour.

            1. Reflect once to get the property list 2) Use LINQ Expressions to compile mutators in memory 3) ??? 4) Profit!

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            P L B 3 Replies Last reply
            0
            • I Ian Shlasko

              PIEBALDconsult wrote:

              But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour.

              1. Reflect once to get the property list 2) Use LINQ Expressions to compile mutators in memory 3) ??? 4) Profit!

              Proud to have finally moved to the A-Ark. Which one are you in?
              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #15
              1. Yes, if I had known what he was doing I would have suggested it, but I didn't know until after he left. He probbaly wouldn't have taken my advice though because "it works". It's that he removed good code and replaced it with bad code that really gets me.

              You'll never get very far if all you do is follow instructions.

              1 Reply Last reply
              0
              • I Ian Shlasko

                PIEBALDconsult wrote:

                But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour.

                1. Reflect once to get the property list 2) Use LINQ Expressions to compile mutators in memory 3) ??? 4) Profit!

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

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

                Had you started your list correctly, at index 0, you'd have seen that it'd be three steps.

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

                P 1 Reply Last reply
                0
                • T Thanks7872

                  foreach (var thisObjective in objectivesLst)
                  {
                  if (thisObjective.EndDate != null)
                  {

                  }
                  else
                  {

                  }
                  }

                  And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

                  "When you don't know what you're doing it's best to do it quickly"- SoMad

                  D Offline
                  D Offline
                  Deflinek
                  wrote on last edited by
                  #17

                  No, no, no. You got it all wrong. The EndDate property does actually important part of work here and the if{..}else{..} is there to prevent that pesky optimizer from removing useful code :D But if by any chance it really is that way, you better start to look for a good gun and a good lawyer...

                  -- "My software never has bugs. It just develops random features."

                  1 Reply Last reply
                  0
                  • L Lost User

                    Had you started your list correctly, at index 0, you'd have seen that it'd be three steps.

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

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

                    Eddy Vluggen wrote:

                    it'd be three steps

                    Gimme three steps. Gimme three steps, mister...

                    You'll never get very far if all you do is follow instructions.

                    1 Reply Last reply
                    0
                    • T Thanks7872

                      foreach (var thisObjective in objectivesLst)
                      {
                      if (thisObjective.EndDate != null)
                      {

                      }
                      else
                      {

                      }
                      }

                      And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

                      "When you don't know what you're doing it's best to do it quickly"- SoMad

                      K Offline
                      K Offline
                      K2DaC2
                      wrote on last edited by
                      #19

                      Sometimes long running loops are solving race conditions ;D If you remove this code, the method is executed faster and suddenly nothing works anymore! (Real world example!)

                      1 Reply Last reply
                      0
                      • T Thanks7872

                        foreach (var thisObjective in objectivesLst)
                        {
                        if (thisObjective.EndDate != null)
                        {

                        }
                        else
                        {

                        }
                        }

                        And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

                        "When you don't know what you're doing it's best to do it quickly"- SoMad

                        B Offline
                        B Offline
                        Bernhard Hiller
                        wrote on last edited by
                        #20

                        Did the guy face some problems with lazy loading? The optimizer might remove an empty foreach, hence he accesses a property of every item. Why not create a ForceLoad function in the list, and call it? - Well, obfuscating the intent of some code is such a great idea.

                        1 Reply Last reply
                        0
                        • I Ian Shlasko

                          PIEBALDconsult wrote:

                          But then the main developer decided to become clever and use Reflection to populate the User objects, and now it takes a half hour.

                          1. Reflect once to get the property list 2) Use LINQ Expressions to compile mutators in memory 3) ??? 4) Profit!

                          Proud to have finally moved to the A-Ark. Which one are you in?
                          Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #21

                          Don't even have to be that clever, just caching the result of reflection calls in a Dictionary<string, PropertyInfo> takes away all the slowness. I've had to do that a few times over the years. Linq Expressions lets you write arguably more elegant code to do it, but might be a bit hard to get an incompetent developer to understand ;)

                          I 1 Reply Last reply
                          0
                          • B BobJanova

                            Don't even have to be that clever, just caching the result of reflection calls in a Dictionary<string, PropertyInfo> takes away all the slowness. I've had to do that a few times over the years. Linq Expressions lets you write arguably more elegant code to do it, but might be a bit hard to get an incompetent developer to understand ;)

                            I Offline
                            I Offline
                            Ian Shlasko
                            wrote on last edited by
                            #22

                            That makes it faster, but you're still adding boxing/unboxing, since PropertyInfo.Invoke() isn't strongly-typed. Not significant in small doses, but call it a few thousand times with primitive types and you'll notice a difference. My current implementation feeds the columns in a custom-made data grid, and that can seriously add up.

                            Proud to have finally moved to the A-Ark. Which one are you in?
                            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                            B 1 Reply Last reply
                            0
                            • I Ian Shlasko

                              That makes it faster, but you're still adding boxing/unboxing, since PropertyInfo.Invoke() isn't strongly-typed. Not significant in small doses, but call it a few thousand times with primitive types and you'll notice a difference. My current implementation feeds the columns in a custom-made data grid, and that can seriously add up.

                              Proud to have finally moved to the A-Ark. Which one are you in?
                              Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #23

                              That's true but I've done something similar (model-view binding into a data grid where the columns are based on properties of the data objects) and it was easily fast enough once I cached the PropertyInfos. I'm not saying you are wrong (you're not, your solution is better), just that you don't need to go to that level to get most of the benefit.

                              1 Reply Last reply
                              0
                              • T Thanks7872

                                foreach (var thisObjective in objectivesLst)
                                {
                                if (thisObjective.EndDate != null)
                                {

                                }
                                else
                                {

                                }
                                }

                                And this beauty lies inside one of the wonderful projects i came across. I have not removed any code from the above snippet. This is part of the code which i found in completed project. This developer should really be awarded... :laugh:

                                "When you don't know what you're doing it's best to do it quickly"- SoMad

                                T Offline
                                T Offline
                                TheGreatAndPowerfulOz
                                wrote on last edited by
                                #24

                                It's invisible NSA code

                                If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
                                You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
                                Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

                                1 Reply Last reply
                                0
                                • N newton saber

                                  FUD* rules! You don't want to take it out, because it MAY do something. What if in it's nothingness it does a something? Hahaha - :D Besides, who cares? No one pays me to remove code. Do they? :D I'm trolling for people who think I'm serious. Are you one of them? *FUD - Fear, Uncertainty, Doubt

                                  S Offline
                                  S Offline
                                  Stefan_Lang
                                  wrote on last edited by
                                  #25

                                  Actually it does something: it reads Enddate, which may cause an access violation if it was called on an invalid object. I'm not sure what language that is from, so maybe it isn't possible to have an invalid object in the list, but then the entire list could be invalid, no?

                                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    Indeed, get rid of the var and replace it with the actual type.

                                    You'll never get very far if all you do is follow instructions.

                                    B Offline
                                    B Offline
                                    Brady Kelly
                                    wrote on last edited by
                                    #26

                                    People were paid to make my compiler do that. And it does.

                                    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