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. Cleverest Code of the Day!

Cleverest Code of the Day!

Scheduled Pinned Locked Moved The Lounge
csharpsysadmincollaborationquestion
36 Posts 19 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.
  • E Ehsan Sajjad

    While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

    private bool IsHasValue(int? a)
    {
    if (a== null)
    {
    return false;
    }
    return true;
    }

    The code is written by very senior developer on my team with huge years of experience in c# :laugh:

    R Offline
    R Offline
    Rage
    wrote on last edited by
    #2

    Well, it works.

    Do not escape reality : improve reality !

    E 1 Reply Last reply
    0
    • R Rage

      Well, it works.

      Do not escape reality : improve reality !

      E Offline
      E Offline
      Ehsan Sajjad
      wrote on last edited by
      #3

      who said it does not works, it's about reinventing the wheel :)

      N 1 Reply Last reply
      0
      • E Ehsan Sajjad

        While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

        private bool IsHasValue(int? a)
        {
        if (a== null)
        {
        return false;
        }
        return true;
        }

        The code is written by very senior developer on my team with huge years of experience in c# :laugh:

        R Offline
        R Offline
        realJSOP
        wrote on last edited by
        #4

        My advice - don't change the code, and hope your "very senior developer" doesn't have a CP account.

        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
        -----
        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
        -----
        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

        E N 2 Replies Last reply
        0
        • E Ehsan Sajjad

          While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

          private bool IsHasValue(int? a)
          {
          if (a== null)
          {
          return false;
          }
          return true;
          }

          The code is written by very senior developer on my team with huge years of experience in c# :laugh:

          F Offline
          F Offline
          F ES Sitecore
          wrote on last edited by
          #5

          That code certainly .HasValue

          1 Reply Last reply
          0
          • R realJSOP

            My advice - don't change the code, and hope your "very senior developer" doesn't have a CP account.

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            E Offline
            E Offline
            Ehsan Sajjad
            wrote on last edited by
            #6

            i won't change and i am sure senior dev does not use CP :laugh:

            L 1 Reply Last reply
            0
            • E Ehsan Sajjad

              While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

              private bool IsHasValue(int? a)
              {
              if (a== null)
              {
              return false;
              }
              return true;
              }

              The code is written by very senior developer on my team with huge years of experience in c# :laugh:

              Z Offline
              Z Offline
              Z C M
              wrote on last edited by
              #7

              A consultant friend of mine who has been programming far longer than I have—he started programming on the early Macs and helped write the first music notation software for the Mac—doesn't see why C# should need to use .SubString() to get the left/right most characters from a string and instead wrote these. I don't really have a problem with this sort of thing. I just don't see the point, but that's just me.

                  public string LeftStringFunction(string sValue, int iMaxLength)
                  {
                      //Check if the value is valid
                      if (string.IsNullOrEmpty(sValue))
                      {
                          //Set valid empty string as string could be null
                          sValue = string.Empty;
                      }
                      else if (sValue.Length > iMaxLength)
                      {
                          //Make the string no longer than the max length
                          sValue = sValue.Substring(0, iMaxLength);
                      }
              
                      //Return the string
                      return sValue;
                  }
              
                  public string RightStringFunction(string sValue, int iMaxLength)
                  {
                      //Check if the value is valid
                      if (string.IsNullOrEmpty(sValue))
                      {
                          //Set valid empty string as string could be null
                          sValue = string.Empty;
                      }
                      else if (sValue.Length > iMaxLength)
                      {
                          //Make the string no longer than the max length
                          sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
                      }
              
                      //Return the string
                      return sValue;
                  }
              

              "...JavaScript could teach Dyson how to suck." -- Nagy Vilmos

              Richard DeemingR E M 3 Replies Last reply
              0
              • E Ehsan Sajjad

                i won't change and i am sure senior dev does not use CP :laugh:

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

                You see that large number at the top left of this website? A large fraction of those are actually sockpuppets with John pulling the strings. Don't be surprised if the 'persona' of your senior colleague appears out of nowhere. Best Wishes, -David Delaune

                1 Reply Last reply
                0
                • Z Z C M

                  A consultant friend of mine who has been programming far longer than I have—he started programming on the early Macs and helped write the first music notation software for the Mac—doesn't see why C# should need to use .SubString() to get the left/right most characters from a string and instead wrote these. I don't really have a problem with this sort of thing. I just don't see the point, but that's just me.

                      public string LeftStringFunction(string sValue, int iMaxLength)
                      {
                          //Check if the value is valid
                          if (string.IsNullOrEmpty(sValue))
                          {
                              //Set valid empty string as string could be null
                              sValue = string.Empty;
                          }
                          else if (sValue.Length > iMaxLength)
                          {
                              //Make the string no longer than the max length
                              sValue = sValue.Substring(0, iMaxLength);
                          }
                  
                          //Return the string
                          return sValue;
                      }
                  
                      public string RightStringFunction(string sValue, int iMaxLength)
                      {
                          //Check if the value is valid
                          if (string.IsNullOrEmpty(sValue))
                          {
                              //Set valid empty string as string could be null
                              sValue = string.Empty;
                          }
                          else if (sValue.Length > iMaxLength)
                          {
                              //Make the string no longer than the max length
                              sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
                          }
                  
                          //Return the string
                          return sValue;
                      }
                  

                  "...JavaScript could teach Dyson how to suck." -- Nagy Vilmos

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

                  Looks like somebody's trying to reinvent VB6's Left$ and Right$, neither of which throw an exception if you specify a length that's longer than the string. :)


                  "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

                  N D 2 Replies Last reply
                  0
                  • Richard DeemingR Richard Deeming

                    Looks like somebody's trying to reinvent VB6's Left$ and Right$, neither of which throw an exception if you specify a length that's longer than the string. :)


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

                    N Offline
                    N Offline
                    Nish Nishant
                    wrote on last edited by
                    #10

                    Left$ would be GwBasic (not VB6 where the $s were dropped) :-)

                    Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                    Richard DeemingR 1 Reply Last reply
                    0
                    • E Ehsan Sajjad

                      who said it does not works, it's about reinventing the wheel :)

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #11

                      Not just reinventing it, but reinventing it to be less efficient. The actual HasValue implementation just checks an internal flag that's set to true when a value is assigned.

                      Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                      1 Reply Last reply
                      0
                      • R realJSOP

                        My advice - don't change the code, and hope your "very senior developer" doesn't have a CP account.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        N Offline
                        N Offline
                        Nish Nishant
                        wrote on last edited by
                        #12

                        Especially since OP's using his real name :-)

                        Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                        1 Reply Last reply
                        0
                        • E Ehsan Sajjad

                          While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

                          private bool IsHasValue(int? a)
                          {
                          if (a== null)
                          {
                          return false;
                          }
                          return true;
                          }

                          The code is written by very senior developer on my team with huge years of experience in c# :laugh:

                          R Offline
                          R Offline
                          raddevus
                          wrote on last edited by
                          #13

                          Well obviously checking for false (in the calling code) is so much better than checking for null. Plus, if the int isn't null (contains a value) then you get the wonderful True back and true just makes you feel good. :laugh:

                          E 2 Replies Last reply
                          0
                          • R raddevus

                            Well obviously checking for false (in the calling code) is so much better than checking for null. Plus, if the int isn't null (contains a value) then you get the wonderful True back and true just makes you feel good. :laugh:

                            E Offline
                            E Offline
                            Ehsan Sajjad
                            wrote on last edited by
                            #14

                            Yes exactly :laugh:

                            1 Reply Last reply
                            0
                            • E Ehsan Sajjad

                              While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

                              private bool IsHasValue(int? a)
                              {
                              if (a== null)
                              {
                              return false;
                              }
                              return true;
                              }

                              The code is written by very senior developer on my team with huge years of experience in c# :laugh:

                              K Offline
                              K Offline
                              kalberts
                              wrote on last edited by
                              #15

                              Remimds me of the code generated by a compiler I once knew: This was in the days of the superminis, very much CISC. This machine (called ND-500) had a "LoadIndex [register] [indexvalue] [min] [max]" instruction: If the value at [indexvalue] was not between [min] and [max], a exception was generated. This compiler could (optionally) verify all array indexing, using this instruction, but not quite in the straightforward way that you might think. A disassembly showed something like this LOAD R4, indexvalue COMP R4, min JUMPLT error COMP R4, max JUMPGT error % everything is OK, go ahead ... error: LOADINDEX R4, R4, 0, -1 % no legal range, will unconditionally generate exception Generating six instructions to replace a single one can be meaningful, but hardly when one of the six is the instruction you want to replace... I was working in the company making both the CPU and the compiler, so I went to the compiler guy for an explanation. He insisted that since index check was an option, it should not change any code generated, only add code: The index check is like a debugging aid that you might turn off in a production build, and the production version should be exactly the code you debugged, minus the debug features. With the option off, the first instruciton (LOAD R4, indexvalue) was generated. Turn it on, and you get the five additional instructions. Needless to say: Even though his argument sort of sounds plausible, I strongly disagreed with him. First, index checking is not a debug feature; you keep it on in the production version. Second: Given an option set, the compiler should be free to the best code for that selection of options, ignoring other option sets (think of optimization options!) After much arguing, he agreed to rather generate LOAD R4, indexvalue LOADINDEX R4, R4, min, max ... two instructions is better than six. But he stuck to his principles: I never got him to generate a singel LOADINDEX.

                              E 1 Reply Last reply
                              0
                              • K kalberts

                                Remimds me of the code generated by a compiler I once knew: This was in the days of the superminis, very much CISC. This machine (called ND-500) had a "LoadIndex [register] [indexvalue] [min] [max]" instruction: If the value at [indexvalue] was not between [min] and [max], a exception was generated. This compiler could (optionally) verify all array indexing, using this instruction, but not quite in the straightforward way that you might think. A disassembly showed something like this LOAD R4, indexvalue COMP R4, min JUMPLT error COMP R4, max JUMPGT error % everything is OK, go ahead ... error: LOADINDEX R4, R4, 0, -1 % no legal range, will unconditionally generate exception Generating six instructions to replace a single one can be meaningful, but hardly when one of the six is the instruction you want to replace... I was working in the company making both the CPU and the compiler, so I went to the compiler guy for an explanation. He insisted that since index check was an option, it should not change any code generated, only add code: The index check is like a debugging aid that you might turn off in a production build, and the production version should be exactly the code you debugged, minus the debug features. With the option off, the first instruciton (LOAD R4, indexvalue) was generated. Turn it on, and you get the five additional instructions. Needless to say: Even though his argument sort of sounds plausible, I strongly disagreed with him. First, index checking is not a debug feature; you keep it on in the production version. Second: Given an option set, the compiler should be free to the best code for that selection of options, ignoring other option sets (think of optimization options!) After much arguing, he agreed to rather generate LOAD R4, indexvalue LOADINDEX R4, R4, min, max ... two instructions is better than six. But he stuck to his principles: I never got him to generate a singel LOADINDEX.

                                E Offline
                                E Offline
                                Ehsan Sajjad
                                wrote on last edited by
                                #16

                                that's why i wouldn't get in discussion with senior dev on this :laugh:

                                1 Reply Last reply
                                0
                                • Z Z C M

                                  A consultant friend of mine who has been programming far longer than I have—he started programming on the early Macs and helped write the first music notation software for the Mac—doesn't see why C# should need to use .SubString() to get the left/right most characters from a string and instead wrote these. I don't really have a problem with this sort of thing. I just don't see the point, but that's just me.

                                      public string LeftStringFunction(string sValue, int iMaxLength)
                                      {
                                          //Check if the value is valid
                                          if (string.IsNullOrEmpty(sValue))
                                          {
                                              //Set valid empty string as string could be null
                                              sValue = string.Empty;
                                          }
                                          else if (sValue.Length > iMaxLength)
                                          {
                                              //Make the string no longer than the max length
                                              sValue = sValue.Substring(0, iMaxLength);
                                          }
                                  
                                          //Return the string
                                          return sValue;
                                      }
                                  
                                      public string RightStringFunction(string sValue, int iMaxLength)
                                      {
                                          //Check if the value is valid
                                          if (string.IsNullOrEmpty(sValue))
                                          {
                                              //Set valid empty string as string could be null
                                              sValue = string.Empty;
                                          }
                                          else if (sValue.Length > iMaxLength)
                                          {
                                              //Make the string no longer than the max length
                                              sValue = sValue.Substring(sValue.Length - iMaxLength, iMaxLength);
                                          }
                                  
                                          //Return the string
                                          return sValue;
                                      }
                                  

                                  "...JavaScript could teach Dyson how to suck." -- Nagy Vilmos

                                  E Offline
                                  E Offline
                                  englebart
                                  wrote on last edited by
                                  #17

                                  If Right$() and Left$() make some other code easier to parse. Good for him. It seems like those should be static or some sort of extension method. What is "this" in those methods?

                                  1 Reply Last reply
                                  0
                                  • Richard DeemingR Richard Deeming

                                    Looks like somebody's trying to reinvent VB6's Left$ and Right$, neither of which throw an exception if you specify a length that's longer than the string. :)


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

                                    D Offline
                                    D Offline
                                    den2k88
                                    wrote on last edited by
                                    #18

                                    Richard Deeming wrote:

                                    either of which throw an exception if you specify a length that's longer than the string.

                                    Why on Earth should they?

                                    GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++*      Weapons extension: ma- k++ F+2 X

                                    Richard DeemingR 1 Reply Last reply
                                    0
                                    • E Ehsan Sajjad

                                      While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

                                      private bool IsHasValue(int? a)
                                      {
                                      if (a== null)
                                      {
                                      return false;
                                      }
                                      return true;
                                      }

                                      The code is written by very senior developer on my team with huge years of experience in c# :laugh:

                                      O Offline
                                      O Offline
                                      obermd
                                      wrote on last edited by
                                      #19

                                      Does it predate the Nullable data type being added to the framework?

                                      E 1 Reply Last reply
                                      0
                                      • O obermd

                                        Does it predate the Nullable data type being added to the framework?

                                        E Offline
                                        E Offline
                                        Ehsan Sajjad
                                        wrote on last edited by
                                        #20

                                        the parameter is itself a Nullable and i know this is the latest code that we are working on in .NET 4.0 using C# 6 language in it.

                                        1 Reply Last reply
                                        0
                                        • E Ehsan Sajjad

                                          While comparing my changes with the server source code, just saw with my two eyes this great piece of code which checks if an nullable int is null or not :

                                          private bool IsHasValue(int? a)
                                          {
                                          if (a== null)
                                          {
                                          return false;
                                          }
                                          return true;
                                          }

                                          The code is written by very senior developer on my team with huge years of experience in c# :laugh:

                                          G Offline
                                          G Offline
                                          Gary Huck
                                          wrote on last edited by
                                          #21

                                          You could consider telling the senior dev about the .HasValue property; knowledge share. Also, the correct English for "... an nullable ..." is "... a nullable ...". :)

                                          E 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