l122.posts.articles.style_guide about code posts

l122.posts.articles.style_guide.introduction l122.posts.articles.style_guide.formatting l122.posts.articles.style_guide.formatting.indentation l122.posts.articles.style_guide.formatting.whitespace l122.posts.articles.style_guide.comments l122.posts.articles.style_guide.naming l122.posts.articles.style_guide.naming.variables_and_fields l122.posts.articles.style_guide.introduction

* this guide covers Lua, however it applies to all programming languages

* concepts here are decribed abstractly, so you can apply them how you want!

* dont feel compelled to follow my opinions, unless ive linked you here...

* if you disagree with any of these points strongly, please email me!

i have been into programming for a long time, ive tried many programming languages, different areas of computer science, and at this point ive touched just about everything i consider to be meaningful :) with that i think i have some sort of authority when i say that writing consistent, understandable, maintainable code is the most important thing you can do, regardless of what youre doing, what youre making, how experienced you are with programming, or who you end up working with^(including yourself! take care of yourself, please... :c)

and i really mean that! more than anything else, the most important thing you can do for yourself is to make sure every single line you write makes complete sense given the context of your codebase anywhere from 1 to 6 months later! its the same thing everyone tells you when you first start, that "You read code more than you write it.", its fluffy true lol

that time frame may seem like a slight exaggeration, but i really mean it! i really believe you cant call yourself a respectable programmer if you cant read code that *you wrote*, which *you know* how it works, and its even worse when you inevitably end up restarting a project because the codebase has devolved into a mess...

and thats what this is for! this page documents the rules ive come up for my code in naming, structuring, documenting, etc. that results in sane and understandable code that ages well! our ultimate goal is to embed as much information into the code as physically possible in a way that we can read back later, by providing clear and specific guidelines to follow

██████████████████

l122.posts.articles.style_guide.formatting

the first and probably one of the most important aspects of this is whitespace! whitespace is the small tabs, newlines, and spaces between significant bits of code, and theyre not meaningless! whitespaces tell the intent behind what the code does and how you should look at it, starting with the use of Allman casing!

l122.posts.articles.style_guide.formatting.indentation

to start, heres an example of both options with this demo code in C, Allman on the left, and K&R on the right

the very first thing anyone notices when comparing the two most popular indentation styles, or casings, K&R and Allman , is that K&R casing gives an overall smaller footprint, and this is most of the reason people seem to prefer K&R over Allman casing, but does this actually matter? how much space is actually given back, and what advantages does it give?^(turns out not a lot)

this second image represents how a programmer may look at this code while skimming through it, not necessarily watching for anything specific

these highlighted sections represent associated blocks and how you may percieve them together, a big change is that the closing } for each block creates a dedicated space for just the brace to sit, and this is visually distinct from the rest of the block! this space is significant because it reads as a line break, and line breaks represent a different *section* of the code, not a different block, things can be related to eachother and exist within different blocks, so this sends a confusing message to the reader, and may cloud up someones search for some specific part of the code by appearance

also shown in the previous image is a slight inconsistency between when to put a space after the condition of a statement, and when to leave it attached to the end! this comes down to personal style, but some things look very strange when the opening brace is tacked on at the end, you will see programmers either put a space before each opening brace, or never put a space between an opening brace, or they may just choose whatever looks best, and thats a problem...

i dont think anyone disagrees that case 's':{ just looks strange... someone as well may prefer to put switch (argv[flag][c]){ because they prefer the brace to sort of disappear in front of the condition, ...just that this creates a discrepancy between the opening and closing braces which now look completely different from eachother, and dont match visually to their counterparts on the opposite side of the block...

this is the main advantage of Allman, which is the visual clarity and associative structure it gives, there are only two rules to follow Allman casing effectively, ensure:

* every brace has its own line

* all content within a block is indented

thats it! theres no breaking the rules to account for } else { inline... use Allman, you know you want to...

to people who like K&R, tell me how you feel about this :) ...

l122.posts.articles.style_guide.formatting.whitespace

besides indentation style, there are a lot more meanings for whitespace, and we can dig into more of them by analyzing the example again!

the first principle is that containers, operators and the values they operate on should be separated by a space, and this is the default look! say that we have a function which takes 5 arguments txyzw and we create a function definition using them, we would start with the first rule of separating them by space:

local function myFunction (t, x, y, z, w) end

and this works, however when reading this for the first time, when you dont know what t or w are, this definition removes any association between the values, you dont know whether x and y belong to t, and whether w stands for width or a this is a 4 dimensional point!^(may sound pedantic, but this is a simplified example and its usually much more complicated) to fix this we add association by placing double spaces between each argument:

local function myFunction (t, x, y, z, w) end

and through this it becomes clear what the intention actually is! just from the look of it, the appearance of a function signature which has one single argument without association to any other arguments, shows that the initial argument is an object or type which all of the others do apply to, and there are many easy patterns we can observe like this

(a, b, c, x, y, z) -- ambiguous, maybe math (a, b, c, x, y, z) -- one object (a, b, c, x, y, z) -- two objects (a, b, c, x, y, z) -- one object, probably sets fields

this concept is especially important from the caller too, say youre calling something that uses magic numbers or fields of some variable, from a quick look you dont exactly know which is which, and you can get confused or miss details:

l122.posts.articles.style_guide.comments

comments are usually not important even in large codebases, not in the way people think they usually are where youre describing the behavior of some code which is already known, no!

function table.encode (table) local f = "" local function w (tabs, content) -- prepend tabs to the start of the line -- for _=1, tabs do f = f..'\t' end -- write content -- f = f..content..'\n' end ... ...

comments which describe things inline like this are not useful and take up valuable screen real estate from which you could actually read the code itself and figure out what it is!

this first comment, prepend tabs to the start of the line might seem informative, but this code here is so simple that you dont even need to read the comment! its just a loop, which prepends a tab for however many tabs are required!

this is a pretty simple case, but writing this out completely takes more space than it does to just read the code, what if you cant provide an accurate summary within the space given?

* incomplete

l122.posts.articles.style_guide.naming

* incomplete

l122.posts.articles.style_guide.naming.variables_and_fields

* incomplete


uploaded 26.05.20 updated 26.11.21, 1795290198