• Programming Niches
    69 replies, posted
  • I never use comments for commenting, only for debugging when I need to stop a line of code from executing but don't want to delete it as I don't know if this is the line causing the issue or not. i'm a naughty scripter :(
  • Avatar of SiPlus
  • Better place for this thread: [url]http://facepunch.com/forums/240[/url]
  • But this thread is about lua, and this is the lua forum? In large part anyway. The OP does have multiple languages but most people seem to be hitting up the lua snippets.
  • Avatar of Drakehawke
  • [QUOTE=Splambob;35953268] In large part anyway. The OP does have multiple languages but most people seem to be hitting up the lua snippets.[/QUOTE] Probably because it's in the Lua forum. [editline]14th May 2012[/editline] [QUOTE=_Kilburn;35949369]I use that to distinguish important comments from simple comments. Sometimes when your code is getting really huge, you just need a way to separate it into several parts, and making huge eye catching comments is one of the ways to easily find your way in the code.[/QUOTE] I'd just use ctrl + f personally.
  • Avatar of FPtje
  • My Lua scripting style has changed over the years. Some things I'd like to note is that I've seen the virtues of preventing sidescrolling lines, although I still make some mistakes because I have word wrapping on :). Someone once told me that the Lua operators (~=, not, etc.) were ugly and that I should use Garry's C-style syntax implementation instead. The only C-style thing I really use in Lua are the /* */ comments because -[[]] is a bitch to type. Oh and I use // (and /**/) to comment code out, but I use -- to provide line to line explanations. For function headers and sections I've got a snippet in sublime: / * <tab> produces this: [lua]/*--------------------------------------------------------------------------- Title ---------------------------------------------------------------------------*/[/lua] With the "Title" text selected. This is useful because it gives you a nice block comment which is consistent with all the other block comments. (in the amount of dashes :v:) Edit: I just remembered I maintain the GLua highlighter for sublime. The comment snippet is in it :v:
  • Java itself has a coding style you should follow [url]http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141855.html#11684[/url]
  • Avatar of DarkShadow6
  • [QUOTE=_Kilburn;35937239]I like your programming style, it looks exactly like mine. [/quote] Orly. How odd. [QUOTE=_Kilburn;35937239]There's just a major flaw in my coding, I always forget to use a global table to store my variables. I know that's what I should do but I just keep forgetting it and just declare ugly global variables everywhere. [/quote] It happens to the best of us. When I started programming Lua, it was on that now-shitty game (not saying it wasn't shitty before, because it kinda was) ROBLOX. I was so terrible at Lua for the first year or so I used it. Then I became the god of all things ROBLOX. ... Then the devs went and broke the game even more. Then I ragequit. I'll spare you the tl;dr story behind that. [QUOTE=whitespace;35937312]I have a pretty inconsistent style, but I think it's mostly fine. I like to use the Lua comments and operators as well.[/QUOTE] [QUOTE=Banana Lord.;35937694]as I messed with GLua more my style changed a lot, now it's fairly consistent[/QUOTE] More examples of code I do not like. It may be consistent, but it just looks all floppy and ugly to me because of the whitespace and parenthesis spam. [QUOTE=Splambob;35939227]The more I use Lua, the less my code looks like Java which is my other strong language. [/quote] Java is my other language too (I mean, I can use a lot of languages if I need to, but I'm best at Lua and Java). [QUOTE=Splambob;35939227]It sucks though because when I go back to coding in Java I start getting annoyed because it simply isn't as flexible as Lua[/quote] Java was made to be extendable and powerful but still simple to use. Sadly, because of this, and because of the fact that it also has a lot of pre-built security measures to keep your programs safe from attack, it is missing a lot of possibly useful features. Like metatables. If Java ever got anything similar to metatables for classes, __add, __sub, __div, __mul, __index, etc, I would totally throw away Lua. [QUOTE=Splambob;35939227]With that said, I'm finding it hard to let go of the C-style operators because they feel nicer to use.[/quote] And that's your opinion. If that's what you like to do, go for it. I don't really like them, though. I want my Lua to be Lua :U [QUOTE=Splambob;35939227]Anyway my latest thing has been to start abusing tables because they're the most useful data structures I've ever come across. Assign anything to ANYTHING? I can pretend that it's an OBJECT?! AND I get to use it like an array too?! I'LL TAKE 20. [/quote] When I found out about tables... Oh, I had the most wonderful time finding out what they did. [QUOTE=James xX;35939982] I put all my functions in a local table, and when I need to use the functions, it returns a copy of that table.[/QUOTE] Well, yeah, you can do that in some instances and have it work totally fine. There are places where this is a better practice. Then again, you could always just make those functions into a module, or put them into a global table instead of returning a new local instance of the table every time. That's kind of a memory eater there. [QUOTE=Sigma7;35942280]I hate it when people don't leave comments explaining what a function does. Seriously, its so basic that everyone should know to do it but it seems like some people don't.[/QUOTE] Why should we? I'm programming so things will work, not so nobos can come along and try to learn from my code or something. And if people want to extend my code, they could always just ask me. :\ [QUOTE=James xX;35942947][I]Explain why you rate someone dumb.[/I][/QUOTE] For some reason there's boxes flying around this place like flies. I find it funny how people that rated the OP dumb still posted here. [QUOTE=whitespace;35942993] You can also name your functions so that by looking at the name you get the general idea what it does. Some of the more cluttered stuff inside the function can then be commented to explain what that part does.[/QUOTE] I always do that. [QUOTE=Ylsid;35950685]I never use comments for commenting, only for debugging when I need to stop a line of code from executing but don't want to delete it as I don't know if this is the line causing the issue or not.[/QUOTE] Agree'd. [QUOTE=Splambob;35953268]But this thread is about lua, and this is the lua forum?[/QUOTE] I put this here because I wanted to see your people's Lua styles. [QUOTE=Drakehawke;35953934]ctrl + f[/QUOTE] This. [QUOTE=FPtje;35954079]-snip-[/QUOTE] Interesting. You are very thorough in your ways of the Luas. I've seen your code; you are very consistent and your code is relatively clean, though you use some things I don't like, of course :u [QUOTE=Map in a box;35954260]Java itself has a coding style you should follow [url]http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141855.html#11684[/url][/QUOTE] Implying conventions are law. I still use my own-ish style. [editline]14th May 2012[/editline] tl;dr
  • Avatar of dylanb5123
  • [lua] hook.Add("CalcView", "TopDownView", function(ply, pos, angles, fov) local view = {} view.origin = pos+Vector(0,0,900) view.angles = Vector(90,0,0) view.fov = fov return view end) hook.Add("ShouldDrawLocalPlayer", "DrawLocalPlayer", function() return true end) hook.Add("Think", "ClampViewAngles", function() local ang = LocalPlayer():GetAimVector():Angle() LocalPlayer():SetEyeAngles(Angle(0, ang.Yaw, ang.Roll)) end) hook.Add("HUDPaint", "TPSDisplay", function() if !LocalPlayer():Alive() then return end local AIM_COLOR = Color(255,255,255,255) surface.SetDrawColor(AIM_COLOR) local ang = LocalPlayer():GetAimVector() local startpos = (LocalPlayer():GetShootPos()+(ang*50)):ToScreen() local endpos = (LocalPlayer():GetShootPos()+(ang*250)):ToScreen() local circlepos = (LocalPlayer():GetShootPos()+(ang*257)):ToScreen() local reticlepos = LocalPlayer():GetEyeTrace().HitPos:ToScreen() surface.DrawLine(startpos.x, startpos.y, endpos.x, endpos.y) surface.DrawCircle(circlepos.x, circlepos.y, 7, AIM_COLOR) surface.DrawCircle(circlepos.x, circlepos.y, 1, AIM_COLOR) surface.DrawCircle(reticlepos.x, reticlepos.y, 7, AIM_COLOR) end) [/lua] Theres my most recent script... Hope it reflects my style of coding. I'm not a fan of comments usually. Hope this thread is still alive.
  • Avatar of FPtje
  • [QUOTE=dylanb5123;35967905][lua] Code [/lua] Theres my most recent script... Hope it reflects my style of coding. I'm not a fan of comments usually. Hope this thread is still alive.[/QUOTE] I tend to put enters to separate variable declarations and programming logic, but I'm fine with reading code that doesn't have that :)
  • Avatar of dylanb5123
  • [QUOTE=FPtje;35968245]I tend to put enters to separate variable declarations and programming logic, but I'm fine with reading code that doesn't have that :)[/QUOTE] I'm a big fan of really compact scripts
  • Avatar of DarkShadow6
  • [QUOTE=dylanb5123;35969180]I'm a big fan of really compact scripts[/QUOTE] Yeah obviously. I like your code, besides the fact that you don't space your arithmetic [del]and commas[/del] I am the master of not reading things.
  • Avatar of daranable
  • 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: [url]https://github.com/daranable/Data-Signals/blob/master/lua/datasignal.lua[/url] 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.
  • Avatar of Kogitsune
  • 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: [lua] 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[/lua] 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: [code]eff = EffectData( ) eff:SetOrigin( tr.HitPos ) eff:SetStart( tr2.HitPos ) util.Effect( "lmage_bolt", eff )[/code] It keeps a flow to my code I guess - I probably only do it out of habit.
  • Avatar of KatNotDinner
  • [QUOTE=Kogitsune;35985763]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: code 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: [code]eff = EffectData( ) eff:SetOrigin( tr.HitPos ) eff:SetStart( tr2.HitPos ) util.Effect( "lmage_bolt", eff )[/code] It keeps a flow to my code I guess - I probably only do it out of habit.[/QUOTE] Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.
  • [QUOTE=daranable;35970534]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: [url]https://github.com/daranable/Data-Signals/blob/master/lua/datasignal.lua[/url] 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.[/QUOTE] your code is beautiful and you should feel beautiful
  • Avatar of FPtje
  • [QUOTE=KatNotDinner;35989232]Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.[/QUOTE] [lua] for k,v in pairs(player.GetAll()) do if v == LocalPlayer() then continue end -- Do stuff for every player except the LocalPlayer here. end [/lua] It looks nicer than: [lua] for k,v in pairs(player.GetAll()) do if v ~= LocalPlayer() then -- Do stuff for every player except the LocalPlayer here. end end [/lua]
  • Avatar of Drakehawke
  • [QUOTE=KatNotDinner;35989232]Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.[/QUOTE] It's similar to "break", if you have "break" in a loop then it breaks out of the loop where it is and carries on with the rest of the code, "continue" will tell Lua to skip the current iteration, but NOT cancel the entire loop, so it just continues to the next iteration. For example: [lua] for i = 1, 10, 1 do if i ~= 2 then print( i ) end end [/lua] is the same as [lua] for i = 1, 10, 1 do if i == 2 then continue end print( i ) end[/lua] 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
  • [QUOTE=KatNotDinner;35989232]Kinda off-topic but can you show me how "continue" is used? I've heard it exists in GMod lua bit I've never seen it in use.[/QUOTE] It jumps to the next item in a loop. [lua] 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 [/lua] [editline]17th May 2012[/editline] Wow, I'm late.
  • Avatar of _nonSENSE
  • [QUOTE=whitespace;35991314]Since when has 'continue' existed in gmod?[/QUOTE] [B][URL="http://www.facepunch.com/threads/1008655"]A while.[/URL][/B]
  • Avatar of Averice
  • I always use parenthesis in my if statements, even when it's only a boolean like: [lua] if( something ) then -- Do somehting. end [/lua] 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.
  • Avatar of JustSoFaded
  • That's exactly how I do it as well Averice. I also use the semicolon. [lua] function test() if( bool ) then FunctionCall(var1, var2, var3); end end [/lua]
  • Avatar of FPtje
  • I personally don't use them because I believe they are redundant characters. :) Edit: 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.
  • Avatar of daranable
  • [QUOTE=dylanb5123;36012600]...Ewwww[/QUOTE] Heh, I don't know why people hate semi-colons. They are slightly annoying to get used to. But personally now that I am getting more used to them because of JavaScript and Java I actually am starting to prefer them. 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.
  • Avatar of dylanb5123
  • [QUOTE=daranable;36012676]They are slightly annoying to get used to.[/QUOTE] Exactly lol
  • Avatar of Divran
  • Made even more annoying by editors that tell you exactly where you're missing semicolons, but can't place them there for you.
  • Avatar of Bobblehead
  • [lua] --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))) [/lua]
  • Avatar of Bynari
  • 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. [lua] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function rollFace() azwazwazwazwazwazwazwl.wwwwrcfgvbujhl; mklrtfuyjh tttttgyuhujhyieacwgdhrfmbgiyh,upk;leacvsghdnvkhm,jlm xdcfgvecbhcrjntvuyn78,l8p2olnl;mnp889mnklnaika; end [/lua] 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. [lua] AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function slamFace() jijbniitbunkjunfgbnkjbnkutbufntkufnktbunkfjbndklklfnkbjnkftn olhmnlfkthmnflhtjnfljkbnkdndhkdjhndkuhndktjnkdfhndfkcjkbn ddrtjkndklhjnldfhkjljjndhlndfhjnjni d uidthjidjnidh dghedghjiodfh end [/lua] On an interesting side note- when I run these scripts through gmod, the console errors tend to spam Katy Perry lyrics.