Programming Styles

As everybody who programs knows, there is a rainbow array of different programming styles. Some are for minimalists, and others are for ‘readability’; or to be almost like reading a book. Although, some are very under developed and lack consistency. Which is why I’m making this thread.

Tell us about your style, why you do it, or the benefits of using it. I’ll share my opinion on semicolons first.

Semicolons

I usually program with Java, so I’m used to using semicolons. I’ll use semicolons in Lua because I’m just so used to it that it happens without me thinking about it. Although semicolons aren’t required in any way when using Lua, I prefer them because they give me a sense of completion in the line. I feel like snippets such as

[lua]local var = 0[/lua]

don’t quite end at zero, but continue on to the wrest of the lines. I know in my head they don’t, but I interpret it as I would other languages I’m more familiar with. (C/C++/Java)

[lua]local var = 0;[/lua]

Ah now it feels complete. You can all complain about how it’s dumb and adds extra bytes to the file size now.

File size in text files (lua) doesn’t really matter unless you got 200,000 lines of code or more.
I’ve seen plenty of styles, one of the styles that I like looking at but not using is putting the first digit of variables value type at the beginning of it’s name, ergo:


local bVar = false
local sVar = "yep"
local iVar = 300
local fVar = 0.03
local tVar = {}

But anyways, I’ve tried 3 styles:
Names, including spaces


local var_name_here = 0

CamelCaps


local VarNameHere = 0

But so far, my favourite is camelBack, because it looks very tidy, can be easily readable and is commonly used in C# programming.


local varNameHere = 0

I use semicolons when onelining several methods at once
I use underscores on functions that I save as “backups” because I’m detouring them

I tab like a fucking bitch. Every new for, if, inline function, or method called with a long line of arguments is given its own tab.

[editline]20th May 2014[/editline]

And I use UpperCamelCase

localvar
CONST_VAR
functionName
{table, entries} and (function, arguments)

no semicolons

lowerCamelCase for variables and function names alike,
UPPER_WITH_UNDERSCORES for constants,
semicolons everywhere.

I am a practitioner of inverted mongoose.


if not self.Panels.Menu then
	local p = vgui.Create( "DFrame" )
	p:SetTitle( "RPG Setup" )
	p:SetDraggable( true )
	p:ShowCloseButton( true )
	p:SetDeleteOnClose( false )
	p:SetSize( ScrW( ) / 2, ScrH( ) / 2 )
	p:Center( )
			
	local sheet = vgui.Create( "DPropertySheet", p )
	sheet:Dock( FILL )

Rules:

No semicolons
Don’t talk about semicolons
Spaces after every delimiter like ( { ,
No non-essential parenthesis
Avoid lambdas - declaring a function is generally better unless you need to encapsulate
No c operators ( ! // /* */ )
Variable names are all lower case, properties and functions are all in proper case.

I’m also the kind of guy who, when you have a bunch of declarations in a row, tabs them all out so that the = and the value line up

Embrace the ~= operator.
Burn the != operator.

I know exactly what you mean. Look at my code on this post here: http://forum.facepunch.com/showthread.php?t=1394975. I have so many unnecessary lines and organization tactics, but I actually have OCD, and that keeps my code tight.

Isn’t != garrycode while ~= standard lua?

Some snippets that should speak for themselves:



if (someCondition () and thisIsABoolean) -- parentheses here for readability
   or (someOtherConditionsOrSomething ()) -- align the conditions
   or (more and (evenMore () or not lessThanBefore ())) -- condition description as comments, if necessary
   or (more and (evenMore32 () or not lessThanBefore2 ()))
then -- gets it's own line if it improves readability
      finallyWeCameToAnEnd ()
end



local data = {
    something = "here and there",
    ["something else"] = "value" -- bad, mixing styles
}


cube:SetPos (pos + Vector (
	m_sin (m_cos (ct) * 4   + ct * 2.2 + i + 90) * 37,
	m_cos (m_sin (ct) * 4.5 + ct * 2.3 + i + 90) * 37,
	m_sin (m_cos (ct) * 5   + ct       + i)      * 37 -- it's all about the alignment
))

No semicolons.
Spaces before/after parenthese pairs, after commas, before/after operators.
No non-essential parenthesis, except for cases where it looks confusing without them.
Lambdas: yes, but if it’s more than a few lines I will probably make it a local function instead and put it right above it.
No C syntax, I want to use 100 % lua without GMod-modified bleh.
Variable names are all lower case (with_underscores_as_spaces), function names are camelCase.
Empty lines: where they make sense and improve readability.

Common variable names:
i(, j) = indices/counters, k = key, v = value
ply = player, ret = return value (in cases I don’t return immediately), typ = type (i.e. string that type() returns)

for k, v in next, TABLENAME do end
instead of
for k, v in pairs(TABLENAME) do end

The following were added by Garry:



/* ... */
// ...
!=
!
&&
||
continue


Semicolons are valid with stock Lua.

I thought continue was Lua 5.2?

Garry’s Mod hasn’t got Lua 5.2 nor does LuaJIT (at least the version Garry’s Mod is using) implement continue.

Don’t use GLua syntax unless you want to limit the tooling that you can work with in your project. As an example, I use the SublimeLinter plugin to let me know when I made a syntax error while writing code. I would have to recompile lauc.exe if I wanted to be able to use GLua syntax, which isn’t worth it and doesn’t provide enough benefits.

Here’s an example of two functions in my admin mod.

http://ss.infd.us/linux/2014-05-20@23-45-49.png

  • No semicolons.

Ever.

  • No parenthesis around if statements: rather than if (a == b) then, I use if a == b then
  • No “airy” parenthesis
  • All lua operators, no C.
  • Comments on the same line as code, it helps to clarify what the comment is describing. If it’s summarising a function, neatly before the function, or “section” of code.
  • Lowercase variable names.
  • Empty lines between functions.
  • One “tab” per function block, not two.
  • One space after commas, one space before and after operators.
  • No lambdas unless absolutely necessary.
  • Function names CamelCase.if (a == b) then

I like some of your styles. I think I’m going to adopt some of the more strict Lua styles, which is to have no semicolons, and only use vanilla Lua operators.

I do like the /* */ comments though, looks better imo and easier to write. Also what are replacements for !/&&/||?

&& = and
|| = or



if x and y then
    ...

if x or y then
    ...


Of course, but there’s no symbols or anything? I prefer and/or even so - just wondering

Nope