r/lua • u/Admirable-Donut-6192 • Nov 10 '25
I coded the Collatz conjecture in Lua, please give feedback
I am a lua developer and to test my abilities I decided to code the Collatz conjecture, it's available on GitHub It's a simple code but I am just testing my skills, if you find any way I could make the code better tell me. Thanks Here's the GitHub: https://github.com/Gs-pt/Collatz-conjecture?tab=readme-ov-file
5
Upvotes
2
u/Isogash Nov 10 '25 edited Nov 10 '25
Don't use goto to loop. The only time you should use goto is to break out of multi-level loops, and only when it's the only neat way to do it. Use repeat ... until instead.
Don't define a function within the loop. This re-assigns the function every time, which is just unnecessary. Define your functions at the start of the file.
To be honest, you don't need to use an "is_even" function at all, n % 2 == 0 is plenty readable enough already and you can use a comment if you really want to make it more readable.
It's less important but it's good to get into a habit of being consistent about where you leave a blank line. The basic rule is that you should always have a blank line after a block ends (with the possible exception of when it's within a table definition.) Generally, you should use blank lines to separate different blocks visually to help delineate the blocks.
2
u/Bright-Historian-216 Nov 10 '25
there is simply NO reason to use goto. usually you'll hear this from older folk because high-level languages usually don't even have goto, hell, some implementations of lua forbid goto, and yet it is somehow part of lua... anyway, got sidetracked, use loops instead of goto. why? in simple scripts like yours it's somewhat okay (although it's still an eyesore, AND in your code it's literally less efficient because you're redefining functions every single time) but write something more than double digit lines and it immediately, immediately!! becomes unreadable. if you want goto, learn assembly, it only has goto and none of the traditional loops in any programming language ever.
anyway, thanks for listening to my anger-fueled rant. as you can tell, i hate goto, like basically anyone else ever.
1
u/xoner2 Nov 12 '25
Indentation is bad. Use 2 spaces indent. Use editor that auto-indents. Code should look good.