可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Most of the blogs or tutorials or books have private methods at the bottom of any class/module. Is this the best practice?
I find having private methods as and when necessary more convenient. For example:
public
def my_method
# do something
minion_method
end
private
def minion_method
# do something
end
public
def next_method
end
This way I find the code more readable instead of scrolling up and down continuously which is very irritating.
Is there something terribly wrong in this approach? Is having private methods at the bottom not just a best practice and something else?
回答1:
The best practice in my point of view is to go sequentially and declare your methods without keeping private in point of view.
At the end, you can make make any method private by just adding: private :xmethod
Example:
class Example
def xmethod
end
def ymethod
end
def zmethod
end
private :xmethod, :zmethod
end
Does this justify your question?
回答2:
As others have already pointed out the convention is to put private methods at the bottom, under one private class. However, you should probably also know that many programers use a double indented (4 spaces instead of 2) method for this. The reason is that often times you won't see "private" in your text editor and assume they could be public. See below for an illustration:
class FooBar
def some_public_method
end
def another_public_method
end
private
def some_private_method
end
def another_private method
end
end
This method should prevent you from having to scroll up and down and will make other programmers more comfortable in your code.
回答3:
There's also the option to prepend private
to the method definition since Ruby 2.1.
class Example
def xmethod
end
private def ymethod
end
private def zmethod
end
end
Looking at the definition, you instantly know if a method is private, no matter where in the file it's defined. It's a bit more typing (if you don't autocomplete) and not all your def
s will be nicely aligned.
回答4:
I think that public methods is a some kind of interface of the object, and it's logical to place them on the most prominent place i.e. in the top of file.
回答5:
You don't need to put public
or private
above each method. I usually put all of my private methods at the bottom of my class. Also, don't have to explicitly say public
as methods are public by default. For example:
class FooBar
def some_public_method
end
def another_public_method
end
private
def some_private_method
end
def another_private method
end
end
回答6:
I'm coming from java background and I hate to have to scroll to see method type. I think it's insane that one cannot specify method visibility per method without ugliness. So I ended up putting a comment #private
before each suck method and then declaring private :...
.
回答7:
I don't like having to specify public or private for each method. Putting all private methods at the bottom lets me have a single instance of "private" per file. I guess it's a matter of taste.
回答8:
One style is to group methods together so that you only use private
and protected
once per class at most. Another style is to specify visibility right after the method definition:
class Example
def my_private_method
end
private :my_private_method
def my_public_method
end
end
As of Ruby 2.1.0 def
returns the method name as a symbol, so a more streamlined style is possible:
class Example
private def my_private_method
end
def my_public_method
end
protected def my_protected_method
end
private_class_method def self.my_private_class_method
end
end
(Note that we use private_class_method
for class methods -- otherwise we'd get NameError: undefined method
since private
expects an instance method. Even when using it as a macro like in the original example it only affects the visibility of instance methods.)
I like this inline visibility style best, as it allows you to organize methods as you wish. It decreases the risk of adding a new method in the wrong place and inadvertently making it private.
As for the class method syntax, you can handle it this way instead:
class Example
private def my_private_method
end
class << self
private def my_private_class_method
end
end
end
回答9:
It's a matter of taste I suppose, but I'd rather explicitly name each method private on the line after said method like so:
class Example
def cthulhu
end
def rlyeh
end
def foo
end
private :foo
def bar
end
private :bar
def baz
end
private :baz
end
回答10:
Dennis had the perfect answer, that is, when using ruby >=2.1, just prefix the def with private (or protected,public)
But I believe that it's now also possible to use private as a block
as in:
private begin
def foo
end
def bar
end
end
def zip
end