Page 1 of 1

Where could I have someone read through my code?

Posted: Wed Oct 15, 2014 10:58 pm
by jumpsplat120
TL;DR learned Love and Lua at the same time and code is terrible, but I can't figure out how to rewrite it.

A long while ago, like 3 or so months ago I started trying to learn java so I could learn how to code mods for Minecraft, because I had all these cool ideas and people kept telling me they were impossible. Now, I'm never one to believe something isn't doable until I give it a shot so I tried to learn Java, but I couldn't get a grasp of it because people kept skipping stuff in all the tutorials I was watching. Then, while browsing through tutorials I found a tutorial for lua, which looked a million times easier for me to read and understand, so I went at starting to learn it. For the most part, I was watching one set of tutorials and I thought I had it down pretty well, but I figured the best way for me to practice what I've learned was to apply it to something. I started a game that I wanted to make called "The Spaceist", a mix between FTL, DnD and a choose your own adventure book. After coding this for a while (it was all in a console, looked very old school) someone mentioned that it would be cool if my game was more visual, so I started looking for a way to make my game more of a 2d type game, when I found love. I started to learn Love, and started coding the game from scratch, again, learning as I went. Now months later, having learned both more lua and more Love, I realize that a lot of my old code is probably REALLY bad, but when I look at it, it all just seems familiar and correct and I can't really see any other way to do it. Is there anywhere/anyone who would be willing to go over it? Possibly a website or something along those lines? Also, hi everyone, first post!

Re: Where could I have someone read through my code?

Posted: Thu Oct 16, 2014 6:49 am
by veethree
Upload your .love here and i'm sure someone would be willing to look over it and give you some tips.

Re: Where could I have someone read through my code?

Posted: Thu Oct 16, 2014 7:41 am
by Plu
For larger projects, dropping it here is probably a good idea :)

Re: Where could I have someone read through my code?

Posted: Thu Oct 16, 2014 7:45 pm
by jumpsplat120
Alright, here it is. Also, at some point between then and now I seem to have messed up movement, and I don't know how that happened. 0.o

Also, it's too big to upload, so I'm putting it on git. It's uploading now, should only take a minute.

https://github.com/jumpsplat120/The-Spaceist

Re: Where could I have someone read through my code?

Posted: Thu Oct 16, 2014 9:02 pm
by nice
Here's some books about lua:
http://www.lua.org/pil/

Re: Where could I have someone read through my code?

Posted: Thu Oct 16, 2014 11:58 pm
by notencore
Nothing too serious, just did a quick browsing, but you use a lot of abbreviations with letters just smashed together and almost no comments to back them up. :\

So, just a minor suggestion, try expanding your variable names (drawks1 to kestral.draw[1], etc). I also noticed a bunch of function calls (testKestral) that I can't find any declarations for.

Just some quick tips, and I'm no expert, everyone has their own preferences so go with what feels natural to you! :D

Re: Where could I have someone read through my code?

Posted: Fri Oct 17, 2014 3:57 am
by Positive07
I have a code that will surely help you:

Code: Select all

function (code)
    if code.finished ~= false then
         if code.buggy then
              return correct(code)
         end
         return code
    else
        if wantToImprove(code) then
            return improve(code)
        end
        newProject.begin()
        return code
    end
end
Take into account that the functions correct() and improve() takes some time to execute so if you want to begin project its not recommended to call correct(), also wantToImprove takes into account some other variables like code.looksRight, code.works and some other things.

In other words, you dont need to write your code again because its ugly or unreadable, if it works or you are familiar with it then leave it like that... If you cant find a bug it may be better to have a readable code, but if you are familiar with it then you can find the problem most of the times.
If you have already finished, it works and you dont have some other project in mind, and you want to improve the way you wrote your code then you can rewrite it (you probably wont though)

Re: Where could I have someone read through my code?

Posted: Fri Oct 17, 2014 7:36 am
by jumpsplat120
Thanks everyone for the advice! It really does help, and I guess I'll continue with what I'm doing for now, trying to learn and make a game simultaneously. We'll see how it works. :D

