匈牙利表示法适用于 Perl 吗?
在 Perl 中,对任何事物的引用都是一个简单的标量,并且具有 $
标志。有时很难说它是什么样的参考。
我个人在引用的变量名前加上一个字母,表示引用类型。例子:
my $aValues = []; # arrayref
my $hValue = {}; # hashref
my $oValue = bless {}; # object
my $sValue = \(my $s = 'foo'); # scalarref
...
我见过这种表示法的支持者和反对者。你用它吗?它有什么缺点吗?
In Perl, reference to anything is a simple scalar, and has the $
sigil. It's sometimes hard to say what kind of reference it is.
I personally prefix variable names for references with a letter, which indicates ref type. Examples:
my $aValues = []; # arrayref
my $hValue = {}; # hashref
my $oValue = bless {}; # object
my $sValue = \(my $s = 'foo'); # scalarref
...
I've seen both supporters and opponents to such notation. Do you use it? Does it have any disadvantages?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯。 Arrayrefs 和 hashrefs 之类的东西最终不会像 Perl 数据类型经常出现的那样令人困惑。 (另外:
$oValue
?你能有多通用?它是什么类型的对象?)请给我语义单位匈牙利表示法:
$size_bits
,$size_bytes
、$size_kb
、$size_blocks
、$size_mb
、$size_gb
。底层数据类型?并不像每秒增加 3 个字节到 10 兆字节并得到 13 那样重要。这正是某种形式的匈牙利表示法有用的地方。Meh. Arrayrefs and hashrefs and the like don't end up being that confusing around here that the issue comes up for our Perl-data-types very often. (Also:
$oValue
? How generic can you be? What sort of an object is it?)Give me semantic-units Hungarian notation instead:
$size_bits
,$size_bytes
,$size_kb
,$size_blocks
,$size_mb
,$size_gb
. The underlying data type? Not as important as adding 3 bytes to 10 megabytes per second and getting 13. That is where Hungarian notation of some form is useful.我个人觉得匈牙利符号很难阅读。我只使用英语约定:
我通常将对象视为标量,因为它的使用方式足以明显表明它是一个对象。但是,如果我发现需要,我会使用:
Personally I find hungarian notation hard to read. I just use English conventions:
I usually treat objects the same as scalars because how it's used makes it obvious enough that it is an object. However if I ever find a need to I'd use:
如果您想要静态类型语言,您知道在哪里可以找到它们...
但是匈牙利表示法从来就不是用于技术数据类型的(尽管它已经赢得了大部分被误解并应用于技术数据类型的坏名声)。最初发明它的匈牙利人(Charles Simonyi)希望它能够传达语义信息,例如在处理表格的代码中使用
row
和col
前缀这样您就不会混淆行索引和列索引。If you want statically typed languages, you know where to find them...
But Hungarian notation was never intended to be about technical data types (though it has earned most of its bad rep being misunderstood and -applied for technical data types). The Hungarian who originally invented it (Charles Simonyi) intended it to convey semantic information, e.g. using
row
andcol
prefixes in code that deals with tables so that you don't mix up row and column indexes.匈牙利表示法的目的是嵌入
从变量名称的 。现在,由于 Perl 没有静态类型系统,因此匈牙利表示法似乎应该相当常见。但是,以匈牙利表示法的价值为例:跟踪不可信数据的来源。
匈牙利表示法经常被引用的示例之一是在应用程序中的所有字符串变量前加上
s
或u
前缀(对于 safe 和 < em>不安全),具体取决于字符串是否来自受信任的来源(或已被清理)或不受信任的来源。但是:只需将 unsafe 替换为 tainted,你就可以得到 Perl 中污染的完美描述。因此,在这种情况下,即使 Perl 没有静态类型系统,它也有一个动态类型系统,允许您在语言中表达可信/不可信的语义,因此匈牙利表示法是多余。 (即使在没有内置污染支持的语言中,通常也有更好的方法,例如子类型(Python、Ruby、Smalltalk...)、注释/属性(Java、C#...)、元数据(Clojure,...)和静态类型(Haskell,ML,...)。)此外,Perl 非常具有表现力,因此将变量的整个上下文保留在您的头脑中(或在一个变量中)要容易得多满屏的代码),因此,语义通常从周围的代码中显而易见。
当然,好的命名也有帮助。
请记住:匈牙利表示法是为 C 发明的,它不具备表达能力,并且具有静态类型系统,其几乎唯一的用途是作为笑话的妙语。
The purpose of Hungarian notation is to embed semantic annotations that
in the names of variables. Now, since Perl doesn't have a static type system, it might appear that Hungarian notation should be fairly common. But, take the canonical example for the value of Hungarian notation: tracking the origin of untrusted data.
One of the oft-cited examples for Hungarian notation is to prefix all string variables in an application with either
s
oru
(for safe and unsafe), depending on whether the string came from a trusted source (or has been sanitized) or an untrusted one. But: just replace unsafe with tainted and you have a perfect description of tainting in Perl. So, in this case, even though Perl doesn't have a static type system, it has a dynamic type system that allows you to express the semantics of trusted/untrusted within the language and thus Hungarian notation is superfluous. (And even in a language without built-in support for tainting, there are usually much better ways, such as subtyping (Python, Ruby, Smalltalk, ...), annotations/attributes (Java, C#, ...), metadata (Clojure, ...) and static typing (Haskell, ML, ...).)Also, Perl is pretty darn expressive, and thus it is much easier to keep the entire context for a variable in your head (or within one screenful of code), so, often enough the semantics are apparent from the surrounding code.
Good naming also helps, of course.
Remember: Hungarian notation was invented for C, which isn't expressive and has a static type system whose pretty much only use is as the punchline of a joke.
我不这样做是因为引用的用法会清楚地表明它的含义。例如:
为变量选择能够传达其所代表含义的名称将会大有帮助。 Perl 是由语言学家设计的,借鉴了自然语言,因此上下文是关键。我们的大脑已经准备好进行这种处理。在构建代码时利用它!
I don't do this because a reference's usage will make clear its thingy. For example:
Choosing names for your variables that communicate what they represent will go a long way. Perl was designed by a linguist and borrows from natural language, so context is key. Our brains are already wired for this sort of processing. Make use of it in how your structure your code!