• Your Idea of 'Pretty' Code?
    101 replies, posted
[QUOTE=txike;52116090]There's actually a few fairly big differences between obfuscation and encryption.[/QUOTE] People actually thought I was serious. fuck, you guys are gullible. It was a joke mocking his logic, as there's no "single tiny part" of obfuscation.
Well I thought my code was beautiful until it ended out being 500 tabs wide with 200 ends in a row at the bottom...
[QUOTE=txike;52116895][code]local function meme(x,y)return(math.abs(x-y));end[/code][/QUOTE] [code]local function meme(x,y) return math.abs(x-y) end[/code]
[QUOTE=Thiefdroid;52118124]Well I thought my code was beautiful until it ended out being 500 tabs wide with 200 ends in a row at the bottom...[/QUOTE] I know right? I have this problem all of the time.
To be honest if you put your code in this way it becomes way more compact and makes it easier to find whatever you are looking for. [code] AddCSLuaFile() Hook.Add("PlayerSay" , "RespawnCommand" , function(ply , text) if string.lower(text) == "!respawn" or string.lower(text) == "/respawn" or string.lower(text) == "/r" or string.lower(text) == "!r" then ply:SetTeam(2) ply:Kill() ply:Spawn() return ''" end end) if (SERVER) then timer.Create( "ForumAdvert", 300, 0, function() PrintMessage( HUD_PRINTTALK, "Register on our forums at https://tinymode.phy.sx/forums!" ) end ) hook.Add( "PlayerSpawn", "SizeOnSpawn", function( ply ) if ply:Team() == TEAM_PROPS then return end ply:SetModelScale( 0.425, 0 ) ply:SetViewOffset( Vector(0, 0, 28 ) ) ply:SetViewOffsetDucked( Vector(0, 0, 16 ) ) ply:SetStepSize( 16 ) ply:SetHull( Vector( -5.5, -5.5, 0 ), Vector( 5.5, 5.5, 29.5 ) ) ply:SetHullDuck( Vector( -5.5, -5.5, 0 ), Vector( 5.5, 5.5, 17.5 ) ) end ) end [/code]
[QUOTE=Thiefdroid;52118570]Hook.Add[/QUOTE] Wouldn't even work smh
My friend one wrote an anti-cheat and sent me this [img]https://i.imgur.com/Ffxvawb.png[/img] Something about it is beautiful. I'm not sure what...
Not sure if any code is 'Pretty' to me but, I think "config" lua files are pretty because it makes it easy to edit what you want.
[QUOTE=code_gs;52119477]Wouldn't even work smh[/QUOTE] [lua]local Hook = hook[/lua]
The prettiest code is when you keep slightly changing your style every time you write anything. :scream:
What's a good style for a lot of nested for loops? [editline]19th April 2017[/editline] For example, [URL="https://github.com/Mysterypancake1/GMod-Rainbows/blob/master/lua/autorun/client/rainbow.lua#L103-L110"]this[/URL] looks really bad and I don't know what to do: [CODE] local function DrawRainbowRect2( x, y, w, h ) for i = x, x + w - 1 do for j = y, y + h - 1 do surface.SetDrawColor( i / w * 255, j / h * 255, 255 - ( i / w * 255 ), 255 ) surface.DrawRect( i, j, 1, 1 ) end end end [/CODE]
[QUOTE=MPan1;52123814]What's a good style for a lot of nested for loops? [editline]19th April 2017[/editline] For example, [URL="https://github.com/Mysterypancake1/GMod-Rainbows/blob/master/lua/autorun/client/rainbow.lua#L103-L110"]this[/URL] looks really bad and I don't know what to do: [CODE] local function DrawRainbowRect2( x, y, w, h ) for i = x, x + w - 1 do for j = y, y + h - 1 do surface.SetDrawColor( i / w * 255, j / h * 255, 255 - ( i / w * 255 ), 255 ) surface.DrawRect( i, j, 1, 1 ) end end end [/CODE][/QUOTE] I don't think there's any other way to do it. Only thing I'd change is the names of i and j, to stuff like xpos and ypos perhaps.
too long
[QUOTE=Promptitude;52118307][code]local function meme(x,y) return math.abs(x-y) end[/code][/QUOTE] my general philosophy is to have no lines longer than 10 characters [code]local function meme ( x , y ) return math . abs ( x - y ) end[/code]
[QUOTE=code_gs;52099871]Lisp is a beautiful language[/QUOTE] Ah the joys of Lots of Insipid Stupid Parenthesis.
[QUOTE=MeepDarknessM;52123964]my general philosophy is to have no lines longer than 10 characters[/QUOTE] Commodore BASIC programmers love him with this one weird trick
[QUOTE=MPan1;52123876]too long[/QUOTE] If I look at your variable names and don't instantly know what they do - then you should rename them. Short, but straight forward.
My idea of pretty code? Probably anything that doesn't look like mine.. still new to Lua. This thread however is giving me ways I can improve though! Quite a fun language imo.
My personal guidelines when writing personal code are as follows: In general, adhere to vanilla standards whenever possible. That means forgoing "&&" for and, "!=" for "~=", and so forth. The reason for this is simple: when working on vanilla Lua and Garry's Mod Lua simultaneously, it is best to stay consistent to avoid errors. Furthermore, the official documentation and Lua community follows a loosely consistent style worth adopting. Reinventing the wheel for Garry's Mod doesn't really make sense to me. * Spaces instead of tabs. * 2 or 4 spaces for tabs. (I personally use 2, as it's a little more Pythonic, saves space, and it's also used in the official Lua documentation. * Maximum of 80 characters per line (including whitespace) * Contrary to the Lua community, use camelCase for anything local and PascalCase for anything global when working in GLua. This is because Garry's definitions tend to follow this format. * While privacy and constancy doesn't exist in the Lua environment, it's a good idea to illustrate the intention by introducing an underscore in front of anything you introduce that should really only be manipulated with a getter and setter function and indicating constancy with ALL_CAPS. While this is true, you should never use PascalCase with private variables as that implies that it is an internal object for Lua itself. * Use "and", "or", "not", and "~=". It may be tempting to use "&&", "||", "!", and "!=", but I personally believe it's a weird patch on top of the Lua environment that creates inconsistencies if working on other Lua projects. * Inside the parentheses of functions, the only spaces needed are after commas. Placing a space after the open parenthesis and before the closing parenthesis doesn't really make code more readable (at least not to me) and serves only as a waste of space. * Multi-line statements of any kind should have all but the first line indented once. * Operators deserve to be surrounded by spaces for readability. Give them some breathing room. And, the most important thing: * Be ready to sacrifice any or all of these rules at will when working on projects with other people, as you should by default with any project in any language. If you look at my personal code, it doesn't follow this format. But my public code tends not to follow any consistent format, with always changing styles to attempt to find what is most pleasant to work with. My current private code reflects the choices listed above. And, when working with other people's code, I will either adapt it to fit my style or simply adapt my writing to the style of the script.
[QUOTE=wauterboi;52127212]... ALL_CAPS. ... [/QUOTE] I like to call this SCREAMING_SNAKE_CASE Overall I agree with your guidelines. For as much crap as people give PHP(old iterations and code deserve that crap) the Framework Interop Group(FIG) have produced some nice style standards([url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md]PSR-1[/url] [url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md]PSR-2[/url]) that I tend to apply where appropriate in other languages. Though there's still some habits that I haven't or don't want to change(opening brace on new line, single vs double quote usage, space after control structure keywords.) But a good IDE let's you apply code styles at will. IMO there's a lot of people in this thread that aren't experienced with larger code bases or dealing with maintainability of old cold. Which is reasonable considering GMod scripting lends itself well to many one-off or smaller scripts.
[QUOTE=Dgc2002;52129075]I like to call this SCREAMING_SNAKE_CASE Overall I agree with your guidelines. For as much crap as people give PHP(old iterations and code deserve that crap) the Framework Interop Group(FIG) have produced some nice style standards([url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-1-basic-coding-standard.md]PSR-1[/url] [url=https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md]PSR-2[/url]) that I tend to apply where appropriate in other languages. Though there's still some habits that I haven't or don't want to change(opening brace on new line, single vs double quote usage, space after control structure keywords.) But a good IDE let's you apply code styles at will. IMO there's a lot of people in this thread that aren't experienced with larger code bases or dealing with maintainability of old cold. Which is reasonable considering GMod scripting lends itself well to many one-off or smaller scripts.[/QUOTE] I haven't really found something I like doing with quotes. I've been using JSHint for my JavaScript code, which asks for single quotes to be used whenever possible, but outside of JSHint I tend to accidentally carry over my tendencies with C++, in which single quotes indicate a single character and double quotes indicate a string. If I (inevitably) get back into Python I'll try to follow more of a JS-style with using single quotes whenever possible, and I try to do that with Lua.
[QUOTE=wauterboi;52130680]I haven't really found something I like doing with quotes. I've been using JSHint for my JavaScript code, which asks for single quotes to be used whenever possible, but outside of JSHint I tend to accidentally carry over my tendencies with C++, in which single quotes indicate a single character and double quotes indicate a string. If I (inevitably) get back into Python I'll try to follow more of a JS-style with using single quotes whenever possible, and I try to do that with Lua.[/QUOTE] Yea, double quotes for strings single for chars is kind of my basis as well. To my knowledge the main reason those standards recommend single quotes is because PHP allows string interpolation when double quotes are used. Once upon a time it introduced a minuscule overhead, but these days there's no meaningful difference. PyCharm(Great IDE based on IntelliJ) enforces [url=https://www.python.org/dev/peps/pep-0008/]PEP 8[/url] through warnings by default. If you're anything like me it'll make you hate your life if you don't disable some of the rules. I mostly resort to Python for scripts/CLI utilities where it's less important to strictly adhere to those standards.
[QUOTE=vexx21322;52123805]The prettiest code is when you keep slightly changing your style every time you write anything. :scream:[/QUOTE] It drives me crazy. I do this all of the time, then go back and edit all of my previous code that's being used with THAT code to following the rules in which my new style follows... Even if I didn't make it... Is that illegal? [sp]God, I'm fucked in the head.[/sp] [editline]21st April 2017[/editline] [QUOTE=wauterboi;52127212] * Spaces instead of tabs. * 2 or 4 spaces for tabs. (I personally use 2, as it's a little more Pythonic, saves space, and it's also used in the official Lua documentation.[/QUOTE] Everything else I agree with. Just not this. It seems easier to use one tab. It saves time and effort, in my opinion.
Most editors can be set to insert space characters when you press the tab button. My main problem with tabs is that every editor has a different idea of how big tab characters are. Sure, 4 spaces is most common, but iirc Github shows them at 8 spaces, which is where I host my code. Spaces at least are the same size everywhere. File size isn't an issue, it requires no more key presses than tabs do, and I don't have to remind myself not to accidentally use tabs for alignment.
[QUOTE=FPtje;52135258]Most editors can be set to insert space characters when you press the tab button. My main problem with tabs is that every editor has a different idea of how big tab characters are. Sure, 4 spaces is most common, but iirc Github shows them at 8 spaces, which is where I host my code. Spaces at least are the same size everywhere. File size isn't an issue, it requires no more key presses than tabs do, and I don't have to remind myself not to accidentally use tabs for alignment.[/QUOTE] I know at least that my text editor, sublime text 2, can swap spaces for tabs and vice versa, as well as make tab place x amount of spaces.
[QUOTE=Rocket;52135320]I know editors can insert spaces when pressing the tab button, but none of them that I've used know to delete the proper amount of spaces when pressing the backspace button. So to delete a tab that's been converted to spaces, I need to press the backspace button four times.[/QUOTE] Sublime text 3 does this properly. [vid]http://i.imgur.com/1nNYA3H.mp4[/vid] Where it doesn't, there's always Ctrl + backspace, Ctrl + [, or Ctrl + Shift + Tab.
[QUOTE=Rocket;52135320]I know editors can insert spaces when pressing the tab button, but none of them that I've used know to delete the proper amount of spaces when pressing the backspace button. So to delete a tab that's been converted to spaces, I need to press the backspace button four times.[/QUOTE] In Vim and Vim plugins, you do SHIFT+Left or SHIFT+Right to adjust tabs for a line or selection. Otherwise I've found Shift+Tab is a general unindent key.
[QUOTE=wauterboi;52135807]In Vim and Vim plugins, you do SHIFT+Left or SHIFT+Right to adjust tabs for a line or selection. Otherwise I've found Shift+Tab is a general unindent key.[/QUOTE] I've always used shift+< or shift+>. I didn't know you could also use left and right.
Oh, maybe I'm wrong on that then. I always jump to the beginning of the line and delete. I'm not really a Vim junky that gets off to speed.
[QUOTE=FPtje;52135352]Sublime text 3 does this properly. [vid]http://i.imgur.com/1nNYA3H.mp4[/vid] Where it doesn't, there's always Ctrl + backspace, Ctrl + [, or Ctrl + Shift + Tab.[/QUOTE] Visual Studio Code, which has completely replaced Sublime Text in my life, does this as well: [vid]http://i.imgur.com/MHtxPe6.mp4[/vid] The IntelliJ line of IDEs do this with all (un)indentation action as well.
Sorry, you need to Log In to post a reply to this thread.