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.
  • 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
            • G Gary Huck

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

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

              senior is aware of that, but i am not getting why this method was authored in first place :) I will discuss for sure sometime that.

              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
                rnbergren
                wrote on last edited by
                #23

                quit reviewing my code little dude. :)

                To err is human to really mess up you need a computer

                E 1 Reply Last reply
                0
                • R rnbergren

                  quit reviewing my code little dude. :)

                  To err is human to really mess up you need a computer

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

                  :omg: it was you, didn't knew :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:

                    P Offline
                    P Offline
                    patbob
                    wrote on last edited by
                    #25

                    Ehsan Sajjad wrote:

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

                    There's the problem right there -- they're not used to thinking of base, simple types as classes. Ask them what they think an int? is, and they'll probably tell you it's a pointer to an int. Pointers aren't classes that can have functions. Took me a while to get used to the concept too.

                    I live in Oregon, and I'm an engineer.

                    E 1 Reply Last reply
                    0
                    • P patbob

                      Ehsan Sajjad wrote:

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

                      There's the problem right there -- they're not used to thinking of base, simple types as classes. Ask them what they think an int? is, and they'll probably tell you it's a pointer to an int. Pointers aren't classes that can have functions. Took me a while to get used to the concept too.

                      I live in Oregon, and I'm an engineer.

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

                      you are probably right, once i was having code review meeting with that dev and i was asked to replace var keyword with that particular type name, as according to him, it will degrade performance, i then corrected him and gave reference to a post which explained it is just implicit variable that infers the type and nothing else. :)

                      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

                        M Offline
                        M Offline
                        MSBassSinger
                        wrote on last edited by
                        #27

                        I seem to remember VB having that functionality for strings.

                        1 Reply Last reply
                        0
                        • N Nish Nishant

                          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 Offline
                          Richard DeemingR Offline
                          Richard Deeming
                          wrote on last edited by
                          #28

                          The $s weren't required, but IIRC they were still allowed. Left returned a Variant, whereas Left$ returned a String. But I could be wrong - it's been over 15 years since I last touched VB6. :D


                          "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 1 Reply Last reply
                          0
                          • D den2k88

                            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 Offline
                            Richard DeemingR Offline
                            Richard Deeming
                            wrote on last edited by
                            #29

                            Because that's how there were designed. Probably because that's how the Java version[^] works. :) Specifying an index or a length outside of the string is usually a sign that something's gone wrong. In which case, it's better to have your application crash than for it to continue with potentially corrupted data. If you don't like it, you can always add your own checks. Or roll your own "ignore-the-errors" wrappers. Or call the VB.NET methods[^].


                            "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

                            1 Reply Last reply
                            0
                            • Richard DeemingR Richard Deeming

                              The $s weren't required, but IIRC they were still allowed. Left returned a Variant, whereas Left$ returned a String. But I could be wrong - it's been over 15 years since I last touched VB6. :D


                              "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
                              #30

                              Ah yes, now that you say it, it sounds vaguely familiar :-)

                              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:

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

                                My issue is with "IsHas". I use "is" or "has", not both.

                                "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                                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:

                                  J Offline
                                  J Offline
                                  James Lonero
                                  wrote on last edited by
                                  #32

                                  He hasn't written enough code for the week.

                                  1 Reply 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
                                    #33

                                    my one of the colleague just pinged me with :

                                    IsHasValue() => IsHasHavingValuePerhaps();

                                    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:

                                      M Offline
                                      M Offline
                                      Member_5893260
                                      wrote on last edited by
                                      #34

                                      My all-time favourite was from a mid-level programmer of ours who wanted to pass a form field value from one web page to the next: something he did all the time... except that in this instance, for some inexplicable reason, he created a table in the database, used Ajax to write the value to it and retrieve a record ID; he then put the record ID into a hidden field on the page, submitted the form, and then, in the next page, used the record ID to retrieve the value from the database and delete the record. Unfortunately, his English wasn't that great... so in response to the question, "What the hell is this supposed to be?" he said, "That mean I do my way. Fuck you, Dan!"

                                      E 1 Reply Last reply
                                      0
                                      • M Member_5893260

                                        My all-time favourite was from a mid-level programmer of ours who wanted to pass a form field value from one web page to the next: something he did all the time... except that in this instance, for some inexplicable reason, he created a table in the database, used Ajax to write the value to it and retrieve a record ID; he then put the record ID into a hidden field on the page, submitted the form, and then, in the next page, used the record ID to retrieve the value from the database and delete the record. Unfortunately, his English wasn't that great... so in response to the question, "What the hell is this supposed to be?" he said, "That mean I do my way. Fuck you, Dan!"

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

                                        I also had a mid level developer colleague who had set the bool flag to false by default and then the next statement was checking if it's true

                                        M 1 Reply Last reply
                                        0
                                        • E Ehsan Sajjad

                                          I also had a mid level developer colleague who had set the bool flag to false by default and then the next statement was checking if it's true

                                          M Offline
                                          M Offline
                                          Member_5893260
                                          wrote on last edited by
                                          #36

                                          Excellent. The same one mentioned above wrote in ASP (Classic): "if cstr(ucase(x))=cstr(ucase("CA")) then ..." there was also some code about cint("1") somewhere...

                                          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