• Programming Styles
    96 replies, posted
[QUOTE=Robotboy655;44880843] ... For C style functions/ifs [code] int function something() { // Always put the { here, no need to waste a line on it } if ( someCondition ) { // Do shit } else { // Do something else } [/code][/QUOTE] Wait, can I declare functions and if statements like that in GMod Lua?
-snip-
[QUOTE=Zignd;44880919]Wait, can I declare functions and if statements like that in GMod Lua?[/QUOTE] No, it clearly states, for C style functions/ifs.
I really like to align things between similar lines. [code] surface.CreateFont("fontA", { font = "Impact", size = 16, weight = 700, blursize = 0, scanlines = 0, antialias = true, underline = false, italic = false, strikeout = false, symbol = false, rotary = false, shadow = false, additive = false, outline = false, })[/code] I also leave commas on the last item, because it doesn't hurt.
Here is my style, the code is taken from my SNPC base: [lua] --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:Think() if self.DisableWeapons == false && GetConVarNumber("vj_npc_noweapon") == 0 && self:GetActiveWeapon() != NULL then -- When the SNPC has a weapon then... if self.DisableUSE_SHOT_REGULATOR == false then if GetConVarNumber("vj_npc_nouseregulator") == 0 then if self:GetActiveWeapon():GetClass() == "weapon_pistol" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_shotgun" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_crossbow" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_annabelle" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end end end if self:GetActiveWeapon():GetClass() == "weapon_smg1" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_ar2" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_vj_ak47" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self.AccuracyPoor == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_POOR ) end if self.AccuracyAverage == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_AVERAGE ) end if self.AccuracyGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD ) end if self.AccuracyVeryGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_VERY_GOOD ) end if self.AccuracyPerfect == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_PERFECT ) end end end --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:FootStepSoundCode() if self.HasSounds == true then if GetConVarNumber("vj_npc_sd_footstep") == 0 then if GetConVarNumber("ai_disabled") == 0 then if self:IsOnGround() && self:IsNPCMoving() then if CurTime() > self.FootStepT then local randomfootsteps = math.random(1,6) if randomfootsteps == 1 then self:EmitSound( self.FootStep1,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 2 then self:EmitSound( self.FootStep2,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 3 then self:EmitSound( self.FootStep3,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 4 then self:EmitSound( self.FootStep4,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 5 then self:EmitSound( self.FootStep5,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 6 then self:EmitSound( self.FootStep6,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) end if self:GetEnemy() != nil then self.FootStepT = CurTime() + self.FootStepTimeAlert else self.FootStepT = CurTime() + self.FootStepTimeIdle end end end end end end end [/lua]
[QUOTE=vrej;44882000]Here is my style, the code is taken from my SNPC base: [lua] --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:Think() if self.DisableWeapons == false && GetConVarNumber("vj_npc_noweapon") == 0 && self:GetActiveWeapon() != NULL then -- When the SNPC has a weapon then... if self.DisableUSE_SHOT_REGULATOR == false then if GetConVarNumber("vj_npc_nouseregulator") == 0 then if self:GetActiveWeapon():GetClass() == "weapon_pistol" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_shotgun" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_crossbow" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_annabelle" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end end end if self:GetActiveWeapon():GetClass() == "weapon_smg1" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_ar2" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_vj_ak47" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self.AccuracyPoor == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_POOR ) end if self.AccuracyAverage == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_AVERAGE ) end if self.AccuracyGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD ) end if self.AccuracyVeryGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_VERY_GOOD ) end if self.AccuracyPerfect == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_PERFECT ) end end end --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:FootStepSoundCode() if self.HasSounds == true then if GetConVarNumber("vj_npc_sd_footstep") == 0 then if GetConVarNumber("ai_disabled") == 0 then if self:IsOnGround() && self:IsNPCMoving() then if CurTime() > self.FootStepT then local randomfootsteps = math.random(1,6) if randomfootsteps == 1 then self:EmitSound( self.FootStep1,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 2 then self:EmitSound( self.FootStep2,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 3 then self:EmitSound( self.FootStep3,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 4 then self:EmitSound( self.FootStep4,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 5 then self:EmitSound( self.FootStep5,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 6 then self:EmitSound( self.FootStep6,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) end if self:GetEnemy() != nil then self.FootStepT = CurTime() + self.FootStepTimeAlert else self.FootStepT = CurTime() + self.FootStepTimeIdle end end end end end end end [/lua][/QUOTE] Is there such a thing as Post-Traumatic Code Disorder? Why do people nest if statements after if statements after if statements? I can't even read that first chunk of code.
[QUOTE=Revenge282;44882086]Is there such a thing as Post-Traumatic Code Disorder? Why do people nest if statements after if statements after if statements? I can't even read that first chunk of code.[/QUOTE] I AM SORRY ='( EDIT: No but for real the reason why I do that is to stop people from stealing my code. I already had too much theifs on my code, after I started coding like this, it stopped the theifs!
[QUOTE=vrej;44882000]Here is my style, the code is taken from my SNPC base: [lua] --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:Think() if self.DisableWeapons == false && GetConVarNumber("vj_npc_noweapon") == 0 && self:GetActiveWeapon() != NULL then -- When the SNPC has a weapon then... if self.DisableUSE_SHOT_REGULATOR == false then if GetConVarNumber("vj_npc_nouseregulator") == 0 then if self:GetActiveWeapon():GetClass() == "weapon_pistol" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_shotgun" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_crossbow" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_annabelle" then self:CapabilitiesAdd( CAP_USE_SHOT_REGULATOR ) end end end if self:GetActiveWeapon():GetClass() == "weapon_smg1" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_ar2" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self:GetActiveWeapon():GetClass() == "weapon_vj_ak47" then self:CapabilitiesRemove( CAP_USE_SHOT_REGULATOR ) end if self.AccuracyPoor == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_POOR ) end if self.AccuracyAverage == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_AVERAGE ) end if self.AccuracyGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_GOOD ) end if self.AccuracyVeryGood == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_VERY_GOOD ) end if self.AccuracyPerfect == true then self:SetCurrentWeaponProficiency( WEAPON_PROFICIENCY_PERFECT ) end end end --------------------------------------------------------------------------------------------------------------------------------------------- function ENT:FootStepSoundCode() if self.HasSounds == true then if GetConVarNumber("vj_npc_sd_footstep") == 0 then if GetConVarNumber("ai_disabled") == 0 then if self:IsOnGround() && self:IsNPCMoving() then if CurTime() > self.FootStepT then local randomfootsteps = math.random(1,6) if randomfootsteps == 1 then self:EmitSound( self.FootStep1,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 2 then self:EmitSound( self.FootStep2,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 3 then self:EmitSound( self.FootStep3,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 4 then self:EmitSound( self.FootStep4,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 5 then self:EmitSound( self.FootStep5,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) elseif randomfootsteps == 6 then self:EmitSound( self.FootStep6,self.FootStepVolume,math.random(self.FootStepPitch1,self.FootStepPitch2)) end if self:GetEnemy() != nil then self.FootStepT = CurTime() + self.FootStepTimeAlert else self.FootStepT = CurTime() + self.FootStepTimeIdle end end end end end end end [/lua][/QUOTE] No offense but you need to rework your base if it's that messy. Learn how to use a conversion table and it will look A LOT nicer.
I hate the spacing in the code shipped with GMod... all of the extra spaces, line breaks and brackets annoy the piss out of me. Also there's a lot of places in the code where re-organizing the if-statements cuts out a lot of switch cases... but I mean when I see this: [lua]if ( camera.controlkey && camera.controlkey == key ) then[/lua] my first instinct is to do this [lua]if camera.controlkey and camera.controlkey == key then[/lua] although I do understand that in C<whatever> the brackets are necessary and that's where they come from, but when you see like a wall of statements and they all look like that...
[QUOTE=brandonj4;44882229]No offense but you need to rework your base if it's that messy. Learn how to use a conversion table and it will look A LOT nicer.[/QUOTE] Well if I am going to upload something on the workshop, I am going to make it look messy and unreadable as much as I can. This isn't the real way I code. This is just to stop those thieves, since I had few people stealing from me and I the only solution that I found is making my code messy.
There is one situation in which I use semicolons, and that's multi-line tables. It's completely legit Lua and I think it's a better way to end a line than a comma. If no separator was needed then I wouldn't use any, but since it has to be either commas or semicolons, I choose semicolons. No comma on the last line, OCD nightmare: [lua]local tbl = { a = 1, b = 2, c = 69, d = 633 }[/lua] Comma on every line including the last line, looks plain weird: [lua]local tbl = { a = 1, b = 2, c = 69, d = 633, }[/lua] Semicolons: [lua]local tbl = { a = 1; b = 2; c = 69; d = 633; }[/lua] Semicolons anywhere else are still heresy though. :v:
Brainfuck coding [lua] local ca = LocalPlayer():GetNWInt("GryEnergy") * ((EyeFinityScrW()/8.97)/100) local caa = ( LocalPlayer():GetNWInt("GryEnergy")- (c-(LocalPlayer():GetNWInt("GryEnergy") * 2.14))) local ololola = ((EyeFinityScrW()/8.97) - ca) / 2 local aa = EyeFinityScrW() - (EyeFinityScrW()/5.69) + ololola local ba = ScrH() - (94 * (90 + 10 * ca)) -- local w = 100 - (0.1 * (-1 * c)) local wa = -1 * ( (100 - LocalPlayer():GetNWInt("GryEnergy")) / 30) surface.SetTexture( enr ) // energy BAR surface.SetDrawColor(Color(20,150,230alpha_ch[1])) surface.DrawTexturedRectRotated( aa + GryModXDistance:GetInt() + GryModXDistance2:GetInt(), ScrH() - (EyeFinityScrW()/15.1) - wa , ca, (EyeFinityScrW()/87.272727) , -2.98 ) [/lua] [editline]now lel[/editline] I meant i can't read my own code. (Yes, i'm good at shitposting)
That code won't even load: [code] surface.SetDrawColor(Color(20,150,230alpha_ch[1])) [/code] Also, good luck reading your own code after a month.
[QUOTE=vrej;44883284]Well if I am going to upload something on the workshop, I am going to make it look messy and unreadable as much as I can. This isn't the real way I code. This is just to stop those thieves, since I had few people stealing from me and I the only solution that I found is making my code messy.[/QUOTE] But this is here to post your real programming style, not the place to post your wannabe obfuscation that won't stop anyone.
[QUOTE=LennyPenny;44887292]But this is here to post your real programming style, not the place to post your wannabe obfuscation that won't stop anyone.[/QUOTE] Ya but I already said how I really code like: [QUOTE=Mors Quaedam;44863077]Here's an example of two functions in my admin mod. [t]http://ss.infd.us/linux/2014-05-20@23-45-49.png[/t] - No semicolons. [highlight]Ever.[/highlight] - No parenthesis around if statements: rather than [I]if (a == b) then[/I], I use [I]if a == b then[/I] - 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[/QUOTE]
someone should come up with a little snippet that has all the basic things people do, like all different kinds of variables, functions, commas, logic ops and stuff like that and we could all just edit that to fit our style and repost it
[QUOTE=PortalGod;44888475]someone should come up with a little snippet that has all the basic things people do, like all different kinds of variables, functions, commas, logic ops and stuff like that and we could all just edit that to fit our style and repost it[/QUOTE] It wouldn't be hard to adapt mine; and besides for my Lua Class that I'll be teaching, there will be different coding standards used for different projects to get individuals used to adhering to coding standards of a project, or using their own when they create a project whereby others must adhere. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/_coding_standards.lua.html[/url] I still need to finalize that; and I'm going to edit the HTML to make it look a little better in areas.
Spacing. Spacing everywhere. Even when using Derma. Have some snippets: [lua] local pnl = vgui.Create( "DFrame" ) pnl:SetPos( ScrW() / 2 - 125, ScrH() / 2 - 90 ) pnl:SetSize( 350, 180 ) pnl:SetTitle( "" ) pnl:SetVisible( true ) pnl:SetDraggable( true ) pnl:ShowCloseButton( true ) pnl:MakePopup() [/lua] Rarely use comments, mainly C operators ( &&, ||, and stuff ) Naming variables according to what they are ( pnl or main for DFrame, etc ) Spaces between arguments and use Format() for concancenation.
[QUOTE=Acecool;44889668]It wouldn't be hard to adapt mine; and besides for my Lua Class that I'll be teaching, there will be different coding standards used for different projects to get individuals used to adhering to coding standards of a project, or using their own when they create a project whereby others must adhere. [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/_tutorial_quizzes/_coding_standards.lua.html[/url] I still need to finalize that; and I'm going to edit the HTML to make it look a little better in areas.[/QUOTE] Look, I realize that everyone is allowed to have their own coding styles because it's all personal preference. However, you must realize that to someone who has little/no understanding of how Lua or even programming in general works that your syntax is hard to read and requires extraneous work for what appears to be, especially to a beginner, no gain. If you're going to teach a class in Lua syntax, I would greatly advise you teach them *specifically* lua syntax (this means using 'and' and 'or', as well as no semicolons or the underscores) at that point, it would be appropriate to show them your coding style and explain it to them, at which point they can choose to accept it. Simply forcing your coding style onto beginner programmers who may or may not even have a grasp of basic concepts is simply going to frustrate them and turn them off from the whole idea of programming.
[QUOTE=CallMePyro;44890160]Look, I realize that everyone is allowed to have their own coding styles because it's all personal preference. However, you must realize that to someone who has little/no understanding of how Lua or even programming in general works that your syntax is hard to read and requires extraneous work for what appears to be, especially to a beginner, no gain. If you're going to teach a class in Lua syntax, I would greatly advise you teach them *specifically* lua syntax (this means using 'and' and 'or', as well as no semicolons or the underscores) at that point, it would be appropriate to show them your coding style and explain it to them, at which point they can choose to accept it. Simply forcing your coding style onto beginner programmers who may or may not even have a grasp of basic concepts is simply going to frustrate them and turn them off from the whole idea of programming.[/QUOTE] Actually, a lot of beginners whom I've taught have said they prefer my syntax over the standard syntax. But, yes, everyone is different which is why my class will cover many different styles; and there will be a basic starter course which will cover BNF/EBNF for the basic syntax, moving into GLua, then using "standard Lua" syntax, "My" syntax, "GLua" standards, plus many others depending on the "project/assignment" and level of the chosen assignment. Nothing is being forced on anyone; the class is completely voluntary and anyone can jump in to the hardest class right away, or start from the beginning; all of it will be available at all times to browse through. One of the biggest things people have a hard time with when starting out in the field, when they join a large company, is that the company may require each coder to use their specific coding standard. It may be crap, it may be good, but they will require it. When there are hundreds of people coding one project, and each person is given several functions with x input and y output to create; it really helps when it is all coding with the same syntax. My class covers real-world scenarios instead of standard "classroom" mumbo-jumbo. Like I said, I understand where you're coming from, and it [B]will be covered[/B].
I secretly use semicolons [b][i]everywhere[/i][/b] except when I post on this site.
[QUOTE=Acecool;44890233]Actually, a lot of beginners whom I've taught have said they prefer my syntax over the standard syntax. But, yes, everyone is different which is why my class will cover many different styles; and there will be a basic starter course which will cover BNF/EBNF for the basic syntax, moving into GLua, then using "standard Lua" syntax, "My" syntax, "GLua" standards, plus many others depending on the "project/assignment" and level of the chosen assignment. Nothing is being forced on anyone; the class is completely voluntary and anyone can jump in to the hardest class right away, or start from the beginning; all of it will be available at all times to browse through. One of the biggest things people have a hard time with when starting out in the field, when they join a large company, is that the company may require each coder to use their specific coding standard. It may be crap, it may be good, but they will require it. When there are hundreds of people coding one project, and each person is given several functions with x input and y output to create; it really helps when it is all coding with the same syntax. My class covers real-world scenarios instead of standard "classroom" mumbo-jumbo. Like I said, I understand where you're coming from, and it [B]will be covered[/B].[/QUOTE] you're coding lua not C why are you trying to shoehorn the language into being like C that is silly
[QUOTE=BayLife;44890526]I secretly use semicolons [b][i]everywhere[/i][/b] except when I post on this site.[/QUOTE] That's the most sensible way to use them.
[CODE] hook.Add( "PlayerFullyLoaded", "FullyLoaded", function( ply ) if PLUGIN_ENABLED == true then playerNick = ply:Nick() playerSteamID = ply:SteamID() TOP_MESSAGE_TEXT = string.gsub( TOP_MESSAGE_TEXT, "$playername", playerNick ) TOP_MESSAGE_TEXT = string.gsub( TOP_MESSAGE_TEXT, "$playersteamid", playerSteamID ) BOTTOM_MESSAGE_TEXT = string.gsub( BOTTOM_MESSAGE_TEXT, "$playername", playerNick ) BOTTOM_MESSAGE_TEXT = string.gsub( BOTTOM_MESSAGE_TEXT, "$playersteamid", playerSteamID ) net.Start( "sendToPlayer" ) net.WriteString( TOP_MESSAGE_TEXT ) net.WriteString( BOTTOM_MESSAGE_TEXT ) net.Send( ply ) if PRINT_IN_CHAT == true then ply:ChatPrint( TOP_MESSAGE_TEXT .. BOTTOM_MESSAGE_TEXT ) end end end ) [/CODE] I'm still learning to be consistant, but I like doing variableOne for variables that get changed, VARIABLE_TWO for variables that are consistent, and spaces after brackets, except when doing maths. So for example, bottomMessageText:MoveTo( (ScrW()/2)-(bottomMessageTextX/2), (ScrH()/4)+30, 0.1, 5.3, 1 ) Find it easier to see what brackets belong.
I hate it when people use "pl" instead of "ply" or "player" fuck Also appropriately named variables in loops, not just "k, v" but like "player, table"
What's wrong with using 'pl'?
[QUOTE=Shinycow;44900879]What's wrong with using 'pl'?[/QUOTE] pl doesn't tell you what the variable is. ply almost always will be a player object.
[QUOTE=Shinycow;44900879]What's wrong with using 'pl'?[/QUOTE] Also 3 letters minimum is common for shorthand variables. ply, msg, tbl, str, num, ent, pnl, btn, you get the idea.
I hate doing 'true' and 'false' so I do local yes = true local no = false and I prefer 'player' over 'ply' NEVER Any Semi colon's,
[QUOTE=Positive;44902112]I hate doing 'true' and 'false' so I do local yes = true local no = false and I prefer 'player' over 'ply' NEVER Any Semi colon's,[/QUOTE] Do you mean you hate doing true and false as a string? Obviously, who would do that?
Sorry, you need to Log In to post a reply to this thread.