It's not at all necessary, but I find it makes much easier to read code if you instead only use if statements and just return early when you're in a function. For example, you could check isalpha(letter) == true
is true then check letter + key <= 90
do the letter += key; return letter;
then since letter + key must be > 90 if it didn't already return a value, then you can do the while statement and return letter without needing an if statement at all. Then the isalpha(letter) == false
is also unncessary, just return letter at the end.
Like this:
`char rotate(char letter, int key)
{
if (isalpha(letter) {
if (letter + key <= 90)
{
letter += key;
return letter;
}
// do the while loop here
}
return letter;
}`
towerful@programming.dev 11 months ago
I’m a fan of early returns.
So
if(isalpha(letter) == false) return letter if(letter whater) { Do thing Return letter } if(letter something else) { Do whatever Return letter } throw error(“unprocessable letter”)
If find it lets me mentally walk through all the paths.
And if something gets too complex for this, then I need to break it down into further functions