[Tutorial] Beautiful, Readable Code II (Indentation Styles)
#1

Beautiful, Readable Code II - Indentation Styles


Foreword


The idea for this thread was brought to me by Vince in the previous thread, Beautiful, Readable Code I. If you haven't seen that thread yet, I would recommend you read it first by clicking here. In my previous thread I went through several key characteristics of writing readable code including indentation (in general), white space, and comments. That being said, I neglected to touch on indentation styles. I tried to keep my previous thread as "one size fits all" as possible as to not conflict with the different styles of indentation (although inevitably I did). I am creating this thread to explain (in as much detail as necessary for SA-MP scripting purposes) the different styles of indentation and their benefits. I will only be touching on certain popular styles that lay the groundwork for other styles, so to gain a more complete knowledge of indentation styles you may desire to do some reading on your own.

Oh, and one more thing! In the examples I provide I will be creating a simple command using the most popular command processor, ZCMD. If you aren't familiar with the processor, you can read about it by clicking here. Keep in mind that the example command is simply meant to demonstrate indentation.


Benefits


One common misconception is that the length of a script determines how well that script will perform. This is simply not true. The only reason this generalization exists is because of the poorly-written nature of some of the most popular scripts in the community. Somewhere along the line, members of the SA-MP community began to associate a lot of lines in a script with that script being poorly written. While it is possible that a long script is poorly written, on the other hand a script is not poorly written just because it is long. This is an important distinction to understand.

Because of the obsession with line count in the SA-MP scripting community, the choice of indentation styles has been heavily influenced as of recent. As you will see in the examples below, some styles of indentation will produce more lines than others. Keep in mind that despite what indentation style you choose, your script will perform at EXACTLY the same rate. More lines does NOT mean less performance. Instead of focusing on which indentation style creates the least amount of lines, the sole purpose of indentation is readability. Each indentation style affects readability in one way or another, and I will cover these in detail below. While you are reading, focus on which style suits you best and is most appealing. The only benefit that one indentation style provides over another is readability factor, so the indentation style which you find most readable is the one you should use.

One final benefit to understanding indentation styles is flexibility. Although you may find one style more readable than another, you are not always going to be working with code you have written. If, for example, you begin to edit another person's script, chances are they may be exercising a different indentation style. It is important that you are not only able to interpret the style they are using, but you actually must be able to use it yourself! It is vital that the same style of indentation is used throughout an entire script to ensure continuity. This is simply a matter of standards, and while neglecting it won't produce any errors or affect the script itself, continuity of indentation can make a script more readable should a third party ever need to intervene and read a script edited by more than one person. Be flexible!

Allman Style


I have elected to begin this tutorial with the Allman style of indentation because it seems (to me at least) to be the most prominent in the community. Named after Eric Allman, this style of indentation requires the placing of any brace related to a control statement to be placed on its own line. This includes opening and closing braces. See the example below.

ALLMAN STYLE
pawn Код:
COMMAND:heal(playerid, params[])
{
    if(IsPlayerAdmin(playerid))
    {
        SetPlayerHealth(playerid, 100);
    }
    else
    {
        Kick(playerid);
    }
}
This style of indentation allows nests to be clearly defined, which makes modifications to nests significantly more fluid. If you aren't sure what a nest is, refer to my previous thread's basic explanation of indentation. The amount of white space surrounding different nests provides excellent readability and the matching indentation of the opening and closing braces serves as a clear barrier to define the end of a nest. The visibility of the braces also decreases the possibility of leaving out a brace, one of the most frustrating errors that an inexperienced (or experienced alike) scripter can come across. Some would say that the "disconnected" nature of nests from their control statements actually hurts readability as opposed to the styles below.

This style of indentation has more recently come under fire in the SA-MP community because it generates more lines than the styles below. Once again, I would like to reiterate that line count in the case of indentation does not affect performance in the slightest. Your main focus in choosing an indentation style should be readability.

Stroustrup Style


As the second most prominent form of indentation in this community (as I see it), we have the Stroustrup style of indentation. This style is identical to the Allman style, except for the placement of the opening brace on the same line as the initial statement. See the example below.

STROUSTRUP STYLE
pawn Код:
COMMAND:heal(playerid, params[]) {
    if(IsPlayerAdmin(playerid)) {
        SetPlayerHealth(playerid, 100);
    }
    else {
        Kick(playerid);
    }
}
As you can initially see, this style takes up considerably less space than the Allman style. The placement of the opening brace on the same line as the initial statement defining the nest allows nested elements to feel more "connected" to their control statements. Some would argue that this makes reading code more effective in terms of determining cause and effect patterns. On the other hand, the lack of white space has been claimed to hurt readability and make the modification of large nests more difficult.

