sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Source Code Indentation : Why certain amount?

    Scheduled Pinned Locked Moved Developers' Forum
    19 Posts 7 Posters 1.2k Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by

      @unknownuser said:

      (http://forums.sketchucation.com/viewtopic.php?f)":2i409aa9]If you can choose, for example, a level of indentation in your source code editor (please don't choose two!) you have an option. If you are writing Python, four spacesis mandatory so you don't have an option.

      ...AND...
      @unknownuser said:

      (http://javascript.crockford.com/code.html)":2i409aa9]Indentation
      The unit of indentation is four spaces. ...

      I ask these questions, because neither Martin (code in general, Python specific,) nor Doug (Javascript specific,) give any reasons. [Obviously, the Python interpreter needed a specific indentation, by why was 4 chosen as opposed to 2, 3 etc? This discussion really is about what Ruby code indentation should be, but with a background of WHY predessesor languages chose certain indents.]

      What's so special about 4 space identation ??
      What makes it better than any other choice ??

      Why (Martin,) should I stop using 2 space identation ??
      I like it, it's quick to enter, it saves space, it is less likely for someone editing to be tempted to outdent a multi-nested block of code.

      I must state that I absolutely HATE outdented code blocks, they cover the ident guidelines in code editors.

      Would MY choice of identation matter, as SciTE, Notepad++ and any other good code editor can easily convert one identation setting to another ?? **

      **(You may need to asign macrokey(s) in your editor, if you don't wish to go through menus or dialogs.)

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        "outdent"?

        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          @thomthom said:

          "outdent"?

          Yea.. if you look at some of Jim's code, you'll see he has a habit of outdenting codeblocks.

          onmargin
              indented
                  indented twice
                      indented thrice
              outdented statement
              outdented statement
              outdented statement
                      end #3
                  end #2
              end #1
          end
          

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • T Offline
            tomasz
            last edited by

            I am using TABs instead. In notepad++ you can set the 'width' of a single tab. It works fine for me.

            Author of [Thea Render for SketchUp](http://www.thearender.com/sketchup)

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

              I've used to use tabs four space wide. But copying the code into other editors got a tendency to make the indent too wide. So I moved to 2 spaces instead.

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • M Offline
                MartinRinehart
                last edited by

                @dan rathbun said:

                Why (Martin,) should I stop using 2 space identation ??

                20 years ago, when bytes cost money, the standard advice was "indent 3 or 4 spaces." Between then and now the world has settled on 4 spaces. In addition to Crockford in JavaScript, K&R used 4 spaces in C. Stroustrup used 4 spaces in C++. Sun recommended four spaces for Java. Matz mandated 4 spaces for Python. Except for Ruby, the world has decided on four-space indentation.

                I've used conventional 4-space indentation in Basic, C, C++, Java, Python, JavaScript, HTML and CSS, among others. Four spaces are more readable. It's easy to see who aligns with whom, even over a substantial vertical range. Two-space indentation is less readable and it encourages deep nesting levels. Deep nesting is not something to be encouraged.

                Here's a bit of my code that multiplies matrices:

                
                    def * ( m2 )
                        vals = []
                        for r in 0..(@nrows-1)
                            for c in 0..(m2.ncols-1)
                                vals.push( row_col(row( r ), m2.col( c )) )
                            end
                        end
                        return MovableCI_Matrix.new( @nrows, m2.ncols, vals )
                    end # of *()
                
                

                Here's the same code, but with two-space indentation.

                
                  def * ( m2 )
                    vals = []
                    for r in 0..(@nrows-1)
                      for c in 0..(m2.ncols-1)
                        vals.push( row_col(row( r ), m2.col( c )) )
                      end
                    end
                    return MovableCI_Matrix.new( @nrows, m2.ncols, vals )
                  end # of *()
                
                

                Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                1 Reply Last reply Reply Quote 0
                • M Offline
                  MartinRinehart
                  last edited by

                  @dan rathbun said:

                  WHY predessesor languages chose certain indents.

                  Ruby was released in 1995, a vintage year for computer languages. The same year saw Java, JavaScript and PHP released. Three of four chose four-space indentation.

                  For more history see http://www.MartinRinehart.com/articles/genealogy-programming-languages.html

                  Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                  1 Reply Last reply Reply Quote 0
                  • M Offline
                    MartinRinehart
                    last edited by

                    @thomthom said:

                    I've used to use tabs four space wide. But copying the code into other editors got a tendency to make the indent too wide. So I moved to 2 spaces instead.

                    Poke around your editor settings. Tell it to output space characters, not tab characters.

                    Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

                      And the norm is to use space instead of tabs?

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        @unknownuser said:

                        Deep nesting is not something to be encouraged.

                        Another 'convention' stated, but without specifying fundamental reasons.

                        In Ruby, if say there was a "law" of no more than 4 levels of indentation, a coder had to move 5th level code blocks into a separate method, there would be a time cost to make the method call. If the nested block in question was within an iterative block, the multiple method call delays could really add up. (One of the reasons I wish Ruby could be compiled.)

                        I just feel, nesting is nesting, it occurs where it should or needs to occur. Meaning I prefer to create methods for good reason(s), not frivolous reasons.

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • Dan RathbunD Offline
                          Dan Rathbun
                          last edited by

                          @unknownuser said:

                          Four spaces are more readable. It's easy to see who aligns with whom, even over a substantial vertical range.

                          Not for ME. I never used 4 space idents (haven't yet gotten into Python,) and never saw or 'noticed' anything when I learned HTML, CSS, or Javascript (oh.. at least more than 10 years back,) that 4 spaces was some sort of 'convention'.

                          I the past, with plain-jane text editors, perhaps ident alignment needed the help that 4 spaces provided. But with better code editors now having the ident guidelines and foldmargin symbols to mark nested blocks, your arguments regarding 'who aligns with whom' and 'readability over a vertical range' lose some of their importance.

                          In addition, I can feel the muscles of my eyes having to do more work, yanking my eyeballs left and right, reading the 4 space example. This translates (for me,) into fatigue, headache and less programming time.

                          I'm not here much anymore.

                          1 Reply Last reply Reply Quote 0
                          • mitcorbM Offline
                            mitcorb
                            last edited by

                            From a total outsider's point of view, I would like to thank you guys for discussing this matter of indentation in coding, because that removes another layer of bewilderment for me as I try to make sense out of the bizarre, abstruse, and arcane vagaries of coding language. 😄

                            I take the slow, deliberate approach in my aimless wandering.

                            1 Reply Last reply Reply Quote 0
                            • TIGT Offline
                              TIG Moderator
                              last edited by

                              At least with something 'sensible' like Ruby the amount of indentation is up to you as the coder - it's just done to make it more readable and easier to debug...
                              A good text-editor auto-indents for you so you can keep track of the do...end, if...end's etc and the code between them...

                              TIG

                              1 Reply Last reply Reply Quote 0
                              • thomthomT Offline
                                thomthom
                                last edited by

                                I kept with 4 spaces to begin with "just because" it seemed to be the norm.
                                But trying out 2 spaces - I kind of like it.

                                Like Dan says - the editor I use, Notepad++, adds hints to where the blocks starts and end. Those let me easily follow the indentation level with only 2 spaces.

                                indent.png

                                As for deep nesting, I find them just as hard to read regardless of how far they are indented.
                                On a sidenote of that - I find it hard to follow code if it's fragmented into too many methods. If a deeply nested loop fragments into many methods for the only sake of avoiding nesting then it makes me jump up and down in the document just to follow the flow of the code.

                                Thomas Thomassen — SketchUp Monkey & Coding addict
                                List of my plugins and link to the CookieWare fund

                                1 Reply Last reply Reply Quote 0
                                • Dan RathbunD Offline
                                  Dan Rathbun
                                  last edited by

                                  @mitcorb said:

                                  From a total outsider's point of view, I would like to thank you guys for discussing this matter of indentation in coding, ...

                                  😉 I started this thread because things seemed to slow down a bit in here this week, and I knew it would be an interesting discussion. [insert Mutley laugh here.]

                                  Truth is, I have not decided what, if any certain indent, is wrong/correct, too big/too small, etc.

                                  All I know is, at this time, I LIKE 2 spaces for Ruby (and most everything.) HTML in particular, (I don't know why,) but it really seems the most wastful and ugly with 4 or more space indents.

                                  Anyhow... what I was hoping for was specfic, nitty-gritty reasons for the 4 space (and no 2 space,) 'convention'.

                                  I'm not here much anymore.

                                  1 Reply Last reply Reply Quote 0
                                  • mitcorbM Offline
                                    mitcorb
                                    last edited by

                                    Thanks, Dan:
                                    Your leading question "Why certain amount?" is almost exactly my question the first time I saw Ruby code in this forum. That's why I thank you.

                                    Now, I will butt out and just "listen in" for a while.

                                    I take the slow, deliberate approach in my aimless wandering.

                                    1 Reply Last reply Reply Quote 0
                                    • Dan RathbunD Offline
                                      Dan Rathbun
                                      last edited by

                                      Elastic Tabstops

                                      Link Preview Image
                                      Tab stop - Wikipedia

                                      favicon

                                      (en.wikipedia.org)

                                      http://nickgravgaard.com/elastictabstops/

                                      I'm not here much anymore.

                                      1 Reply Last reply Reply Quote 0
                                      • tbdT Offline
                                        tbd
                                        last edited by

                                        Some time ago i liked 4 spaces ident, but nowadays i am on tab 4 - easy to jump to 2 if the code is too wide, smaller filesize.

                                        using RDE autoformat works great on my code and 'alien' code so I can don't care about identing much when I code.

                                        SketchUp Ruby Consultant | Podium 1.x developer
                                        http://plugins.ro

                                        1 Reply Last reply Reply Quote 0
                                        • thomthomT Offline
                                          thomthom
                                          last edited by

                                          I want elastic tabstops!

                                          Thomas Thomassen — SketchUp Monkey & Coding addict
                                          List of my plugins and link to the CookieWare fund

                                          1 Reply Last reply Reply Quote 0
                                          • 1 / 1
                                          • First post
                                            Last post
                                          Buy SketchPlus
                                          Buy SUbD
                                          Buy WrapR
                                          Buy eBook
                                          Buy Modelur
                                          Buy Vertex Tools
                                          Buy SketchCuisine
                                          Buy FormFonts

                                          Advertisement