:ellie: Noëlle the 8-Bit🏳️‍🌈🎄 is a user on elekk.xyz. You can follow them or interact with them if you have an account anywhere in the fediverse. If you don't, you can sign up here.

Is the first way the "Ruby way" (how I wrote it) and the second more functional/the Golang way (from a Golang video tutorial)?

(Also, really warming up to my new Vim colorscheme...) octodon.social/media/1wicQXUJE

@schlink Hm. I dunno much about Golang, but in Ruby I think I'd write:

def factorial(num):
if num <= 1:
return 1
end
num * factorial(num - 1)
end

@noelle huh. I was taught not to have `return` statements early in methods (unless error checking), which I first resisted but eventually came around to as it can improve readability if you're consistent about it.

I'm actually not 100% how your method works!! I'd have thought it would always return a 1...

@schlink It should only return 1 if num is less than or equal to 1 (<= 1). return statements in the final position are apparently unRubyic, so I got rid of it, but you could just as easily write

return num * factorial(num - 1)

:ellie: Noëlle the 8-Bit🏳️‍🌈🎄 @noelle

@schlink As for having return statements early in methods - have return statements wherever it makes sense to have them. Not having them early in functions means you're giving up on a lot of input checking. There's no real value in reserving return statements until the end of a function; it doesn't /actually/ improve readability and it's just arbitrary.

· Web · 0 · 0