The power of INDENTATION - Write easly readable code!

Discussion in 'Resources' started by Digi, Aug 21, 2012.

Thread Status:
Not open for further replies.
  1. I was once a newb at coding when I started on Pawn language, and back then I was writing code in a very compact manner, 'as few spaces, empty lines and lines as possible' I was thinking... but then I realized how wrong I was when I discovered the benefit of indenting code !

    What is "indentation" ?
    In short - formatted code.
    Code that has lots of spacing and empty lines allows it to be alot easier to read and alot easier to find any kind of errors.

    It has no effect on performance ! It's only helpful to humans, the machine doesn't care about your indentation.

    An example of an extreme unindented and compact code:
    Code:
    boolean condition=false;
    if(condition&&(condition||!condition)){
    while(condition){
    System.out.print("...");
    break;}
    for(int i=0;i<100;i++){
    System.out.print(" - "+i);}
    if(condition)return false;else condition=true;
    switch(5){case 1:case 2:case 3:case 4:break;
    default:System.out.print("case 5!");break;}
    return true;}
    I guess it can be read... barely.

    Now take a glance at the same code but indented:
    Code:
    boolean condition = false;
    
    if(condition && (condition || !condition))
    {
        while(condition)
        {
            System.out.print("...");
            break;
        }
        
        for(int i = 0; i < 100; i++)
        {
            System.out.print(" - " + i);
        }
        
        if(condition)
            return false;
        else
            condition = true;
        
        switch(5)
        {
            case 1:
            case 2:
            case 3:
            case 4:
                break;
            
            default:
                System.out.print("case 5!");
                break;
        }
        
        return true;
    }
    
    You can clearly see everything that is going on in there without struggling to understand each symbol.

    There are many indentation styles, it can be obvious which I like from the example above :p You are, of course, free to use any style of indentation, but the point of this thread is to make you aware that they exist and they can make yours and everybody else's life easier when it comes to reading your code !

    Indenting code instantly !
    Most editors (IDEs) have a formatting option to automatically indent and format your code.

    In Eclipse you can use Ctrl+Shift+F to format the current code or you can rightclick project/package and pick Source -> Format.
    You can customize the formatter's behaviour in Window -> Preferences -> Java -> Code style -> Formatter where you can import, export and edit diferent format styles.

    However, you shouldn't rely on the automatic formatter, as it can't group codes and it doesn't add empty lines between statements, that you'd have to do manually while writing the code.


    I hope people won't keep neglecting indentation and learn to write code more readable - expecially when asking for help :)


    Also, have a look at the Clean-up feature as well.


    I've made my own custom formatter style which formats the code something like the example above (empty lines I added myself), if you want you can get it: Digi's formatter (eclipse) - it can be imported in the Formatter in Preferences.
    You can also get the clean-up settings I use: Digi's clean-up (eclipse)
     
  2. Offline

    Deleted user

    Now format it correctly <3
     
    MrBluebear3, afistofirony and jtjj222 like this.
  3. Offline

    chaseoes

    Ctrl + I in Eclipse will indent everything without formatting it (I hate how the formatter moves stuff over x length to new lines.. harder to read).
     
    chasechocolate and zack6849 like this.
  4. Well, my Eclipse seems to only indent the selected code or the current line with Ctrl+i.

    You can customize formatter however, I didn't see an option to customize only indender which seems to follow a diferent method as I used it on my code and it indents diferently from format.

    I don't get it.
     
  5. Offline

    Giant

    Amen to that! The only thing I disagree to is the open curly bracket on a new line. In my opinion that just makes code looks less readable, but that might just be me being stupid!
     
  6. Offline

    Deleted user

    Digi
    ^
     
  7. Offline

    Hidendra

    No it's all about personal preference (or of course, existing project coding guidelines!) If it were me it'd be more like this.
    Code:
    boolean condition = false;
    
    if (condition && (condition || !condition)) {
        while (condition) {
            System.out.print("...");
            break;
        }
    
        for (int i = 0; i < 100; i++) {
            System.out.print(" - " + i);
        }
    
        if (condition) {
            return false;
        } else {
            condition = true;
        }
    
        switch (5) {
            case 1:
            case 2:
            case 3:
            case 4:
                break;
    
            default:
                System.out.print("case 5!");
                break;
        }
    
        return true;
    }
    
    (hint for what I prefer: braces on the same line, 4 spaces no tabs, always use braces even for one line expressions, space after if/while/for statements and the parens (e.g if ()))
     
  8. Offline

    Giant

    That is pretty much how I like it aswell, I tend to not add spaces round the curly brackets for else-es though. And I sort of prefer tabs over 4 spaces as to me, 4 spaces slow me down quite a bit...
     
  9. Offline

    Deleted user

    The great Hidendra speaks.
     
  10. Offline

    Hidendra

    My IDE (IntelliJ) inserts 4 spaces when I hit tab, so I think most other IDEs could do it as well.

    Once upon a time I preferred tabs but I started preferring the 4 space approach after tabs started leaving a bad taste to me. Spaces feel more concrete -- with tabs I find it awkward because some people use tabs in places other than indenting so when you're mixing both of these it can get confusing (as they aren't always the same width!) Now when I see whitespace I know it's definitely just one space wide, there is no uncertainty
     
  11. Offline

    Giant

    That is correct, most IDEs will by default use 4 spaces, I however generally configure my IDE to be insert tabs with the width of about 4 spaces. :)
     
  12. Well, as previously said, it's a preference, you can do the brackets positions and tabs/spaces as you like... however, the point of this thread is to make people aware of the benefits of those spaces before expressions and empty lines between them as well as spaces between symbols so it doesn't look like a character mashup.

    I prefer the bracers on new lines for 2 reasons:
    - I can clearly see that I use a code block there, as I don't use code blocks when I use single expressions for conditions (not for loops tough);
    - I can easily comment out the condition or loop or whatever triggers that code block with Ctrl+/ while the carrot is on that line and/or replace it... and it's consistent with code blocks that don't have conditions/loops :p

    And I prefer tabs because I'm used to manually indenting my code, when I started learning PHP and then Pawn I only used wordpad... and it's a pain to press backspace 4 times xD I feel it's more flexible when each indentation level is one single character instead of 4 individual ones :}
    I only write code with spaces when I write it directly in posts because in my IDE the tabs are 4 spaces width.

    Also, the formatter can be configured to use only spaces, then the tab key will add 4 spaces instead of a tab character.
     
  13. Offline

    desht

    Ctrl+A Ctrl+I is useful here (select all, indent selected).
     
  14. Offline

    Icyene

    This might be just my setup for Eclipse; I don't remember, but Ctrl+Shift+F does the trick on the entire document opened.
     
  15. Offline

    Coelho

    I'm more used to the cases being the same indentation as the switch :p
     
  16. Obviously, but it's an extra key combination to press (I also use Ctrl+S so they're 3 key combinations).
    Also you can do Ctrl+Shift+F and Ctrl+S with one hand while the other hand goes over to the mouse to export :p

    Yes it does format the entire file, now I have a tick for using Ctrl+Shift+F =)
     
  17. Offline

    Icyene

    In retrospect, that may explain why my Shift key gets stuck...
     
  18. Offline

    honestduane

    There is a book called "Clean Code" by a guy known in the development community as "Uncle Bob", its a great book on Software Craftsmanship and I highly recommend that people read it. It speaks to exactly things like this.
     
    Renlar likes this.
  19. Ew C# Style formatting :(
     
  20. Ew observation that has nothing to do with the point of this thread :p
     
  21. Offline

    Chrono7

    I personally use Ctrl + A (to select all) then Ctrl + I (fixes indentations in the selected lines)
    to fix indentation issues that are annoying to do manually. I use this because the formatter breaks my longer lines in half, which bothers me tremendously.
     
  22. Chrono7
    You can use whatever you want as long as you indent it so it can be read :p
     
  23. Offline

    Chrono7

    Yep just sharing xD
     
  24. Offline

    Gravity

    I love and use the allman style (formatting used in the OP) - your code is music to my........... eyes...

    Although I would advise that you always use { }'s for if statements, even if it is only one line. This is suggested by most Java standards, and if you ever go back and add something to that if statement, you may forget that it has no curly braces and so the second thing you add will just always get called. Always adding curly braces just makes life easier updating and adding to code.
     
  25. Offline

    HollowCube

    I really hated the fact that the formatter did that to my code as well. However, follow the instructions on customizing the formatter at http://forums.bukkit.org/threads/how-to-make-eclipse-clean-up-format-your-code-for-you.78574/, then go to the "Line Wrapping" tab. Set the "Max Line Width" to something unreasonably large such as 9999 (the max value). You could also edit the "Line Wrapping Policy" for each part of the code and set it to "Do not wrap" if you really think you're going to go over the limit of 9999...

    EDIT: Apparenty you can use CTRL+A to select all of the line wrapping policies at once. Thanks to @Digi for pointing this out.
     
  26. Offline

    md_5

    I hate Allman styled code. Have to use it for Essentials but at least the IDE does it.
    I just like the Netbeans default.
     
    chasechocolate likes this.
  27. Offline

    Icyene

    In my opinion, Allman is good until you have large schedulers with lots of other nested things in it (or the equivalent of). Then it just looks like a birds-eye view of a city, with all the lines being streets. Its painful looking at the terrible work the road-builder-guys did.
     
  28. Offline

    ZachBora

    I much prefer to put the { on a new line, it forces a space between the statement (if, while, etc) and the first line inside it. It also has a better geometry (gotta love symmetry) and makes it easy to see which bracket corresponds.

    Edit: also, date format yyyy-mm-dd ! It's ISO 8601 which is the best formatting in the world.
     
    Hoot215 and H2NCH2COOH like this.
  29. Offline

    Codisimus

    I don't agree with the indentation of each case within switch statements.

    ZachBora I find that it is much easier to visualize the blocks of code with Egyptian brackets. But most likely bc that is wut I am use to.
     
Thread Status:
Not open for further replies.

Share This Page