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. General Programming
  3. C#
  4. Readability of the code

Readability of the code

Scheduled Pinned Locked Moved C#
questionannouncement
6 Posts 3 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.
  • V Offline
    V Offline
    Vitaliy Tsvayer
    wrote on last edited by
    #1

    Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________

    private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
    {
      string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
      if (string.IsNullOrEmpty(profileKey))
      {
        return null;
      }
    
      string parameterValue = scenario.IPHelper.Profile[profileKey];
      if (string.IsNullOrEmpty(parameterValue))
      {
        return null;
      }
    
      ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
      ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue);
    
      return pd;
    }
    

    version 2: __________________________________

    private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
    {
      ContentParameterDetail pd = null;
      string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
    
      if(!string.IsNullOrEmpty(profileKey))
      {
        string parameterValue = scenario.IPHelper.Profile[profileKey];
        if (!string.IsNullOrEmpty(parameterValue))
        {
          ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
          pd = parameter.GetDetailbySPGWCode(parameterValue);
        }
      }
    
      return pd;
    }
    
    S F 2 Replies Last reply
    0
    • V Vitaliy Tsvayer

      Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________

      private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
      {
        string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
        if (string.IsNullOrEmpty(profileKey))
        {
          return null;
        }
      
        string parameterValue = scenario.IPHelper.Profile[profileKey];
        if (string.IsNullOrEmpty(parameterValue))
        {
          return null;
        }
      
        ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
        ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue);
      
        return pd;
      }
      

      version 2: __________________________________

      private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
      {
        ContentParameterDetail pd = null;
        string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
      
        if(!string.IsNullOrEmpty(profileKey))
        {
          string parameterValue = scenario.IPHelper.Profile[profileKey];
          if (!string.IsNullOrEmpty(parameterValue))
          {
            ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
            pd = parameter.GetDetailbySPGWCode(parameterValue);
          }
        }
      
        return pd;
      }
      
      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      I prefer version 1 cause there it's perfectly clear that null gets returned if string.IsNullOrEmpty is true. In version 2 I have to take a look at the whole method to see that pd isn't changed anywhere and therefor null will be returned.


      "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

      www.troschuetz.de

      V 1 Reply Last reply
      0
      • V Vitaliy Tsvayer

        Hi, there are two functions below that do the same thing. And the question is which one of them is more readable? Which one would you prefere? And why? Thanks in advance, Vitaliy version 1: __________________________________

        private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
        {
          string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
          if (string.IsNullOrEmpty(profileKey))
          {
            return null;
          }
        
          string parameterValue = scenario.IPHelper.Profile[profileKey];
          if (string.IsNullOrEmpty(parameterValue))
          {
            return null;
          }
        
          ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
          ContentParameterDetail pd = parameter.GetDetailbySPGWCode(parameterValue);
        
          return pd;
        }
        

        version 2: __________________________________

        private ContentParameterDetail GetParameterFromProfile(ContentParameterX prm, SubscriptionScenario scenario)
        {
          ContentParameterDetail pd = null;
          string profileKey = scenario.TimeoutMode ? prm.GetFromProfileTimeout : prm.GetFromProfile;
        
          if(!string.IsNullOrEmpty(profileKey))
          {
            string parameterValue = scenario.IPHelper.Profile[profileKey];
            if (!string.IsNullOrEmpty(parameterValue))
            {
              ContentParameter parameter = _entity.ContentConfig.GetParameter(prm.ParameterID);
              pd = parameter.GetDetailbySPGWCode(parameterValue);
            }
          }
        
          return pd;
        }
        
        F Offline
        F Offline
        Filip van der Meeren
        wrote on last edited by
        #3

        I would go with version 1... You can actually see when it returns null, in version 2 you really have to read all the code to know when it returns null...

        Don't you also love the code?

        V 1 Reply Last reply
        0
        • S Stefan Troschuetz

          I prefer version 1 cause there it's perfectly clear that null gets returned if string.IsNullOrEmpty is true. In version 2 I have to take a look at the whole method to see that pd isn't changed anywhere and therefor null will be returned.


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          V Offline
          V Offline
          Vitaliy Tsvayer
          wrote on last edited by
          #4

          Yes, good point. I just wonder if those multiple return statements are bad. Otherwise I defenitely go with version 1. Vitaliy

          S 1 Reply Last reply
          0
          • F Filip van der Meeren

            I would go with version 1... You can actually see when it returns null, in version 2 you really have to read all the code to know when it returns null...

            Don't you also love the code?

            V Offline
            V Offline
            Vitaliy Tsvayer
            wrote on last edited by
            #5

            Yes, Stefan in above reply also pointed that out. OK, I go with version 1. I LOVE THE CODE! :-D Vitaliy

            1 Reply Last reply
            0
            • V Vitaliy Tsvayer

              Yes, good point. I just wonder if those multiple return statements are bad. Otherwise I defenitely go with version 1. Vitaliy

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Vitaliy Tsvayer wrote:

              if those multiple return statements are bad

              This is a controversial topic and you can find many comments on this when googling for "multiple returns" e.g. this one[^] by Bruce Eckel.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              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