Pretty Lua Code

Hey everyone, so I’m new to GLua. Well, I’m new to Lua in general, but I understand the basics of Lua. So, that being said, I already see a big problem with me: I’m OCD as fuck with my code. Now, I know a lot of programmers are like this but it seems like I can’t get it right. No matter what I do, I always see in my code where I can make it prettier.

So, what defines “pretty” code in Lua? Is it spacing the function declaration/ending two lines from the code within the function? Do I make hook.Add only one line below the function ending? Do I space parenthesis out from what they contain? I mean I just want to know what is pretty and what is not in Lua?

It’s all personal perception and preference. Just compare base code to some gamemode’s code to _Josh ‘Acecool’ Moser code and you will see that almost nothing is unified.

[editline]28th February 2016[/editline]

Except spacing. You should always space/tab blocks of code.

I see, I guess it’s true… Thanks for clarifying that! :slight_smile:

Don’t space your parenthesis and don’t use C-like syntax (make your code syntax error-free in normal lua). Anonymous functions are okay:

hook.Add("Think", "think", function()

Indent to follow scope rules except in the case of

   net.WriteInt(1, 16)

Same goes for things like camera operations or stencils. When you indent, be consistent. Space after every comma. Trailing comma in multiline table definitions:

table = {

Don’t space before function call parenthesis. Space your operators (1 + 2 + (3 - 4)). Space before starting a comment but don’t space after the --. You can use whatever naming scheme you want (because it’s already all over the place), but I recommend alllowercase or lowerCamelCase (I use lowerCamelCase for variables/functions and UpperCamelCase for class functions). Be very careful when writing Derma because that shit will go 15 levels deep.

^ this.

Formalities really slow down the coding process and don’t help readability that much, except for spaces after commas and operators. alllowercase variables are the fastest and easiest to type.

When referring to a player or an entity, always use the same variable name (I use Pl and Ent respectively). Helps when moving code around


You might want to check out (I think there is one for NP++ aswell but i can’t seem to find it) this plugin will basically tell you if your code is ugly and how to “beautify” it.

Oh and you can also read the “official” lua style guide:

This isn’t Java, no reason for camelCase, avoid C style operators, and don’t go crazy with spaces.

I disagree on the camel case point somewhat, the style guide has a good point that lowercase variables should not need multiple words but in the event they do thisVariableDoesThings is far more readable than thisvariabledoesthings

But god do I also hate people that use C style operators and semicolons in Lua for no real reason

But then again lua has functions like isnumber and setmetatable. I personally use camel case with an uppercase first letter, I was more criticizing the lowercase first letter you often see in Java and other languages.

So, these are some examples of how I usually like to make my codes:

print( "Hello World!" )


	print( "Hello World!" )


for k, v in pairs( ass ) do

	print( "I'm tired of typing 'Hello World!'" )


Would these be considered “pretty” to you guys?

I wouldn’t worry about it too much, everyone does it differently and the only time anyone (Acecool) tried to implement universal practices it went… badly! As long as you follow basic practice and your code is readable, you’ll pick up your own ‘style’ as you go

Just keep things consistent, do what works for you and if others are going to be reading or using your code make sure you don’t go full __ACECOOL;

I personally hate excessive amount of spaces between lines. It makes the code look larger than it is. Tabulating every block correctly should be enough to make it readable. Anything else is just a waste of time IMO.

Pretty code is what you think it is.

My definition of pretty code is what suits my eyes the most. I don’t like code clamped up into one place, so I put more spacing than needed.

I would also like to add that I find the way my comments look very important. For example:

Instead of using the traditional "//" or "--", I do my comments like this:

--> Blalalalalala



                      I'm sexy af


Or even 



Please don’t space your parenthesis.

print("Hello World!")

That’s what you think looks good. There is no reason to not space your parenthesis.

I hate when people space parenthesis.
print(“No space, master race.”)

That’s so unnecessary and time consuming that its kinda rediculous.

Sorry for the loss of the 0.3 seconds when spacing your parenthesis…