less_retarded_wiki

main page, file list (587), source, all in md+txt+html+pdf, report abuse, stats, random article, consoomer version

Programming Style/Code Formatting

Probably in majority of cases a programming language lets the programmer choose the aesthetic style in which to write the code (just like a writer may format his text in visually different ways without changing the meaning of it) -- one has a choice in naming variables, indentation and aligning commands, inserting comments and so on. This gives rise to various styles -- typically a programmer will have his own preferred style, kind of like handwriting, but once he works in a team, some compromise has to be found to which everyone must conform so as to keep the code nice, consistent and readable by everyone. Some project, e.g. Linux, have evolved quite good, tested and de facto standardized styles, so instead of inventing a custom style (which may not be as easy as it sounds) one may choose to adopt some of the existing styles. While this is more of a surface-level part of programming, it is still quite important and thinking about it may go very deep, it is not to be underestimated.

There exist automatic code formatters, they are often called code beautifiers. But not everything can be automatized, for example a program will hardly comment your code, or inserting empty spaces to separate logically related parts of a sequential code is also something that human like intelligence is needed for.

Recommended LRS C Programming Style/Formatting

Here we propose a programming style and C code formatting you may use in your programs. { It's basically a style I personally adopted and fine-tuned over many years of my programming. ~drummyfish } Remember that nothing is set in stone (except that you mustn't use tabs), the most important thing is usually to be consistent within a single project and to actually think about why you're doing things the way you're doing them. Keeping to the standard set here will gain you advantages such as increased readability for others already familiar with the same style and avoiding running into traps set by short-sighted decisions e.g. regarding identifiers. Try to think from the point of view of a programmer who gets just your source code without any way to communicate with you, make his life as easy as possible. Also suppose he's reading your code on a calculator. The LRS style/formatting rules follow:

if (a == b)
{
  doSomething();
  doSomething2();
}
else
{
  doSomethingElse();
  doSomethingElse2();
}
void doSomethingCool(int a, int b, int c)
{
  // ...
}
switch (myVariable)
{
  case 0:
    doSomething();
    break;
   
  case 1:
    doSomethingElse();
    break;
    
  case 2:
  {
    int a = x + y;
    doSomethingCool(a);
    break;
  }
  
  default:
    break;
}

// or even (depending on how long the sections are)

switch (myVariable2)
{
  case 0: doSomething1(); break;
  case 1: doSomething2(); break;
  case 2: doSomething3(); break;
  case 3: doSomething4(); break;
  default: break;
}
int a = x;
char b = y;
double q;

doSomething(a);

c += 3 * a;
d -= b;

if (c < d)
  a = b;

Example

Here is a short example applying the above shown style:

TODO (for now see LRS projects like Anarch, small3dlib, SAF etc.)

Powered by nothing. All content available under CC0 1.0 (public domain). Send comments and corrections to drummyfish at disroot dot org.