Feeds:
Posts
Comments

Posts Tagged ‘rant’

Perl And Type Checking

(aka known as which language am I using again?)

Dave Rolsky has a new post on perl 5 overloading. It’s fairly informative, but it contains this little gem (emphasis mine):

If you don’t care about defensive programming, then Perl 5′s overloading is perfect, and you can stop reading now. Also, please let me know so I can avoid working on code with you, thanks.

Defensive programming, for the purposes of this entry, can be defined as "checking sub/method arguments for sanity".

Blanket statements like this really get my gripe up. Let’s have a look at defensive argument checking in Perl taken to its illogical conclusion.

Defensive Primitive Type Checking

use strict;
use warnings;

use Carp;
use Scalar::Util 'looks_like_number';

sub can_legally_drink
{
    my $age = shift;
    croak "$age is not a number" unless looks_like_number($age);
    return $age >= 18;
}

print can_legally_drink('x'), "\n";

And fantastic news! can_legally_drink correctly detected that my argument isn’t a number.

x is not a number at age.pl line 10
        main::can_legally_drink('x') called at age.pl line 14

But hang on a minute. Not all integers are ages. Surely we want to check if a real age type was passed in.

Checking For A ‘Real’ Type

My stripped down defensive age type might look something like this.

package age;

use Carp;
use Scalar::Util 'looks_like_number';

sub isa_age
{
    my $arg = shift;
    return ref($arg) and blessed $arg and $arg->isa('age');
}

sub new {
    my ($class, $years) = @_;
    croak "$years is not a number" unless looks_like_number($years);
    bless { years => $years }, $class;
}

sub years {
    return $_[0]->{'years'}
}

sub less_than {
    my ($self, $other) = @_;
    croak "$other is not an age" unless isa_age($other);
    return $self->years() < $other->years();
}

And then my calling code can look like this:

package main;

sub can_legally_drink
{
    my $age = shift;
    croak "$age is not an age" unless $age->isa('age');
    return ! $age->less_than(age->new(18));
}

print can_legally_drink(age->new(18)), "\n";
print can_legally_drink(18), "\n";

And woohoo, the second line throws an error as I wanted.

Actually, I don’t write Perl like this. Dave, you probably want to avoid working on code with me, thanks.

Moose

To be fair, Rolsky is talking his own book. Moose has a bunch of stuff that handles all this type checking malarky nice and cleanly. If you’re building something big in Perl, you should take a look.

But if you really care about types, I mean defensive programming that much, you could use a statically typed language instead and then you even get fast code thrown in for free.

Read Full Post »

boor
n.
1. A person with rude, clumsy manners and little refinement.

A couple of surprising attacks from Zloyrusskiy on my recent posts. I’m honour-bound to respond in kind.

Your problems with Perl programs is in not so good understanding of the language[sic]. Try to understand it[sic] principles or write your programs in python if deems[sic] more appropriate.

P.P.S. Author of post, Perl is not for you, it requires a sharp mind and intellect (and knowledge of the syntax of course). Write your programs in python and everything will be fine.

Dude, as one of the rare multilingual1 Engländers, I feel fully qualified in saying this: Your English is poor and unidiomatic. Despite this, I don’t say "Stop speaking English. Try Russian mate, English clearly doesn’t suit you". No sir. I hope you’ll improve. The more people who speak English, the better [for me].

You can use your sharp mind and intellect to draw parallels with Perl.

Second example (about numbers) i never used in my life in any language =). I can not even imagine where this might come in handy, so you worry about this.

What do you mean here? You mean you have never used a language that protected you from adding a string to an integer? Python, Lisp, C++, Java (in fact any modern statically typed language) all do this automatically. Which languages have you used?

And you missed my point. I was saying that with subroutines in any languages, you can fix things about that language that you don’t like. I used Perl for the examples, but I could have easily used C or Emacs Lisp, or Python.

Maybe I should be more explicit about what I’m trying to say. Or maybe it’s just you.

I’m perfectly comfortable with the default behaviour of Perl hashes. And sometimes I’m fine with the fact that 1 + 'banana' == 1. But when I’m writing my nuclear reactor software, I like to protect against that type of error.

Thanks for the comments and for providing material for this post.


1. I can also get by in American and Australian

Read Full Post »

Super Languages – Redux

The great thing about the internet is that you can always find someone to disagree with the most self-evident things. It is an enduring mystery.

1+1 is not equal to 2

And I guess if I can’t come up with a counter-argument, your assertion is proved correct.

The unreasonable burden of proof

So, some guy came up with a bunch of cases where apparently language choice makes no difference. If I can summarise his cases, I think they were

  • interaction with [poorly designed] legacy systems
  • algorithmic research
  • limited platforms such as embedded systems, GPUs, parallel computing

