[Just for fun] Roast my code.

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
zalander
Citizen
Posts: 75
Joined: Mon Jan 09, 2023 5:58 am
Location: India
Contact:

[Just for fun] Roast my code.

Post by zalander »

You read the title.
Roast it (if you want)

Code: Select all

--main.lua
function love.load()
    require("libs/sav")
    tmp = nil
    ar = {}
    y = 0
    mode = "run"
    fnt = love.graphics.newFont(12)
    love.filesystem.createDirectory("saves")
    function initar(file, add)
        tmp = load(file)
        for i = 1, #tmp, 1 do
            ar[i] = tmp:sub(i, i)
        end
        for i = 1, #ar, 1 do
            if ar[i] == "/" and ar[i + 1] == "n" then
                ar[i] = "\n"
                for i = i + 2, #ar, 1 do
                    ar[i - 1] = ar[i]
                    if i == #ar then
                        ar[i] = nil
                    end
                end
            end
        end
        if add then
            ar[#ar + 1] = "\b"
        end
        if ar[#ar - 1] == "n" then
            ar[#ar - 1] = ""
        end
    end
    initar("saves/def_save.txt", true)
    function getnewlines(array, offset)
        tmp = {}
        tmp[1] = 0
        tmp[2] = offset
        while true do
            tmp[1] = tmp[1] + 1
            if array[tmp[1]] == "\n" then
                tmp[2] = tmp[2] + 1
            elseif array[tmp[1]] == nil then
                return tmp[2]
            end
        end
    end
    love.keyboard.setKeyRepeat(true)
    love.window.showMessageBox("Credit", "Made by Zalan(Zalnder)", "info")
    tmp = love.window.showMessageBox("Warning", "This is not a fully finished build of version 2.0.1 bugs may occur. \n Continue ?", {"yes", "no(exit)"}, "warning")
    if tmp == 2 then
        love.event.quit(0)
    end
    love.graphics.setFont(fnt)
end
function love.update(dt)
    if mode == "run" then
        if (getnewlines(ar, 1) + 3) * 14 - math.abs(y) > love.graphics.getHeight() then
            y = y - 12
        end
    end
    if love.keyboard.isDown("escape") then
        tmp = ""
        ar[#ar] = nil 
        for i = 1, #ar, 1 do
            if ar[i] == "\n" then
                tmp = tmp.."/n"
            else
                tmp = tmp..ar[i]
            end
        end
        save("saves/def_save.txt", tmp)
        love.event.quit(0)
    end
end
function love.draw()
    if getnewlines(ar, 1) > 100 then
        love.graphics.print(ar, 32, y)
    else
        love.graphics.print(ar, 20, y)
    end
    for i = 0, getnewlines(ar, 1), 1 do
        love.graphics.print(i, 0, i * 14 + y)
    end
    love.graphics.rectangle("line", 0, love.graphics.getHeight() - 20, love.graphics.getWidth(), 30)
    love.graphics.print("This is a extreamly early protoype !", 0, love.graphics.getHeight() - 20)
    if mode == "run" then
        love.graphics.print("Press left control to save", 12 * 36, love.graphics.getHeight() - 20)
    elseif mode == "file opn" then
        love.graphics.print("This is load mode enter filename to be loaded", 12 * 36, love.graphics.getHeight() - 20)
    elseif mode == "file sav" then
        love.graphics.print("This is save mode enter filene to be saved", 12 * 36, love.graphics.getHeight() - 20)
    end
    if love.keyboard.isDown("lctrl") and mode == "run" then
        love.graphics.print("(saved)", love.graphics.getWidth() - 69, love.graphics.getHeight() - 20)
    end
end
function love.keypressed(key)
    ar[#ar] = nil
    if key == "return" or key == "down" then
        if mode == "run" then
            if ar[#ar - 1] == "/" and ar[#ar] == "l" then
                ar = {}
                mode = "file opn"
            elseif ar[#ar - 1] == "/" and ar[#ar] == "s" then
                ar = {}
                mode = "file sav"
            else
                ar[#ar + 1] = "\n"
            end
        else
            if mode == "file opn" then
                tmp = ""
                for i = 1, #ar, 1 do
                    tmp = tmp..ar[i]
                end
                print(tmp)
                if not(love.filesystem.getInfo("saves/"..tmp)) then
                    error("file not found") -- error out if file is not found
                end
                save("saves/def_save.txt", love.filesystem.read("saves/"..tmp))
                ar = {}
                initar("saves/def_save.txt", true)
                mode = "run"
            end
            if mode == "file sav" then
                tmp = ""
                for i = 1, #ar, 1 do
                    tmp = tmp..ar[i]
                end
                if not(love.filesystem.getInfo("saves/"..tmp)) then
                    love.filesystem.newFile("saves/"..tmp)
                end
                save("saves/"..tmp, load("saves/def_save.txt"))
                ar = {}
                initar("saves/def_save.txt", true)
                mode = "run"
            end
        end
    end
    if key == "backspace" then
        if mode == "run" then
            if not(y == 0) then
                if ar[#ar] == "\n" then
                    if y > 0 then
                        y = 0
                    else
                        y = y + 15
                    end
                end
            end
        end
        ar[#ar] = nil
    end
    if mode == "run" then
        if key == "lctrl" then
            tmp = ""
            for i = 1, #ar, 1 do
                if ar[i] == "\n" then
                    tmp = tmp.."/n"
                else
                    tmp = tmp..ar[i]
                end
            end
            save("saves/def_save.txt", tmp)
        end
    end
    ar[#ar + 1] = "\b"
end
function love.textinput(key)
    ar[#ar] = nil
    ar[#ar + 1] = key
    ar[#ar + 1] = "\b"
end

Code: Select all

--save.lua (libs)
function save(filename, data)
    if love.filesystem.getInfo(filename) then
        love.filesystem.write(filename, data)
    else
        love.filesystem.newFile(filename)
        love.filesystem.write(filename, data)
    end
end
function load(filename)
    if love.filesystem.getInfo(filename) then
        return(love.filesystem.read(filename))
    else
        -- else is edited for this project
        love.filesystem.newFile(filename)
        love.filesystem.write(filename, "Type away~ /")
        return(love.filesystem.read(filename))
    end
end
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
User avatar
Azzla
Prole
Posts: 43
Joined: Sun Mar 29, 2020 2:23 am

Re: [Just for fun] Roast my code.

Post by Azzla »

1. 'ar' is a terrible, non-descript variable name
2. 'sav' instead of 'save' for the sake of abbreviating a mere four letter word into a 3 letter filename
3. 'fnt' instead of 'font' for the sake of abbreviating a mere four letter word into a 3 letter variable name
4. 'tmp' instead of 'temp' for the sake of... you know what? This is probably just my pet peeve or something.
5. Defining and requiring functions in love.load for no reason. Also your save "library" has literally two functions in it.
6. Magic values everywhere. Define numeric constants that are associated with UI/graphic/text rendering somewhere at the top of your file for readability and easier adjusting.

That's all I can roast at a quick glance, I've never tried to make a text editor so I can't really critique your algorithms. :D You have no window wrapping so you can type on a line forever, but that's more of a "incompleteness" thing than a code thing.
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
User avatar
zalander
Citizen
Posts: 75
Joined: Mon Jan 09, 2023 5:58 am
Location: India
Contact:

Re: [Just for fun] Roast my code.

Post by zalander »

Azzla wrote: Sun Dec 24, 2023 7:07 am 1. 'ar' is a terrible, non-descript variable name
2. 'sav' instead of 'save' for the sake of abbreviating a mere four letter word into a 3 letter filename
3. 'fnt' instead of 'font' for the sake of abbreviating a mere four letter word into a 3 letter variable name
4. 'tmp' instead of 'temp' for the sake of... you know what? This is probably just my pet peeve or something.
5. Defining and requiring functions in love.load for no reason. Also your save "library" has literally two functions in it.
6. Magic values everywhere. Define numeric constants that are associated with UI/graphic/text rendering somewhere at the top of your file for readability and easier adjusting.

That's all I can roast at a quick glance, I've never tried to make a text editor so I can't really critique your algorithms. :D You have no window wrapping so you can type on a line forever, but that's more of a "incompleteness" thing than a code thing.
I asked more of a "roast" but thanks for pointing out the probles ill work on them to fix them :D
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: [Just for fun] Roast my code.

Post by dusoft »

He roasted you well. All the good, basic pointers to improve your code. Just read some tutorial about code standards.

Also lots of ifs in loops, maybe some could be replaced by recursive functions, but I am lazy to check in detail, sorry. Anyway, the code is bit overcomplicated and difficult to read since your variables are not self descriptive.
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: [Just for fun] Roast my code.

Post by knorke »

You re-use the "tmp" variable multiple times for completely different things.

Code: Select all

tmp = load(file)
->
fileContent = load(fileName)

Code: Select all

    function getnewlines(array, offset)
        tmp = {}
        tmp[1] = 0
        tmp[2] = offset
        ->
        local i = 0
        local newlinesCount = 0
        

Code: Select all

  tmp = love.window.showMessageBox("Warning", "This is not a fully finished build of version 2.0.1 bugs may occur. \n Continue ?", {"yes", "no(exit)"}, "warning")
    if tmp == 2 then
->
  local clickedButton = love.window.showMessageBox("Warning", "This is not a fully finished build of version 2.0.1 bugs may occur. \n Continue ?", {"yes", "no(exit)"}, "warning")
    if clickedButton == 2 then    
    

Code: Select all

   if love.keyboard.isDown("escape") then
        tmp = ""
->
   if love.keyboard.isDown("escape") then
        inputText = ""
User avatar
zalander
Citizen
Posts: 75
Joined: Mon Jan 09, 2023 5:58 am
Location: India
Contact:

Re: [Just for fun] Roast my code.

Post by zalander »

Thanks for the advise :)
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: [Just for fun] Roast my code.

Post by milon »

Why is 'tmp' a global??? Local variables have far better performance!

Also, try to structure your game to not need love.load() at all - it'll help you improve your code structure.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [Just for fun] Roast my code.

Post by zorg »

milon wrote: Thu Dec 28, 2023 4:14 pm Why is 'tmp' a global??? Local variables have far better performance!

Also, try to structure your game to not need love.load() at all - it'll help you improve your code structure.
First one is minuscule on most real life examples, but it does help code structuring;
The second one is up to preference, really. :3
Me and my stuff :3True 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.
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

Re: [Just for fun] Roast my code.

Post by Bobble68 »

mr global.png
mr global.png (3.35 MiB) Viewed 190547 times
Azzla wrote: Sun Dec 24, 2023 7:07 am You have no window wrapping so you can type on a line forever, but that's more of a "incompleteness" thing than a code thing.
Also, love.graphics.printf has text wrapping built in, you might want to consider using it instead.
Dragon
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: [Just for fun] Roast my code.

Post by pgimeno »

Globals have two big pitfalls.

First, they are prone to bugs. Consider this situation (very simplified, but hopefully you'll understand how it generalizes):

Code: Select all

function swap_a_and_b()
  tmp = a
  a = b
  b = tmp
end

a = 7
b = 8
tmp = 9
swap_a_and_b()
print(tmp)
You typically expect tmp to still be 9, but being a global, it's been modified by the function that was called before it was used, and the value printed will be 7. This can lead to bugs that are hard to track. Also, namespace pollution is a thing, and one may inadvertently use the same name for two different things, and have one overwrite the other, causing a bug.

The second pitfall is about code readability and organization. It's very difficult to keep track of where globals are initialized or changed, while you're reading a source file trying to track a variable down. It's easier if everything is in locals. An external variable coming from a module, for example, will typically be defined at the top of the file with something like:

Code: Select all

local myVar = require('myModule')
Then while reading the file, you know that if you want to know what myVar is, you just have to go look into myModule.lua.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests