最近Stack Overflow上有一群讨厌perl的人,所以我想我应该把我的“关于你最喜欢的语言你讨厌的五件事”的问题带到Stack Overflow上。拿你最喜欢的语言来说,告诉我你讨厌它的五件事。这些可能只是让你烦恼的事情,承认的设计缺陷,公认的性能问题,或任何其他类别。你只需要讨厌它,它必须是你最喜欢的语言。
不要拿它和其他语言比较,也不要谈论你已经讨厌的语言。不要用你最喜欢的语言谈论你喜欢的事情。我只是想听到你讨厌但能容忍的东西,这样你就可以使用所有其他的东西,我想听到你希望别人使用的语言。
每当有人试图把他们最喜欢的语言强加给我时,我就会问这个问题,有时是面试问题。如果有人找不出他最喜欢的工具的5个缺点,那他对它还不够了解,不能提倡它,也不能利用它赚大钱。他还没有在足够多的不同情况下使用它来充分探索它。他把它作为一种文化或宗教来倡导,这意味着如果我不选择他最喜欢的技术,我就错了。
我不在乎你用什么语言。不想使用特定的语言?那就不要。你通过尽职调查做出了明智的选择,但仍然没有使用它?好吧。有时正确的答案是“你有一个强大的编程团队,有良好的实践和丰富的Bar经验。改成Foo是愚蠢的。”
This is a good question for code reviews too. People who really know a codebase will have all sorts of suggestions for it, and those who don't know it so well have non-specific complaints. I ask things like "If you could start over on this project, what would you do differently?" In this fantasy land, users and programmers get to complain about anything and everything they don't like. "I want a better interface", "I want to separate the model from the view", "I'd use this module instead of this other one", "I'd rename this set of methods", or whatever they really don't like about the current situation. That's how I get a handle on how much a particular developer knows about the codebase. It's also a clue about how much of the programmer's ego is tied up in what he's telling me.
憎恨并不是衡量人们了解多少的唯一尺度,但我发现它是一个相当不错的尺度。他们讨厌的事情也让我知道他们对这个话题的思考有多好。
Perl
Mixed use of sigils
my @array = ( 1, 2, 3 );
my $array = [ 4, 5, 6 ];
my $one = $array[0]; # not @array[0], you would get the length instead
my $four = $array->[0]; # definitely not $array[0]
my( $two, $three ) = @array[1,2];
my( $five, $six ) = @$array[1,2]; # coerce to array first
my $length_a = @array;
my $length_s = @$array;
my $ref_a = \@array;
my $ref_s = $array;
For example none of these are the same:
$array[0] # First element of @array
@array[0] # Slice of only the First element of @array
%array[0] # Syntax error
$array->[0] # First element of an array referenced by $array
@array->[0] # Deprecated first element of @array
%array->[0] # Invalid reference
$array{0} # Element of %array referenced by string '0'
@array{0} # Slice of only one element of %array referenced by string '0'
%array{0} # Syntax error
$array->{0} # Element of a hash referenced by $array
@array->{0} # Invalid reference
%array->{0} # Deprecated Element of %array referenced by string '0'
In Perl6 it is written:
my @array = ( 1, 2, 3 );
my $array = [ 4, 5, 6 ];
my $one = @array[0];
my $four = $array[0]; # $array.[0]
my( $two, $three ) = @array[1,2];
my( $five, $six ) = $array[1,2];
my $length_a = @array.length;
my $length_s = $array.length;
my $ref_a = @array;
my $ref_s = $array;
Lack of true OO
package my_object;
# fake constructor
sub new{ bless {}, $_[0] }
# fake properties/attributes
sub var_a{
my $self = shift @_;
$self->{'var_a'} = $_[0] if @_;
$self->{'var_a'}
}
In Perl6 it is written:
class Dog is Mammal {
has $.name = "fido";
has $.tail is rw;
has @.legs;
has $!brain;
method doit ($a, $b, $c) { ... }
...
}
Poorly designed regex features
/(?=regexp)/; # look ahead
/(?<=fixed-regexp)/; # look behind
/(?!regexp)/; # negative look ahead
/(?<!fixed-regexp)/; # negative look behind
/(?>regexp)/; # independent sub expression
/(capture)/; # simple capture
/(?:don't capture)/; # non-capturing group
/(?<name>regexp)/; # named capture
/[A-Z]/; # character class
/[^A-Z]/; # inverted character class
# '-' would have to be the first or last element in
# the character class to include it in the match
# without escaping it
/(?(condition)yes-regexp)/;
/(?(condition)yes-regexp|no-regexp)/;
/\b\s*\b/; # almost matches Perl6's <ws>
/(?{ print "hi\n" })/; # run perl code
In Perl6 it is written:
/ <?before pattern> /; # lookahead
/ <?after pattern> /; # lookbehind
/ regexp :: pattern /; # backtracking control
/ ( capture ) /; # simple capture
/ $<name>=[ regexp ] /; # named capture
/ [ don't capture ] /; # non-capturing group
/ <[A..Z]> /; # character class
/ <-[A..Z]> /; # inverted character class
# you don't generally use '.' in a character class anyway
/ <ws> /; # Smart whitespace match
/ { say 'hi' } /; # run perl code
Lack of multiple dispatch
sub f( int $i ){ ... } # err
sub f( float $i ){ ... } # err
sub f($){ ... } # occasionally useful
In Perl6 it is written:
multi sub f( int $i ){ ... }
multi sub f( num $i ){ ... }
multi sub f( $i where $i == 0 ){ ... }
multi sub f( $i ){ ... } # everything else
Poor Operator overloading
package my_object;
use overload
'+' => \&add,
...
;
In Perl6 it is written:
multi sub infix:<+> (Us $us, Them $them) |
(Them $them, Us $us) { ... }
Ruby有许多与速度相关的缺陷,但我并不讨厌它们。它也有社区传福音过度的缺陷,但这并没有真正困扰我。以下是我最讨厌的:
Closures (blocks) have 4 different creation syntaxes, and none of them are optimal. The elegant syntax is incomplete and ambiguous with hashes, and the full syntax is ugly.
The community tends to be against real documentation, favoring ‘read the code’. I find this childish and lazy.
Metaprogramming abuse, particularly in libraries, makes bugs a nightmare to track down.
On a related note, pervasive metaprogramming makes a comprehensive IDE difficult, if not impossible, to make.
The way block passing to functions is done is silly. There is no reason blocks should be passed outside the parameter list, or have odd special syntax to access (yield). I am of the opinion that blocks should have been given a less ambiguous syntax (or hashes could have used different delimiters; perhaps <> rather than {}), and passing as parameters to methods should have been just like all other parameters.
object.method(1, {|a| a.bar}, "blah")
These oddities, like the block must be the last parameter passed and passing more than one block is different with longer syntax, really annoy me.