fixed
-
- Prole
- Posts: 8
- Joined: Thu Feb 29, 2024 11:28 am
fixed
sorted
Last edited by uknowntgyallyonski on Thu Mar 07, 2024 12:13 am, edited 2 times in total.
- BrotSagtMist
- Party member
- Posts: 659
- Joined: Fri Aug 06, 2021 10:30 pm
Re: help cleaning up my code
Start by using [code][/code] in this forum.
Then, why are you creating functions that only contain 1-3 lines of code?
There is no point adding extra lines just for the sake of splattering functionality all over the place.
Then, why are you creating functions that only contain 1-3 lines of code?
There is no point adding extra lines just for the sake of splattering functionality all over the place.
obey
Re: help cleaning up my code
TBH, the way that they create many threads, how they don't use the code tags, plus asking basic questions that hint to a skillset level where they shouldn't be able to write multi-state game code in the first place, makes me think that they did not write this code but got it from somewhere else, like an AI.
Edit: hm, also the way that they phrased their question: "make it more presentable". For whom? Your instructor or teacher? I'm not against people asking for help with homework / assignments, but to not mention it makes it seem like you're trying to get others to do your homework for you.
Edit: hm, also the way that they phrased their question: "make it more presentable". For whom? Your instructor or teacher? I'm not against people asking for help with homework / assignments, but to not mention it makes it seem like you're trying to get others to do your homework for you.
Last edited by RNavega on Wed Mar 06, 2024 10:04 pm, edited 2 times in total.
Re: help cleaning up my code
I would suggest the opposite of BrotSagtMist:
Create more functions that only contain 1-3 lines of code for better readability.
As an example your function love.mousepressed() has 5 indentation levels. With some easy refactorings you can get this down to two or three and have an easier time to read the functions:
Also you can replace most comments with meaningful named variables, constants or function names - e.g. leftMouseButton.
Edit:
Don't double check your conditions and the code gets even easier:
Additionally you can use guard clauses and early returns for fewer indentations and better readability.
Create more functions that only contain 1-3 lines of code for better readability.
As an example your function love.mousepressed() has 5 indentation levels. With some easy refactorings you can get this down to two or three and have an easier time to read the functions:
Code: Select all
function love.mousepressed(x, y, button)
if gameState == "menu" or gameState == "difficulty" or gameState == "instructions" then
local leftMouseButton = 1
if button == leftMouseButton then
handleMouseOutsideOfPlaying(x, y)
end
end
end
function handleMouseOutsideOfPlaying(x, y)
if gameState == "menu" then
handleMouseInMenu(x, y)
elseif gameState == "difficulty" then
handleMouseInDifficulty(x, y)
elseif gameState == "instructions" then
handleMouseInInstructions(x, y)
end
end
Edit:
Don't double check your conditions and the code gets even easier:
Code: Select all
function love.mousepressed(x, y, button)
local leftMouseButton = 1
if button ~= leftMouseButton then
return
end
if gameState == "menu" then
handleMouseInMenu(x, y)
elseif gameState == "difficulty" then
handleMouseInDifficulty(x, y)
elseif gameState == "instructions" then
handleMouseInInstructions(x, y)
end
end
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: fixed
Also for the love of everything, do not blank your original post or edit the topic to "FIXED", that's quite disrespectful to anyone that would have learned something from whatever it was.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests