Indeed, the Smalltalk way is to put private methods in the 'private' category. This indicates that you shouldn't use these methods, but of course doesn't enforce this.
This is by design - it's a feature, not a bug. Smalltalk was designed from the beginning precisely to be an open system.
Some advantages:
If I simply have to - maybe the library designer didn't foresee a need to expose some particular thing I simply have to have - I can still call those private methods. Obviously, this isn't something one does lightly: rather, judiciously, cautiously, knowing that it's a tactical solution.
Language simplicity.
(As per Alexandre Jasmin's comment) Smalltalk makes no distinction between what you, the programmer, can do and what the language/environment can do. That means that Smalltalk-the-image exposes all the things needed for you to build your own inspectors/debuggers/whatever without having to supply special tools using we-can-do-this-but-you-can't techniques.
Private and protected methods are in fact a significant weakness of languages like c++, java and c#. They basically say to their users: I don't want to learn and evolve. The consequence of that (and a lot more early binding) is that those languages require much more BDUF and are thus far less usable for a modern (agile) development process.
The first question is what private/protected access modifiers are about? Fundamentally, it is not about safety or security. It is about exposing the right interface to the user. Starting from that, it makes little difference between having categories protected/private and a language construct specifically for that.
I would even say that having private/protected visibility modifier brings more complexity to the problem than it actually solves.
Besides that, I don't think that private/protected visibility is a good answer to this problem
At the least, Smalltalk should have the textual convention that method names that begin with 'underscore' are verboten to call outside of the objects themselves. Unfortunately, I don't think that 'underscore' is allowed as the first character of a method name.
发布评论
评论(4)
事实上,Smalltalk 的方式是将私有方法放在“私有”类别中。这表明您不应该使用这些方法,但当然并不强制这样做。
这是设计使然 - 这是一个功能,而不是一个错误。 Smalltalk 从一开始就被设计为一个开放系统。
一些优点:
Indeed, the Smalltalk way is to put private methods in the 'private' category. This indicates that you shouldn't use these methods, but of course doesn't enforce this.
This is by design - it's a feature, not a bug. Smalltalk was designed from the beginning precisely to be an open system.
Some advantages:
事实上,私有方法和受保护方法是 c++、java 和 c# 等语言的一个重大弱点。他们基本上对用户说:我不想学习和发展。其结果(以及更多的早期绑定)是这些语言需要更多的 BDUF,因此对于现代(敏捷)开发过程来说不太适用。
Private and protected methods are in fact a significant weakness of languages like c++, java and c#. They basically say to their users: I don't want to learn and evolve. The consequence of that (and a lot more early binding) is that those languages require much more BDUF and are thus far less usable for a modern (agile) development process.
第一个问题是私有/受保护的访问修饰符是什么?从根本上来说,这与安全无关。它是关于向用户公开正确的界面。从这里开始,保护/私有类别和专门为此构建的语言结构之间没有什么区别。
我什至会说,拥有私有/受保护的可见性修饰符会给问题带来比它实际解决的更复杂的问题。
除此之外,我不认为私有/受保护的可见性是解决这个问题
The first question is what private/protected access modifiers are about? Fundamentally, it is not about safety or security. It is about exposing the right interface to the user. Starting from that, it makes little difference between having categories protected/private and a language construct specifically for that.
I would even say that having private/protected visibility modifier brings more complexity to the problem than it actually solves.
Besides that, I don't think that private/protected visibility is a good answer to this problem
至少,Smalltalk 应该有这样的文本约定:以“下划线”开头的方法名称禁止在对象本身之外调用。不幸的是,我认为不允许将“下划线”作为方法名称的第一个字符。
At the least, Smalltalk should have the textual convention that method names that begin with 'underscore' are verboten to call outside of the objects themselves. Unfortunately, I don't think that 'underscore' is allowed as the first character of a method name.