0

I thought I understood the rules on indenting but almost every question I ask on S/O ends up with a comment "indent your code correctly". Just want to clear this up once and for all. This is a snippet from a project I am working on. What's indented correctly and what isn't?

  def display
      print "   1     2     3     4     5     6     7"
      @board.each do |i|
          print "\n"
          @draw = "+-----+-----+-----+-----+-----+-----+-----+"
          puts @draw
          i.each do |n|
              print "|  #{n}  "
          end
          print "|"
      end 
      print "\n"
      print @draw
  end
Jacob Moore
  • 277
  • 2
  • 12
  • I think it comes down to personal preference. But a general personal preference of most people (including me) seems to be: Indent using spaces with Tab Width: 2, at least for Ruby. While there is no real problem in the app, but using 2 spaces is more readable and might avoid conflicts when working with others. Also saves you from that look from older devs. – bishal Sep 20 '17 at 03:51
  • So other than the full tab I used to indent, the code is indented in the correct areas? I've been under the assumption that you on indent after anything with a "do, end" – Jacob Moore Sep 20 '17 at 04:03
  • 1
    Here's a style guide you can check through https://github.com/bbatsov/ruby-style-guide, but in general it's less an issue of 2 spaces vs 4 spaces or hard tabs vs soft tabs so much as some lines are indented when they shouldn't be and others aren't indented when they should and that ends up with code that looks like a `do` without an `end` or an `end` that doesn't appear to be lined up or closing anything obvious and then it just gets hard to tell what scope you're in and a whole host of issues can creep in un-noticed – Simple Lime Sep 20 '17 at 04:08
  • 1
    There's nothing huge with this code sample, but in [this one](https://github.com/jmooree30/Knights_Travels/blob/master/knights_travels.rb) posted in your [most recent question](https://stackoverflow.com/questions/46187888/keeping-track-of-the-path-knights-travel), if someone is glancing through that code and then are asked what the `end` on line 32 is closing, I feel like more people would get it wrong than correct, until they sit down and start kind of manually indenting things to notice that it actually closes `class Knight` and `shortest_path` is intentionally defined outside the class – Simple Lime Sep 20 '17 at 04:12
  • 1
    So, in summary, just need to make sure you're `end` is at the same indentation level as whatever it's closing, whether it's a `class`, `def`, `if`, `do`, etc. – Simple Lime Sep 20 '17 at 04:19
  • This mostly looks fine, except: your lowest (left-most) level of indention is 2 spaces, when it should be 0. The different levels of indentation in your code are separated by 4 spaces, but the widely-accepted style in the Ruby community is to use 2 spaces. I'd suggest reading more Ruby code; like find a gem you use and just read the source code of it. – David Grayson Sep 20 '17 at 05:06
  • 2
    The easiest way to format your code correctly is to not format your code. Any halfway-decent editor has code formatting for Ruby builtin that follows the Community Coding Style Guidelines. Don't manually perform a task that a machine can do. (That's pretty much what it means to be a programmer.) – Jörg W Mittag Sep 20 '17 at 06:24

1 Answers1

-1

Here is the good link to follow all the rules of the indentation

https://github.com/github/rubocop-github/blob/master/STYLEGUIDE.md

Nimish Gupta
  • 3,095
  • 1
  • 12
  • 20