And going back to the point I was trying to make

  • for a given problem, some languages will be better than others
  • libraries are (almost always) more important than languages, but selecting the language will determine the libraries

Legacy Systems

I’ve glued together plenty of legacy systems in my time. Some were written in Cobol, some were Pascal auto-translated into C, some were, shudder "modern" C++. I’ve gotta tell ya, I was seriously pleased to have libraries available.

Back in the day we were doing it with CORBA. These days I might hack something RESTful up with HTTP/ Twiggy. Or I could pass data around via the file system, or a database.

Anyway, this is stuff that is unlikely to be built in to the language. It is going to come as a library right? But for sure, if I pick a language that can interact with the file system, the database or sockets I’m going to have an easier job than if I don’t.

Brand New Micro-Processors

Does this even happen these days? I thought everything was a variant of Intel or ARM.

If I understand correctly, the argument is that if there is only a single choice of language (because, for example, it is a new micro-processor without a C compiler yet), then language doesn’t make a difference.

You know, I can’t agree with that. I can think of two possibilities.

Option 1.

Assembler (or assembly, whatever), is the best language for that microprocessor. Because otherwise I have to first implement the other language in assembly.

Option 2

Another language, let’s say C, is better than assemblyer. So first of all I implement C in assembler.

But wait a minute. Then I’ve admitted that one language is better than another.

Embedded Systems

And as for embedded. Why does everyone with a moderately complex embedded system start with Linux? Is it a) because Linux provides loads of libraries that are great for implementing embedded systems, or b) there is no b.

Algorithmic Research

I don’t have much experience in this area, but given the obvious flaws in the rest of the guy’s argument, I have no doubt that a few well chosen libraries can be very helpful. After all, in traditional sciences, progress is made by standing on the shoulders of giants. Why should it not be the same in computer science?

Yes, much of that work is now available in just about any decent language, but I think my point is plain.

No, no it isn’t plain. Sorry. What are you talking about? If languages now implement much of that work that shows that libraries help no?

Linking To Supporting Research

At the risk of making the same mistake, I can’t believe you (Jared) linked to that function point analysis as support. I don’t think much of the technique to begin with and that was a great example of why no one should. They included HTML (Why not include photoshop while you’re at it?) and SQL Forms beat everything else by nearly a factor of 2. Ugh.

I often see articles that begin with a line such as Numerous studies have shown that… and then strangely, the article doesn’t link to a single study. Kinda like the original article that I was refuting.

I linked to a study (I also saw a similar table in one of McConnell’s books) and you think that weakens my argument? So let’s see what their methodology was…

Where does the data come from? The gearing factors in this table were drawn from 2786 completed function point projects in the QSM database. As mixed-language projects are not a reliable source of gearing factors, only single-language projects are used. As an additional resource, the David Consulting Group has graciously allowed QSM to include their data.

Okay, that seems at least somewhat rigorous. But a handwavy "but but, HTML, SQL Forms" has convinced me they don’t know what they are talking about. Thanks.

I don’t know why they included HTML, or why SQL Forms is so good (I haven’t used it), but in specialised areas, I can easily believe a language (+ its libraries) can beat others by a factor of 2.

Read Full Post »

Super Programming Languages

Jonathan Edwards, in The Myth of the Super Programming Language makes the extraordinary claim that language choice makes little difference.

There is no reason to believe that language choice makes a big difference. People have been doing studies for decades, and there are no reproducible strongly positive results. The burden of proof is on the poster, who is making a surprising claim.

To be fair, I believe he actually intended to single out Haskell and Lisp and state that it is impossible to get an order of magnitude increase in effectiveness. I’m not sure he is right there either, but I’ll dispute the actual claim he made.

Proof By Obviousness

First of all, there are studies that show that different languages required a different amount of code to implement a certain amount of functionality. Take a look at the language / function point here. Smalltalk (28 lines/fp) is more than 5 times better than C (148 lines/fp) in this metric. And comments about APL not-withstanding, code that you don’t write takes the least time.

Second of all, it is hardly a surprising claim. In the leap from Assembler to C, I’m obviously going to save programming time by not having to write code to push my parameters on to the stack every time I want a function.

Moving from C to Java I don’t have to write [so much] memory management code. Java to Perl, I don’t have to specify my data structures ahead of time.

Each language change, there is code that I just don’t have to write any more1. How can that not make a difference?

I’m not smart enough to get Haskell. If I had to guess, I’d say that Jonathan isn’t either, and is mildly upset that folks can out-perform him by choosing these languages.

Perl is the most super programming language I know.

And just to demonstrate the amount of code I need to specify a simple data structure that I get for free in perl, here is some C++.

struct MyData {
    vector<string> list;
    map<string, string> dict;
};

MyData data;
if (data.dict.find("a") == data.dict.end()) {
    data.dict.insert(make_pair("a", "b"));
}
data.list.push_back("text");
// data.list.push_back(1.5);

The perl is clearly terser. Is it less understandable (assuming you know both languages)? No way – it’s clearer.

And I know I’ve been kinda mean with the heterogenous list.

my %data;
$data{dict}->{a} = 'b' unless exists $data{a};
push @{$data{list}}, 'text';
push @{$data{list}}, 1.5; #  :)

Choice Of Language Isn’t The Most Important Factor

As long as you don’t choose something crazy, like Intercal, or Assembler.

The important thing is that there are libraries that solve the majority of your problem. As many people have pointed out, a lot of development these days is gluing together libraries. For me, as much as 95% of the systems I write are third-party libraries.

And my favourite example of something I wrote that would have been much longer in either Perl or C++ is my Emacs Database Mode. Emacs Lisp isn’t a great language, although it is good enough, but for certain problems it has some amazing libraries.


1. Of course, there are trade-offs.

Read Full Post »

Caleb Cushing has spilled a lot of virtual ink writing about the obligations that come with releasing open source software.

Yes, I think I’ve proven that volunteering to do open source comes with a responsibility or obligation.

(emphasis mine)

I kid you not. That is what he thinks.

And people have responded reasonably. In my opinion.

What is Caleb’s End Goal?

HUH? you people need to READ what I SAID and STOP reading INTO it.

I can think of three possibilities.

  • He likes getting comments on his blog (I can’t blame him)
  • He want open source authors to do more than they are already doing
  • If something is released [on CPAN?] that isn’t up to a certain standard, he would prefer that it wasn’t released.

More Comments

He has started closing commenting on his posts so this can’t be the reason. He could still be deriving satisfaction from high numbers of visitors though.

Open Source Authors do More

This is unrealistic. If we1 wanted to do more, we would be doing it already. If we had the time and the inclination to improve the quality, improve the documentation, fix the bugs…

Basically, someone whining, isn’t going to make people increase the amount of effort they put in which is probably already at the limit of what they want to do.

Don’t Release Imperfect Code

Or when is nothing better than something?

Chromatic mentioned one case in the opposite of modern. When an example is actively bad it reflects badly on Perl. But I think that a programmer who is motivated enough to release code to CPAN will be good enough to not write bad perl.

When a novice writes poor perl examples and others learn from those examples, it reflects poorly on perl. With CPAN, the natural barrier means that a novice is unlikely to be able to upload a module before they become more adept.

Of course, as Dave points out, a lot of what is on CPAN is not exemplary despite the fact that the authors are not novices. However, a certain amount of bad perl is certain to make it onto the internet. Should we exclude all imperfect code and lose perfectly adequate modules?

edit: edited for clarification.

I consider the cases of Authen::SASL, POE and AnyEvent that I’ve relied on recently. Now, in all those cases, the documentation has varied from pretty good to excellent (and they also have handy test cases). However, even if they had no documentation at all, it is surely better to have a working example that you can trace through with the Perl debugger than it is to have to write it yourself from scratch.

In the worst case, if the code is completely unusable I have to write to myself from scratch, but I would have had to do that anyway if the code had not been released. And if I’m not a developer I still wouldn’t have been better off without the code.

So Caleb, consider what you’re asking for. You might think that it would be great if you don’t get perl modules released to CPAN that are a bit rough around the edges, but I assure you that you’re wrong.


1. Yeah, I said we rather than they, even though my own contributions are limited.

Read Full Post »

Removed from Planet Emacsen

Now that I write mainly about p.erl I had considered not publishing this post, but I’m curious to know the reason if any and email to the planet emacsen maintainer, Edward O’Connor, has gone unanswered.

A couple of years ago when I started blogging about emacs I asked to be included in planet emacsen. And now I have been removed. What can be the reason for that I wonder.

Well, I have just come back from a blogging break. Between my post on sorting records with emacs in August 2009 and my post on Enablers and Obstructors in March 2010, there were almost 7 months. But the latest post in Amortized Analysis (a blog that hasn’t been removed from planet emacsen) was 4th January 2008. That is more than 2 years ago.

Maybe I’m not that relevant anymore. I have posted mainly P.erl posts since my break. But I do still do emacs stuff, e.g. Directory Aliases Revisited and Programming with Types. And Alexandre Vassalotti’s most recent Emacs post was September 2007.

Or was it the back and forth with Ian Eure (and to a lesser extent, Aaron Hawley) about my Emacs Database Mode. Did someone ask for me to be removed?

Anyway, whatever the reason, it has been done. What solution do I have? I have created a yahoo pipe called Better Planet Emacs which you can subscribe to. It filters a lot of the non-emacs related posts out so for me at least it is better than stock planet emacs. If you want to copy it and customise it for your own use you should go here.

Read Full Post »

Follow

Get every new post delivered to your Inbox.