Variable Validation
What are the advantages/disadvantages between both of these bits of code to validate the correct input type? The top one is mine and the bottom is from a video I was just watching and i've been wondering if my method is just too sloppy/ugly/etc.
2 Replies
whileCondition
is a bit redundant for what you are doing: break;
instead to break out of the while loop - this also means you don't need the else{}
block and can just put the two lines inside it on their own, but it's still fine to keepresult
in the end anyway, you can actually replace whileCondition = true
with return result;
directly :)
Console.ReadLine()
can return a null string in certain cases, so the example code from the video which does the string.IsNullOrEmpty()
check is valid (though most people I know prefer using string.IsNullOrWhiteSpace()
). Never too early to get in the habit of checking things for nullNot to mention noise-to-signal ratio.
The second piece of code does the same thing, but is less "fluff" around the important bits