Last line return vs if-else return

Hi,

I’m noticing in the alphacourse solutions, outputs are being returned a certain way. For example:

if x == 1
return x
end
return y

Is that preferred over returning an output in the following manner:

if x == 1
return x
else
return y
end

Is the first preferred over the second? Or is it merely for style.

Thanks,
Ryan

Hi Ryan,

It’s typically easier to read code that doesn’t have extra if/else if/else branches in it, so stylistically, the first one is more correct. It also sets you up to ease into Ruby’s implicit last line returns.

Later on, you’ll be introduced to another way to write the code for single line if statements, which is even shorter and more like English:

return x if x == 1

1 Like

Awesome thank you for the quick feedback!