Ethical Coding Style
-
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 -
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.Nethis 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
-
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
: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
-
: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
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
-
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
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
orswitch
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 -
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
orswitch
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.NetOK, 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