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. Ethical Coding Style

Ethical Coding Style

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 2 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.
  • W Offline
    W Offline
    Wayne Phipps
    wrote on last edited by
    #1

    I regularly have questions about what/which way is best, most efficient or ethical. I want to develop applications that look, feel and work well from both a user and coding perspective. If as an example I wanted to check if a value had changed, I would consider using something like: int _CurrentValue; int CurrentValue { set{_CurrentValue = value;} get{return _CurrentValue;} } bool ValueChanged(int NewValue) { if (CurrentValue != NewValue) { return true; } else { return false; } } I was taught (many years ago) that a method should have one entry and one exit point wherever possible so to abide to that standard, the above could be written as: bool ValueChanged(int NewValue) { bool result; if (CurrentValue != NewValue) { result = true; } else { result = false; } return result; } I was also taught however that you shouldn't use a variable where one was not needed and as the previous code demonstrated, only the programming style was different the result would be the same. I was just hoping for comments as which method is best/more efficient. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

    C 1 Reply Last reply
    0
    • W Wayne Phipps

      I regularly have questions about what/which way is best, most efficient or ethical. I want to develop applications that look, feel and work well from both a user and coding perspective. If as an example I wanted to check if a value had changed, I would consider using something like: int _CurrentValue; int CurrentValue { set{_CurrentValue = value;} get{return _CurrentValue;} } bool ValueChanged(int NewValue) { if (CurrentValue != NewValue) { return true; } else { return false; } } I was taught (many years ago) that a method should have one entry and one exit point wherever possible so to abide to that standard, the above could be written as: bool ValueChanged(int NewValue) { bool result; if (CurrentValue != NewValue) { result = true; } else { result = false; } return result; } I was also taught however that you shouldn't use a variable where one was not needed and as the previous code demonstrated, only the programming style was different the result would be the same. I was just hoping for comments as which method is best/more efficient. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      his is the strangest thing I've ever seen. Why does 'ValueChanged' change the value ? How would someone looking at the API work this out ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

      W 1 Reply Last reply
      0
      • C Christian Graus

        his is the strangest thing I've ever seen. Why does 'ValueChanged' change the value ? How would someone looking at the API work this out ? Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

        W Offline
        W Offline
        Wayne Phipps
        wrote on last edited by
        #3

        :wtf: I was testing the values returned from a method and left that in by accident. I agree, a method that checks if a property has changed should not change the property it was checking. I have ammended the example as not to cause confusion. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

        C 1 Reply Last reply
        0
        • W Wayne Phipps

          :wtf: I was testing the values returned from a method and left that in by accident. I agree, a method that checks if a property has changed should not change the property it was checking. I have ammended the example as not to cause confusion. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          In that case I have several comments: 1. If I were to write this, I'd just do something like return(CurrentValue == theValue);, there's no need for a bool variable 2. If I did have to use a variable, I'd make sure it had a default value. In cases like this, I'd start with something like bool retVal = false;, and just set it to true if the test passed later 3. You seem to be asking if the current value != a specific value. I'd do this in the calling code: if (myClass.CurrentVale != theValue) instead of adding a method needlessly. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

          W 1 Reply Last reply
          0
          • C Christian Graus

            In that case I have several comments: 1. If I were to write this, I'd just do something like return(CurrentValue == theValue);, there's no need for a bool variable 2. If I did have to use a variable, I'd make sure it had a default value. In cases like this, I'd start with something like bool retVal = false;, and just set it to true if the test passed later 3. You seem to be asking if the current value != a specific value. I'd do this in the calling code: if (myClass.CurrentVale != theValue) instead of adding a method needlessly. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

            W Offline
            W Offline
            Wayne Phipps
            wrote on last edited by
            #5

            Thanks for your comments, they all make sense. However I wrote a simple method to demonstrate multiple and single exit points from the method not the simple evaluation of an integer. Some methods become much more complicated when using nested if or switch statements and it appears very easy to end up with multiple exit points which make the method difficult to follow and debug. I try to avoid this wherever possible but just wanted other opinions on best practices. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

            C 1 Reply Last reply
            0
            • W Wayne Phipps

              Thanks for your comments, they all make sense. However I wrote a simple method to demonstrate multiple and single exit points from the method not the simple evaluation of an integer. Some methods become much more complicated when using nested if or switch statements and it appears very easy to end up with multiple exit points which make the method difficult to follow and debug. I try to avoid this wherever possible but just wanted other opinions on best practices. Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students LearnVisualStudio.Net

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              OK, in that case, I'd always create the return value first, default it to the worst case scenario ( and never create a variable that I don't assign to ), and then I'd prefer switch to if statements where-ever possible, and if my switch statements are getting too big, think about if I can use OO or at least extra levels of call stack depth to factor some of that out. Christian I have several lifelong friends that are New Yorkers but I have always gravitated toward the weirdo's. - Richard Stringer

              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