C
C#2y ago
Metalkon

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
Kiel
Kiel2y ago
whileCondition is a bit redundant for what you are doing: you can use 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 keep since you are returning result 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 null
Pobiega
Pobiega2y ago
Not to mention noise-to-signal ratio. The second piece of code does the same thing, but is less "fluff" around the important bits