ComputerCraft Forums

ComputerCraft => Programs => Topic started by: nixgoat on May 07, 2021, 09:52 AM

Title: Rhythmblock - a graphical, customizable music player
Post by: nixgoat on May 07, 2021, 09:52 AM
(https://camo.tmpim.com/257fba25a321380784ec9a6fd1e0ddef59302d3b/68747470733a2f2f66656d2e6d696e742e6c6762742f6d2f52687974686d626c6f636b2f7261772f6272616e63682f6d61737465722f6c6f676f2e706e67)
Rhythmblock
a graphical, customizable music player for ComputerCraft

(https://camo.tmpim.com/cb97748b0dfc85aa105f03a7458b07718d568564/68747470733a2f2f66656d2e6d696e742e6c6762742f6d2f52687974686d626c6f636b2f7261772f6272616e63682f6d61737465722f73637265656e73686f742e706e67)

Features

Keyboard Shortcuts

space - play/stop
e - eject
q - quit

Install

run the following on the desired CC computer

wget https://fem.mint.lgbt/m/Rhythmblock/raw/branch/master/rhythmblock.lua Rhythmblock
Source Code

you can find the source code for this project at https://fem.mint.lgbt/m/Rhythmblock
Title: Rhythmblock - a graphical, customizable music player
Post by: nixgoat on May 07, 2021, 10:05 AM
i'm open for people to review my code! i'm starting out in lua and it would be pretty useful if you sent me suggestions on what to improve
Title: Rhythmblock - a graphical, customizable music player
Post by: exerro on May 07, 2021, 01:08 PM
Hello and congrats on the first post! It's a nice program - don't have much use for it myself, but it does one thing and it does it well, that's always a good sign :)

Regarding the code, I noticed some good things:
* Good use of functions! For the most part, the code isn't too indented which is nice. The little "units of functionality" and recurring operations have been taken out of larger blocks of code and put into small, focussed functions.
* There are some comments. You psycho, documenting your code... Everybody knows that to be a successful programmer you need 1000 line unreadable monoliths that nobody but you will understand, and that's on a good day.

A not so good thing...
* You're afraid of `local` - functions a variables should almost always be local. You have a few locals in the program but there should be *way* more. This is probably what I'd focus on if I were you. I get the impression that maybe you're coming from another language? This is a pretty Lua-specific thing that you just need to get used to. Linters can generally catch and warn about global usages.

And some trivial issues...
* Your `round` function is overcomplicated. round(x) = floor(x + 0.5) - Think of rounding 1, 1.25, 1.5, 1.75, and 2 using this.
* You compare to `true` sometimes, which is rarely needed. `if x == true then` can usually be simplified to `if x then`
* Your mouse click box testing is shared between two lines and pretty long, this could maybe be a function that's called where mouse click testing is needed?
* I generally try to group variables, functions, and "main code" together. You've got some variables interspersed between functions which can make them a lil' hard to track down. This is mostly personal preference though.
* It's good to roughly describe what a function does, if it's non trivial, and also the general aim/purpose of a file. Nobody does this.

Good luck with your future projects!
Title: Rhythmblock - a graphical, customizable music player
Post by: Lupus590 on May 07, 2021, 04:03 PM
QuoteLinters can generally catch and warn about global usages.
There's also the bios.strict_globals setting which will crash the program if it tries to set a global.
You can use the set program or the settings API to set any settings API value.

Quote from: exerro on May 07, 2021, 01:08 PMYour `round` function is overcomplicated. round(x) = floor(x + 0.5) - Think of rounding 1, 1.25, 1.5, 1.75, and 2 using this.
I can't think of a better way of doing this.
Title: Rhythmblock - a graphical, customizable music player
Post by: nixgoat on May 08, 2021, 12:39 AM
thank you @exerro! i took the time to fix the if statements but couldn't fix the global vars. the rounding variable is actually just a kang from here (https://scriptinghelpers.org/questions/4850/how-do-i-round-numbers-in-lua-answered)