Re: Where could I have someone read through my code?

Posted: Fri Oct 17, 2014 11:31 am
by undef
Just looked over it a bit.
A few advices:

1. Indentation
Sometimes you use it, sometimes you don't, that makes your code harder to read.
Always indent correctly.

2. Avoid updating Data that does not need updating

Code: Select all

WindowWidth = love.graphics.getWidth()
WindowHeight = love.graphics.getHeight()
musicVolume = love.audio.getVolume()
This is in your update function, even though these values probably never change. And if they do, you should know exactly when.
There are probably a lot more cases like that in the code, but now you know what to look for.

3. "Boolean hell"
If you need too many booleans to describe something, there is probably a more elegant description.
One technique you might not be familiar with is switching out functions.
This can be very useful and make your code shorter, easier to read and more efficient.
A simple example:

Code: Select all

function love.update(dt)
    state.update(dt)
end

menuState = {
   update = function() <...> end
   draw = function() <... end
end

ingameState = {
   update = function() <...> end
   draw = function() <... end
end

someOtherState = {
   update = function() <...> end
   draw = function() <... end
end
And then you just do state = menuState, or state = someOtherState on certain events.
This should eliminate a lot of your if-statements.

4. Writing the same code multiple times
This should not happen.
Like your function "drawOptions" in menu.lua at line 216...
There are many many ways to to this in a better way.
The simplest improvement would be to take all of the code out of the if-statement that is the same for all cases.
However in this case all you need to do is create a string from a number and use it in the print function.
(This string: "Music Volume: ! ! ! ! ! ! ! ! ! !" )
This string could be generated using a simple function, taking the musicVolume as a parameter and returning the string.
(Good exercise)

5. No hardcoding!
A lot of times you use absolute values in your coordinates, like "WindowHeight - 150".
This will look very differently on different resolutions.
Always use values relative to your screen size, like "3/4*WindowHeight".


So yeah, there is plenty to improve.
There is probably a lot more, because I just briefly looked at main.lua and menu.lua

What could also help you a lot is learning more lua "vocabulary", there are a lot of useful functions you probably don't know, or are not familiar using.
These functions could make your code much conciser and solutions to certain problems much easier.
They take time to get used to though, so I recommend just fooling around with them a bit from time to time in small scripts, just to remember them when you really need them.



Anyway, I'm glad you want to improve your code and not just "get it done somehow" like a lot of programmers do.
Code quality is not just important for the coder, but for all of us. Even if he's the only one who ever reads it.
Happy refactoring!

Re: Where could I have someone read through my code?

Posted: Fri Oct 17, 2014 6:56 pm
by Positive07
undef wrote: Anyway, I'm glad you want to improve your code and not just "get it done somehow" like a lot of programmers do.
Code quality is not just important for the coder, but for all of us. Even if he's the only one who ever reads it.
Happy refactoring!
Yeah that is really true, its good to know that your code can be improved, but it's not something you do the first time you write code, is something that you get better at by programming, you are not forced to improve but if you know that making readable code is better then you will probably get to it.


To expand on undef, switching is amazing it can help A LOT!! I do it with tables too, for example:
Some times you will need to do:

Code: Select all

love.keypressed = function(k,r)
    if k == "a" then
         <...>
    elseif k == "b" then
         <...>
     ....
    elseif k == "z" then
        <...>
    end
end
I think that is better to have a table holding the actions for each key like this

Code: Select all

keypress = {
    a = function () <...> end;
    b = function () <...> end;
    ....
    z = function() <...> end
}
love.keypressed = function (k,r)
    keypress[k]()
end
This also allows you to change the behavior of keys really easily

Code: Select all

keypress.a = newAbahavior
Or even the whole table

Code: Select all

keypress = newTable
Also if you have a button in your game that does the same thing that a keypress, you can do

Code: Select all

keypress.a()
And you dont have to rewrite the code again