Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created June 27, 2011 15:23
Show Gist options
  • Select an option

  • Save joefiorini/1049083 to your computer and use it in GitHub Desktop.

Select an option

Save joefiorini/1049083 to your computer and use it in GitHub Desktop.
Indent methods under private?
class Blah
private
def method1
end
def method2
end
end
# vs.
class Blah
private
def method1
end
def method2
end
end
# vs.
class Blah
def public_method
end
private
def method1
end
def method2
end
end
@rwz

rwz commented Jun 28, 2011

Copy link
Copy Markdown

i'm using #1 most of the time, but just because it's something like a rails-default. actually think that #2 and #3 are making way more sense.

@zedtux

zedtux commented Jun 28, 2011

Copy link
Copy Markdown

#3

@hakanensari

Copy link
Copy Markdown

#4

@metaskills

Copy link
Copy Markdown

I just wanted to follow up that my vote for #2 is due to my editor being customized the scope selector and color for public/protected/private in a very distinct way. The comments that people put around these using the #2 or #3 variant are nothing more than a patch around most themes not doing this already (see attached image) http://twitpic.com/5i3gyn.

TL;DR, this whole problem could be traced back to poor editor themes.

@dmathieu

Copy link
Copy Markdown

#1, definitely.

@joefiorini

joefiorini commented Jun 28, 2011 via email

Copy link
Copy Markdown
Author

@metaskills

Copy link
Copy Markdown

@joefiorini That is possible, but is hard to agree with absolute certainty. I know that I personally would rather see a class with a few key public interfaces that manage a series of very complex operations in many distinct chunks of work aptly named in many private methods vs one long procedural private method.

@trans

trans commented Jun 28, 2011

Copy link
Copy Markdown

@metaskills it would be nice to see that for a number or editors. which editor do you use?

@joefiroini if you think private methods are bad I fear "you are doing it wrong". now you could just leave everything public and document the public API, but in either case many small methods are the proper approach. read http://thinking-forth.sourceforge.net/

@trans

trans commented Jun 28, 2011

Copy link
Copy Markdown

@hakanensari which one is #4 ?

@metaskills

Copy link
Copy Markdown

@trans I use TextMate, but I had to make a custom scope selector and color for those. I did that 6 years ago when I started to learn ruby. No editor or highlighter does it by default, that I know of.

@javan

javan commented Jun 28, 2011

Copy link
Copy Markdown

Used to be a #3, but recently switched to #1 to match the coding style at work.

@wusher

wusher commented Jun 28, 2011

Copy link
Copy Markdown

#3

@aslamnd

aslamnd commented Jun 29, 2011

Copy link
Copy Markdown

#1

@rlisowski

Copy link
Copy Markdown

#2 with comment eg.

private #################################### private

@trans

trans commented Jun 29, 2011

Copy link
Copy Markdown

@korin That's an interesting one. It reminds me that I had tried this a few times:

    ;; private ;;

@petebrowne

Copy link
Copy Markdown

#1 is the Rails convention (I think)
#2 makes the most sense
#3 is probably the most readable

I vote #3

@stan

stan commented Jul 1, 2011

Copy link
Copy Markdown

#3

@meanphil

meanphil commented Jul 1, 2011

Copy link
Copy Markdown

#3

@slayyyyer

Copy link
Copy Markdown

I use #2... It's not really too hard with syntax highlighting to pick out private methods.

@jocubeit

jocubeit commented Jul 2, 2011

Copy link
Copy Markdown

#2 for me.

It's a pity it's not a block to show it's applicability in a text editor with fold indicators (as demostrated by @metaskills)

Coming from the world of M$, I would prefer @trans initial musings... but would be just as happy with a block. Wishful thinking...

@seanhussey

Copy link
Copy Markdown

I go with #2, but stay consistent with pre-existing codebases. I've also made use of private :method_name (which @brianvh pointed out). I can't be sure, but I think I got that from something Dave Thomas once said, maybe in the PragProg Metaprogramming screencasts.

@mindscratch

Copy link
Copy Markdown

I do what @george does

@jeltz

jeltz commented Jul 16, 2011

Copy link
Copy Markdown

#2 makes the most sense since it is just a method invocation, while #3 is slightly less readable, and #1 I have never liked.

@tokland

tokland commented Dec 15, 2012

Copy link
Copy Markdown

I use #3 although I #2 is certaintly more accurate since "public/protected/private" are not special keywords but normal method calls. #1 makes me feel sick, indentation levels are too precious a thing to waste them like that.

@kotp

kotp commented Jun 2, 2013

Copy link
Copy Markdown
class Classy
  def initialize
  end

  private
  begin
    def private_method_1
    end

    def private_method_2
    end
  end
end

Someone wrote private do which wouldn't work, but got me thinking about the above example.

@hindenbug

Copy link
Copy Markdown

#2

@rafaltrojanowski

Copy link
Copy Markdown

#1 - according to Rails convention. Vim support this with vim-ruby and in vimrc: let g:ruby_indent_access_modifier_style="indent"

@joelhelbling

Copy link
Copy Markdown

@shved

shved commented Jul 24, 2016

Copy link
Copy Markdown

Indenting private section is good for huge classes where you can find some private method, trying to use it, and then realise that it is actually private. So I use #2 for small classes and #1 for huge classes with a lot of private methods.
#3 way seems very strange and meaningless.

@jedeleh

jedeleh commented Jul 31, 2017

Copy link
Copy Markdown

I've always used 1 but at my current job they use 2 so I've switched to that for the sake of consistency. I don't feel strongly but prefer 1.

However, I really dislike 3 as it visually interrupts the scope of the class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment