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. Code Puzzle

Code Puzzle

Scheduled Pinned Locked Moved The Weird and The Wonderful
questiondatabasedocker
28 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.
  • S Sascha Lefevre

    How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

    public class QueryContainer
    {
    private static List<QueryContainer> Container;
    private static QueryContainer instance;

    public static QueryContainer Instance
    {
        get
        {
            if (Instance == null)
                instance = new QueryContainer();
    
            return instance;
        }
    }
    
    public int \_searchID;
    
    public int ID { get { return \_id; } }
    
    public string Query
    {
        get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
        set { Container.Query = value; \_id =+ 1; }
    }
    
    private int \_id;
    private string \_query;
    
    private QueryContainer()
    { }
    

    }

    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

    A Offline
    A Offline
    Agent__007
    wrote on last edited by
    #6

    Singleton! :doh: - Compiler error at =+ - Threading issues: susceptible to multiple instances being created - Possibly a StackOverflowException while "get"ing the Query - Possibly a NullReferenceException while "get"ing the Query - Possibly a NullReferenceException while "set"ing the Query - Should have a static constructor

    Sascha Lefèvre wrote:

    Can you guess the intended purpose?

    Write a test-friendly class? :-\

    You have just been Sharapova'd.

    Richard DeemingR 1 Reply Last reply
    0
    • S Sascha Lefevre

      How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

      public class QueryContainer
      {
      private static List<QueryContainer> Container;
      private static QueryContainer instance;

      public static QueryContainer Instance
      {
          get
          {
              if (Instance == null)
                  instance = new QueryContainer();
      
              return instance;
          }
      }
      
      public int \_searchID;
      
      public int ID { get { return \_id; } }
      
      public string Query
      {
          get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
          set { Container.Query = value; \_id =+ 1; }
      }
      
      private int \_id;
      private string \_query;
      
      private QueryContainer()
      { }
      

      }

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      S Offline
      S Offline
      Smart K8
      wrote on last edited by
      #7

      Singleton is not thread-safe, Query property is potentially recursive, and can cause null exception. Also shouldn't query container contain list of Query instead of list of QueryContainer if QueryContainer is supposed to be a singleton (i.e. having only one). Also the whole class is crazy. Why have ID counter if you're using kinda "singleton"? Also no comments on what the class does or on public members. This IS Sparta! :-D

      1 Reply Last reply
      0
      • A Agent__007

        Singleton! :doh: - Compiler error at =+ - Threading issues: susceptible to multiple instances being created - Possibly a StackOverflowException while "get"ing the Query - Possibly a NullReferenceException while "get"ing the Query - Possibly a NullReferenceException while "set"ing the Query - Should have a static constructor

        Sascha Lefèvre wrote:

        Can you guess the intended purpose?

        Write a test-friendly class? :-\

        You have just been Sharapova'd.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #8

        Agent__007 wrote:

        Compiler error at =+

        Really? That part of the code compiles for me. It doesn't do what the author probably intended, but it compiles.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        A 1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Agent__007 wrote:

          Compiler error at =+

          Really? That part of the code compiles for me. It doesn't do what the author probably intended, but it compiles.


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          A Offline
          A Offline
          Agent__007
          wrote on last edited by
          #9

          It does, indeed! :thumbsup: I was looking at the code and that looked like a mistyped += and thought it would give an error. There are times when I don't think at all! :doh: :sigh:

          You have just been Sharapova'd.

          1 Reply Last reply
          0
          • S Sascha Lefevre

            How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

            public class QueryContainer
            {
            private static List<QueryContainer> Container;
            private static QueryContainer instance;

            public static QueryContainer Instance
            {
                get
                {
                    if (Instance == null)
                        instance = new QueryContainer();
            
                    return instance;
                }
            }
            
            public int \_searchID;
            
            public int ID { get { return \_id; } }
            
            public string Query
            {
                get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                set { Container.Query = value; \_id =+ 1; }
            }
            
            private int \_id;
            private string \_query;
            
            private QueryContainer()
            { }
            

            }

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            P Offline
            P Offline
            Power Puff Boy
            wrote on last edited by
            #10

            Singleton is usually one of the first design patterns people learn. They immediately fall in love with it and are become blind for any other patterns out there. I'd say the problem with this code would be that it causes brain damage.

            1 Reply Last reply
            0
            • S Sascha Lefevre

              How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

              public class QueryContainer
              {
              private static List<QueryContainer> Container;
              private static QueryContainer instance;

              public static QueryContainer Instance
              {
                  get
                  {
                      if (Instance == null)
                          instance = new QueryContainer();
              
                      return instance;
                  }
              }
              
              public int \_searchID;
              
              public int ID { get { return \_id; } }
              
              public string Query
              {
                  get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                  set { Container.Query = value; \_id =+ 1; }
              }
              
              private int \_id;
              private string \_query;
              
              private QueryContainer()
              { }
              

              }

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              F Offline
              F Offline
              Freak30
              wrote on last edited by
              #11

              I assume the intended purpose was to create a list of recently used queries. Even though the list was never actually filled (or allocated for that matter).

              The good thing about pessimism is, that you are always either right or pleasently surprised.

              1 Reply Last reply
              0
              • S Sascha Lefevre

                How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

                public class QueryContainer
                {
                private static List<QueryContainer> Container;
                private static QueryContainer instance;

                public static QueryContainer Instance
                {
                    get
                    {
                        if (Instance == null)
                            instance = new QueryContainer();
                
                        return instance;
                    }
                }
                
                public int \_searchID;
                
                public int ID { get { return \_id; } }
                
                public string Query
                {
                    get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                    set { Container.Query = value; \_id =+ 1; }
                }
                
                private int \_id;
                private string \_query;
                
                private QueryContainer()
                { }
                

                }

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                Richard DeemingR Offline
                Richard DeemingR Offline
                Richard Deeming
                wrote on last edited by
                #12

                Your warnings obviously weren't dire enough - he's back[^], with a modified version of the same class, which still doesn't fix his original problem. :doh:


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                B S 2 Replies Last reply
                0
                • Richard DeemingR Richard Deeming

                  Your warnings obviously weren't dire enough - he's back[^], with a modified version of the same class, which still doesn't fix his original problem. :doh:


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  B Offline
                  B Offline
                  Brisingr Aerowing
                  wrote on last edited by
                  #13

                  Clue bat needed in Aisle 5! Clue bat needed in Aisle 5!

                  What do you get when you cross a joke with a rhetorical question? The metaphorical solid rear-end expulsions have impacted the metaphorical motorized bladed rotating air movement mechanism. Do questions with multiple question marks annoy you???

                  1 Reply Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Your warnings obviously weren't dire enough - he's back[^], with a modified version of the same class, which still doesn't fix his original problem. :doh:


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                    Yes, I saw it.. sighed, and decided to let someone else answer since he apparently didn't believe me :sigh:

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

                      public class QueryContainer
                      {
                      private static List<QueryContainer> Container;
                      private static QueryContainer instance;

                      public static QueryContainer Instance
                      {
                          get
                          {
                              if (Instance == null)
                                  instance = new QueryContainer();
                      
                              return instance;
                          }
                      }
                      
                      public int \_searchID;
                      
                      public int ID { get { return \_id; } }
                      
                      public string Query
                      {
                          get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                          set { Container.Query = value; \_id =+ 1; }
                      }
                      
                      private int \_id;
                      private string \_query;
                      
                      private QueryContainer()
                      { }
                      

                      }

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

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

                      Among all the toe-curling flaws in there, these are my two favorites: It's not even a singleton - it's a "noneton": Trying to access the Instance-property will cause a stack overflow due to recursion.. The List<QueryContainer> will be really, really short. Even when fixing the above flaw.. And the answer to the million dollar question: It's supposed to somehow (don't ask me) help with fixing code that is vulnerable to SQL-injection.

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      Richard DeemingR F 2 Replies Last reply
                      0
                      • P PIEBALDconsult

                        Threading issues certainly. What language?

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

                        PIEBALDconsult wrote:

                        Threading issues certainly.

                        You're WAY off :laugh: (See my solution-post below)

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        1 Reply Last reply
                        0
                        • S Sascha Lefevre

                          Among all the toe-curling flaws in there, these are my two favorites: It's not even a singleton - it's a "noneton": Trying to access the Instance-property will cause a stack overflow due to recursion.. The List<QueryContainer> will be really, really short. Even when fixing the above flaw.. And the answer to the million dollar question: It's supposed to somehow (don't ask me) help with fixing code that is vulnerable to SQL-injection.

                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                          Richard DeemingR Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #17

                          The new version[^] still doesn't fix the problem. I'm running out of different ways to explain to him why his approach isn't going to work. :sigh:


                          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                          S OriginalGriffO 2 Replies Last reply
                          0
                          • Richard DeemingR Richard Deeming

                            The new version[^] still doesn't fix the problem. I'm running out of different ways to explain to him why his approach isn't going to work. :sigh:


                            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

                            He might be impervious to advice..

                            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                            1 Reply Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              The new version[^] still doesn't fix the problem. I'm running out of different ways to explain to him why his approach isn't going to work. :sigh:


                              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                              OriginalGriffO Offline
                              OriginalGriffO Offline
                              OriginalGriff
                              wrote on last edited by
                              #19

                              I know one remarkably effective way to explain to him: use SQL Injection to delete his database. And every time he puts it back, it goes again. It's cruel. It's nasty. It's probably illegal. But it's what his best mate will do, just to see the look on his face...

                              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                              1 Reply Last reply
                              0
                              • S Sascha Lefevre

                                Among all the toe-curling flaws in there, these are my two favorites: It's not even a singleton - it's a "noneton": Trying to access the Instance-property will cause a stack overflow due to recursion.. The List<QueryContainer> will be really, really short. Even when fixing the above flaw.. And the answer to the million dollar question: It's supposed to somehow (don't ask me) help with fixing code that is vulnerable to SQL-injection.

                                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                F Offline
                                F Offline
                                Freak30
                                wrote on last edited by
                                #20

                                Sascha Lefèvre wrote:

                                It's supposed to somehow (don't ask me) help with fixing code that is vulnerable to SQL-injection.

                                After reading the actual questions, I don't think he is trying to fix the actual vulnerability with his code. Instead he may be trying to cheat the test system (by obfuscation) from detecting that there is an SQL injection vulnerability.

                                The good thing about pessimism is, that you are always either right or pleasently surprised.

                                1 Reply Last reply
                                0
                                • S Sascha Lefevre

                                  How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

                                  public class QueryContainer
                                  {
                                  private static List<QueryContainer> Container;
                                  private static QueryContainer instance;

                                  public static QueryContainer Instance
                                  {
                                      get
                                      {
                                          if (Instance == null)
                                              instance = new QueryContainer();
                                  
                                          return instance;
                                      }
                                  }
                                  
                                  public int \_searchID;
                                  
                                  public int ID { get { return \_id; } }
                                  
                                  public string Query
                                  {
                                      get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                                      set { Container.Query = value; \_id =+ 1; }
                                  }
                                  
                                  private int \_id;
                                  private string \_query;
                                  
                                  private QueryContainer()
                                  { }
                                  

                                  }

                                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                  P Offline
                                  P Offline
                                  Power Puff Boy
                                  wrote on last edited by
                                  #21

                                  "But please - no programming questions."

                                  S 1 Reply Last reply
                                  0
                                  • P Power Puff Boy

                                    "But please - no programming questions."

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

                                    As you didn't mark your post as a joke or put a smiley into it I have to assume you're being serious: But my post isn't a programming question.

                                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                    P 1 Reply Last reply
                                    0
                                    • S Sascha Lefevre

                                      How many compiler errors, potential NullReferenceExceptions, StackOverflowExceptions and whatnot can you spot in this presumably review-ready class? And the million dollar question: Can you guess the intended purpose?

                                      public class QueryContainer
                                      {
                                      private static List<QueryContainer> Container;
                                      private static QueryContainer instance;

                                      public static QueryContainer Instance
                                      {
                                          get
                                          {
                                              if (Instance == null)
                                                  instance = new QueryContainer();
                                      
                                              return instance;
                                          }
                                      }
                                      
                                      public int \_searchID;
                                      
                                      public int ID { get { return \_id; } }
                                      
                                      public string Query
                                      {
                                          get { return Container.Find(instance => instance.\_id == \_searchID).Query; }
                                          set { Container.Query = value; \_id =+ 1; }
                                      }
                                      
                                      private int \_id;
                                      private string \_query;
                                      
                                      private QueryContainer()
                                      { }
                                      

                                      }

                                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                      G Offline
                                      G Offline
                                      Gary R Wheeler
                                      wrote on last edited by
                                      #23

                                      1. The Instance property get will overflow the stack due to recursion. 2. The Query property set and get will both throw NullReferenceException's, since Container is never initialized. 3. The code shouldn't even compile. The Query property set assigns value to Container.Query, but since Container is a List<>, it doesn't have a Query property. I'm going to completely ignore the code's stated purpose, which I don't think what they've provided has the faintest hope of meeting.

                                      Software Zen: delete this;

                                      1 Reply Last reply
                                      0
                                      • S Sascha Lefevre

                                        As you didn't mark your post as a joke or put a smiley into it I have to assume you're being serious: But my post isn't a programming question.

                                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                        P Offline
                                        P Offline
                                        Power Puff Boy
                                        wrote on last edited by
                                        #24

                                        Here's your smiley: :) :-D :laugh: ;) ;P :^) :( :sigh: :doh: :(( :zzz: :-\ :omg: :rolleyes: :-O :wtf: :mad::confused: X| :| :~:suss::cool: Pick the one you like best. Only one per member.

                                        J S 2 Replies Last reply
                                        0
                                        • P Power Puff Boy

                                          Here's your smiley: :) :-D :laugh: ;) ;P :^) :( :sigh: :doh: :(( :zzz: :-\ :omg: :rolleyes: :-O :wtf: :mad::confused: X| :| :~:suss::cool: Pick the one you like best. Only one per member.

                                          J Offline
                                          J Offline
                                          Jorgen Andersson
                                          wrote on last edited by
                                          #25

                                          You forgot a few: :baaaa!: :badger: :beer: :bob: :eek: :java: :jig: :vegemite:

                                          Wrong is evil and must be defeated. - Jeff Ello

                                          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