This style, once again, has become more prominent in the SA-MP community with the obsession of line count. While it is perfectly fine to utilize this method of indentation, make sure that you are using it because it is more readable in your eyes, not because you want to save on line count. Once again, line count in terms of indentation has no effect on performance.

K&R Style


Finally we have the K&R style, which is actually the style which the Stroustrup style is derived from. The K&R style is similar to the Stroustrup style in that it places the initial brace on the same line as the initial statement and places the final brace on its own line. The exception comes when else or while are used, where the K&R style places the closing brace on the same line as the else or while keyword. See the example below.

K&R STYLE
pawn Код:
COMMAND:heal(playerid, params[]) {
    if(IsPlayerAdmin(playerid)) {
        SetPlayerHealth(playerid, 100);
    } else {
        Kick(playerid);
    }
}
As you can see, the K&R style uses up the least amount of space of all of the styles showcased thus far. The question becomes, is this a good thing or a bad thing? Once again, it is a matter of readability in your eyes. Considering the fact that I rarely see K&R on these forums, I would not recommend using it for a new project if at any point someone else may be required to read and modify your existing code. This style of indentation is arguably the most susceptible to leaving out a brace, which is, once again, a very annoying experience.

Summary


I tried to remain as objective as possible throughout this thread; however, it's probably already clear to you which style of indentation I favor. Try not to let my commentary sway you. If you find K&R to be the most readable and you don't mind knowing that the majority of people on this forum who may be working with your code have no experience with the style, then go for it! To once again reiterate the point of this thread (I know I'm beating a dead horse), readability is the most important factor to consider in choosing an indentation style.
Reply
#2

I've tried all of the above but I always seem to go back to Allman. I think it's more habit than readability as thats what I started with. If you plan on making more tutorials, please make one on splitting a gamemode into different files/folders. I've been using multiple files for a while now but just can't seem to get a good structure that makes sense.

Rep+.
Reply
#3

Quote:
Originally Posted by Psymetrix
Посмотреть сообщение
I've tried all of the above but I always seem to go back to Allman. I think it's more habit than readability as thats what I started with. If you plan on making more tutorials, please make one on splitting a gamemode into different files/folders. I've been using multiple files for a while now but just can't seem to get a good structure that makes sense.

Rep+.
Great idea! Organization is a key element in productivity. Sounds like an excellent idea for a thread.
Reply
#4

Great job, but I'd stick to the Allman style, which is the most readable in my opinion.
Reply
#5

Quote:
Originally Posted by Red_Dragon.
Посмотреть сообщение
Great job, but I'd stick to the Allman style, which is the most readable in my opinion.
I completely agree with you. I've always personally preferred Allman.
Reply
#6

I used Allman from the beginning and just developed a habit of constantly using the style. Personally I feel it's the easiest to read, and is extremely beneficial when working with large nests. Good job, I enjoyed reading both the Readable Code tutorials.

+rep
Reply
#7

Quote:
Originally Posted by Finn707
Посмотреть сообщение
I used Allman from the beginning and just developed a habit of constantly using the style. Personally I feel it's the easiest to read, and is extremely beneficial when working with large nests. Good job, I enjoyed reading both the Readable Code tutorials.

+rep
Thanks for the feedback. I'm glad you are enjoying the series. Excellent points about Allman - I tried to explain these benefits in the original post as best as I could.
Reply
#8

I'll take the Stroustrup Style, reducing multiple lines, but more than the K & R Style consume even less lines, I see very exaggerated ..

This is possible, because the compiler reads from top to bottom, right to left.

Would I look like me or commands are not returning to 1?
Reply
#9

Quote:
Originally Posted by DeadSkyTkb
Посмотреть сообщение
I'll take the Stroustrup Style, reducing multiple lines, but more than the K & R Style consume even less lines, I see very exaggerated ..

This is possible, because the compiler reads from top to bottom, right to left.

Would I look like me or commands are not returning to 1?
I'm not sure what the first part of your comment is asking, can you try and rephrase it? As for the commands, I've elected not to return any value because the commands are simply a demonstration of indentation, not functionality. I'm trying to keep the code to a minimum in these examples. I mentioned this at the beginning of the thread.
Reply
#10

alman for the win haha
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)