Time for a Holy War

Tags:

먼저, 그 구현이 거의 된게 없다는 Groovy와 요즘 한창 잘나가는 Ruby의 비교에 대한 글이 artima에 실려있습니다. 어디선가 Groovy 프로젝트 리더의 인터뷰를 봤었는데, 거의 ant의 task 확장의 예만 들고 좀 애플리케이션 같은 쪽에는 얘기가 없더군요. 더구나 자기 말로도 ‘serious’ 한데까지는 못쓴다고 인정하는 상태였습니다.

다음은 Michael Tsai가 포스팅한 Perl vs. Python vs. Ruby입니다. Python이야 구글로 간 귀도로 인해서 유명세를 한번 치뤘지만, 루비와 무엇이 다른가에 대한 언어적인 차이는 거의 없어보입니다. 하지만 VM 의 구현 레벨은 얘기가 전혀 다른 상태인 듯 하네요. 단! 올해 JavaOne에 JRuby 가 등장합니다. 흐흐. JRuby가 뜨면 대박납니다, 여러분. 제가 루비를 시작한 결정적 계기중 하나죠. (다른 하나는 metasploit이 펄에서 루비로 옮긴 것입니다.) 아무튼, Tsai는 세 언어의 비교를 위해 한가지 문제를 내고, 그 문제에 여러 코더가 달려듭니다. 파이썬과 루비에 대해서는 그다지 언급할 게 없지만 펄은 참 인상적인 패배를 보입니다.

Blazar라는 베테랑 펄 코더가 Tsai의 코드를 개선해 보입니다.

이랬던 코드가

#!/usr/bin/perl -w

use strict;

my @records = ();

foreach my $line ( <> )
{
    my @record = map {s/"//g; $_} split("\t", $line);
    push(@records, \@record);
}

my $EMAIL = 17;
my $CONTACTME = 27;
my $SKUTITLE = 34;

my @contactRecords = ();
foreach my $r ( @records )
{
    push(@contactRecords, [$$r[$SKUTITLE], $$r[$CONTACTME], $$r[$EMAIL]]);
}

@contactRecords = sort {$$a[0] cmp $$b[0]} @contactRecords;
@contactRecords = grep($$_[1] eq "1", @contactRecords);

foreach my $r ( @contactRecords )
{
    print join("\t", @$r), "\n";
}

이렇게 변했습니다

#!/usr/bin/perl
	
use strict;
use warnings;
	
my @records;
	
push @records, [ (/\"(.*?)\"/g)[17,27,34] ] while <>;
	
($\,$\")=($/,\"\t\");
	
print \"@$_\" for
sort { $a->[0] cmp $b->[0] }
  grep $_->[1] eq '1', @records;
	
__END__

굉장히 짧고 간략해보이죠? 그러나 Q라는 분이 다음과 같이 일침을 날립니다.

Blazar, while this is perfectly reasonable perl code for a seasoned veteran like you or me, it demonstrates perfectly why perl is such a horrible language for writing intuitive and maintainable code.

You cannot look at this code snippet and just “know” what it’s meant to do without reading ever single line and mentally parsing/executing it. Meaningful syntax and well named intermediate variables are essential to writing code that is “intuitive”. You shouldn’t have to comment every second line to make it clear why you are doing something.

You can write easily readable and maintainable perl code, but it’s something the language tries hard to prevent you from doing in it’s simplest form, as you have demonstrated.

Write Only Language의 전형을 보여준 베테랑 펄 코더 아저씨는 정규식 실력을 뽐내기 전에 ‘Don’t code, Write!’ 의 의미를 생각해봐야할 듯 합니다.