I like your code, besides the fact that you don't space your arithmetic
and commasI am the master of not reading things.
Heh, I am a bit obsessive when it comes to my coding style.
Here is a good example of how I format code that I intend to release at some point:
My biggest difference from most people. Is if I intend to release code, I obsessively use luadoc comments to document my functions. (I realise I missed one on the script I linked to, but meh.)
And I am in the same boat as op in using only lua syntax for writing lua. I never understood why we got special language syntax for GMLua.
Other than that, I will generally adjust my coding style to the project I am working on, so that the project stays internally consistent.
I don't like most of the c-style features and stick to standard Lua whenever possible, except continue since that's dead useful.
I also prefer to use plenty of space in my code since it looks cleaner to me:
local function HUDPaint( ) surface.SetMaterial( Head ) local index, pl, x, y, w, h, r, n, hp, j, k, j2, k2, wep, str pl = LocalPlayer( ) if not IsValid( pl ) then return end wep = pl:GetActiveWeapon( ) hp = math.Clamp( pl:Health( ), 0, 999 ) index = math.Clamp( math.ceil( ( hp ) / 20 ), 0, 5 ) if pl:Health( ) > 0 and Slot == "Dead" then Slot = "Center" end if pl:Health( ) <= 0 and Slot ~= "Dead" then Slot = "Dead" end r = ScrW( ) n = ScrH( ) --Draw face x = r / 2 - Coords[ index ].q / 2 y = n - Coords[ index ][ Slot ].h Poly[ 1 ].x = x Poly[ 1 ].y = y Poly[ 1 ].u = Coords[ index ][ Slot ].x Poly[ 1 ].v = Coords[ index ][ Slot ].y Poly[ 2 ].x = Poly[ 1 ].x + Coords[ index ][ Slot ].w Poly[ 2 ].y = Poly[ 1 ].y Poly[ 2 ].u = Coords[ index ][ Slot ].x2 Poly[ 2 ].v = Coords[ index ][ Slot ].y Poly[ 3 ].x = Poly[ 2 ].x Poly[ 3 ].y = y + Coords[ index ][ Slot ].h Poly[ 3 ].u = Poly[ 2 ].u Poly[ 3 ].v = Coords[ index ][ Slot ].y2 Poly[ 4 ].x = Poly[ 1 ].x Poly[ 4 ].y = Poly[ 3 ].y Poly[ 4 ].v = Poly[ 3 ].v Poly[ 4 ].u = Poly[ 1 ].u
I prefer to declare all the locals I am going to be using at the top of the current scope - this helps me keep track of which variables I am actually using and helps me keep my train of thought on the rails.
I dislike creating lambdas and prefer to create a named local function - this is more one of those stupid micro-optimization things since you end up with less generated garbage.
I extremely dislike creating throw-away tables for single-instance things and will re-use them as much as possible when it would make sense. If I'm going to be doing a lot of traces, I'll only use one trace setup table. If I'm going to be using DrawPoly or CalcView I create a single table for them. Obviously there's cases where I can't really do that ( when they need to be distinct ), but this greatly enhances performance.
For some reason, I indent code that would be wrapped with a Using block in C# / VB:
It keeps a flow to my code I guess - I probably only do it out of habit.Code:eff = EffectData( ) eff:SetOrigin( tr.HitPos ) eff:SetStart( tr2.HitPos ) util.Effect( "lmage_bolt", eff )
for k,v in pairs(player.GetAll()) do if v == LocalPlayer() then continue end -- Do stuff for every player except the LocalPlayer here. end
It looks nicer than:
for k,v in pairs(player.GetAll()) do if v ~= LocalPlayer() then -- Do stuff for every player except the LocalPlayer here. end end
for i = 1, 10, 1 do if i ~= 2 then print( i ) end end
is the same as
for i = 1, 10, 1 do if i == 2 then continue end print( i ) end
Output 1, 3, 4, 5, 6, 7, 8, 9, 10
You can get around without it, but you can imagine if you had a large number of conditions to check on each iteration of a loop, it could get very complicated as you can't simply "return" or "break" as then all the subsequent iterations wouldn't run (you could also end up with a large amount of tabs before each line and it generally just wouldn't look very clean).
Edit: ninja'd :c
for _, ply in ipairs(player.GetAll()) do -- Skip this player if his health is less than 50. if ply:Health() < 50 then continue end print(ply:Nick() .. "'s health is over 50.") end
Wow, I'm late.
Since when has 'continue' existed in gmod?
I always use parenthesis in my if statements, even when it's only a boolean like:
if( something ) then -- Do somehting. end
I don't know when I started doing it but I've gotten used to it and I like it, I know a lot of people don't like that sort of style but I'm not going to change just to please someone else.
That's exactly how I do it as well Averice. I also use the semicolon.
function test() if( bool ) then FunctionCall(var1, var2, var3); end end
I personally don't use them because I believe they are redundant characters. :)
Lately I've been noticing that I comment my code a lot. I'm noticing it in my C# exercises in uni, but also in my Lua things.
To me it feels like I have more control over the end of a statement.
I only use them in Lua projects I am the lead on.
But if I am in a project that doesn't use them like I said early, I mold to keep consistent with said project.
Made even more annoying by editors that tell you exactly where you're missing semicolons, but can't place them there for you.
--I've noticed that some people do it like this: print( "hello." ) function( name, ply ) self:SetVector( ( Vector( 36, 5, 122 ) * Vector( 323, 232, 21 ) ) ) --I used to do this, but then I realized that it was a waste of finger energy. :P --Now I do this: print("hello") function(name, ply) self:SetVector((Vector(36, 5, 122) * Vector(323, 232, 21)))
My preferred method is a tad unorthodox, I'll admit, but when I'm coding I like to roll my face across the keyboard. Not half-assed, either; a nice full ear to ear roll.
AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function rollFace() azwazwazwazwazwazwazwl.wwwwrcfgvbujhl; mklrtfuyjh tttttgyuhujhyieacwgdhrfmbgiyh,upk;leacvsghdnvkhm,jlm xdcfgvecbhcrjntvuyn78,l8p2olnl;mnp889mnklnaika; end
I also sometimes find it refreshing (especially when I've hit a particularly difficult problem) to pick up my keyboard and repeatedly bash my forehead into the keys.
AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function slamFace() jijbniitbunkjunfgbnkjbnkutbufntkufnktbunkfjbndklklfnkbjnkftn olhmnlfkthmnflhtjnfljkbnkdndhkdjhndkuhndktjnkdfhndfkcjkbn ddrtjkndklhjnldfhkjljjndhlndfhjnjni d uidthjidjnidh dghedghjiodfh end
On an interesting side note- when I run these scripts through gmod, the console errors tend to spam Katy Perry lyrics.
It drives me insane when people use loads of spaces instead of tabs >_>
It drives me insane when people do this stupid shit.
It's really annoying when you are trying to modify or fix someone elses script.
The code is ridiculous as well, rather than looping through the player.GetAll() table, it checks the owner of every entity. Why not just make a for loop that goes from 1 to a million, picks a random player, check the name and kick if it's equal to "ChickenFriendSteak"?
Seriously though, is this really someone's code?
Lately I've been seeing more and more beginners tabbing like that.
Expression 2 has auto tabbing. I've seen people post E2 code where they either hit backspace to delete the tabs, or go to the settings menu to uncheck the auto tabbing just so their code can look like shit.