Perl vs. PHP

Perl is almost obsolete. This makes me sad given how much Perl I know. Not a year ago, I loved Perl. The power I could command at just a few characters! The language flexibility! I love being able to create my own custom ”dialect” of Perl for any given problem. It’s neat, and fast, and sometimes it’s even elegant.

But not really. It’s never actually elegant in the “easy to understand” sense. Plus, Perl is not an easy language to understand in general. The syntax for deeply nested references can confound the newbie Perl programmer for a very long time. And the distinction between variable types ($, @, %) seemed like a neat distinguisher… until I used PHP more.

PHP is like a really stripped down version of Perl. But it has much better OOP support. Like, tons better. I miss some things, like enforced variable declaration. And Perl’s variable scoping is more intuitive. Perl has better warnings for some things, too. But PHP’s simpler syntax proves more elegant while providing the same level of flexibility. It doesn’t have the beautiful maleability of Perl… but then again, it’s pretty maleable in its own ways. It’s not intended to be an “only one way to do things” language like Python.

One of Perl’s great strengths for me has been its nifty tricks for file I/O and common maintenance-scripting tasks. But Perl 5 has been stagnating while PHP has been growing by leaps and bounds. Most of the neat operators in Perl now have built-in function equivalents in PHP. Often they are even better than Perl’s version. Inevitably they are easier to comprehend. I’ve actually started using CLI PHP for quick one-off scripts. My uses for Perl are waning very quickly.

A year ago, when I was an active Perl hacker, I was angry about Perl 6. Perl 6 obsoletes all Perl 5 code. It’s not backwards compatible at all. “What is the point of this?” I wondered. Perl 6 has many new features, and it cleans up a lot of the syntactic awkwardness of Perl 5. But… a whole new language under the guise of a version-number upgrade?

Now, I can’t wait for Perl 6 to actually arrive. (In a viable, not-dog-slow-beta form.) It will be another year or two, I think, before the language is viable. But I would love to love Perl again.

MySQL’s GROUP BY vs. MS SQL’s GROUP BY

I had a really meaty post here comparing MySQL to MS SQL. But I realized how wrong it was, and changed it before it went live. I had been extolling a neat feature of MySQL, but ended up extolling a safety check of MS SQL instead.

I learned MySQL first, and only more recently started working with Microsoft’s SQL Server. There are a lot of little differences in the products — MySQL has lots of handy tools for small-scale web projects, with its built-in page-sizing LIMIT syntax, fast-prototyping features like ENUMs and SETs, and so on. It knows what it’s good at and it plays to those strengths. SQL Server has a decided advantage when it comes to stored procedures and views. Like, a huge advantage. I love MS SQL’s views. But it’s not a drop-in replacement… they each have very different abilities.

Here is where I was going to talk about MS SQL’s limitation on GROUP BY. But now it is not an either/or comparison … MS SQL’s version is just better. I just didn’t understand. MySQL’s error detection for GROUP BY just sucks and allows you to make big mistakes… and it led me to misunderstand what was happening under the hood.

Say you have this:

CREATE TABLE cakes (
    cake_id INT NOT NULL,
    cake_type INT NOT NULL,
    frostiness INT NOT NULL,
    creaminess INT NOT NULL
)

Now if you want to find the most frosty cake of each cake type, you can just say:
 
SELECT MAX(frostiness) FROM cakes GROUP BY cake_type

And you’ll get the highest frostiness level from each type of cake. In MySQL, you can do this too. But you could also say

SELECT MAX(frostiness), creaminess FROM cakes GROUP BY cake_type

It would proceed to spit out an arbitrary creaminess value from one of the cakes in each cake type. Which one? Who knows! In MS SQL, this is a syntax error. And that’s good.

Suppose you want to know all the data about the creamiest cake. Because I didn’t realize how MySQL chose the rows, I tried to do this:

SELECT *, MAX(creaminess) AS creamiest
FROM cakes
GROUP BY cake_type
HAVING creaminess = creamiest

Clever approach, huh? Group them by cake type, then use HAVING to choose only the ones with the highest creaminess. Except no, this just doesn’t work. What happens is it groups the rows by cake type, choosing one arbitrary row for each cake_type value. Each row does have the correct MAX(creaminess) value for the cakes in that category, but the rest of the data is not necessarily from the row where the MAX(creaminess) came from.

So this chooses some arbitrary rows, and then throws out a bunch of them if they don’t happen to be the ones with the highest creaminess for their type. Although it may seem to work (because it only outputs cakes with the highest creaminess for their type), it silently leaks cake types.

It’s just a malformed query. It’s not a bug. But in MS SQL, this is a syntax error.

Is there ever a time when you want an arbitrary row of data from a GROUP BY? Maybe… but I can’t think of one. Mostly, being able to access columns that aren’t in the GROUP BY statement is only going to introduce hard-to-track-down bugs in your code.

The Perfect Language

Who doesn’t want to write their own language? You liar! Down deep you want to. Everybody does. Everybody! I co-authored a language once before, and it was a big learning experience. (It was a private language for a game company, and it’s still in use today.) Afterwards I didn’t really feel the need to make a language again for many years. But the bug is coming back. None of the languages out there are quite good enough.

They lack compilability, for one thing. I appreciate the “no compiling” nature of scripting languages like PHP or Perl, believe me. But when the program gets big, when I have 20,000 lines of code to manage, I want to be able to syntax check the damned thing before I run it.

Consider this common case: you have a massive Python program embedded in your C++ app. You decide, hey, this function foo is too unwieldy. I’m going to break this into two functions that have different semantics and parameters: fooA and fooB. Now you search and replace every “foo” in your program and turn it into a call to fooA or fooB. And now, to test your work, you have to start up your C++ app and see if it crashes. And it will. You better hope your unit test suites can catch most of them…

Refactoring tools are supposed to help. But wouldn’t you know it, refactoring tools are only actually powerful in Java… which is a compiled language. The refactoring tools for, say, PHP or Python are pretty pathetic.

There are also tools in the same vein that are supposed to “syntax check” your code for you. However, the Python tools I used for this purpose were just not any good at it. I didn’t use lambda functions or dynamic class construction or anything like that, either. They couldn’t find simple typos in calls to predefined function names. Fail.

So the perfect language should be runnable as a scripting language AND should be compilable. For this to be useful, the language will have to lose some of the more outlandish features of modern scripting languages, such as the ability to dynamically add and remove functions from classes. If you can just add any function to a class, then there’s no way for the compiler to know if a syntax error is a syntax error, or just a function that won’t exist until run-time. Plus, it’s maddeningly hard to follow code that dynamically adds functions to classes. I won’t miss this.

I’ll need serious interface support. I guess I might as well have exceptions too. And the other usual suspects: dynamic list and hash support, nifty regex and text procession operators, probably some built-in web management APIs. Hmm, this will take 8 to 12 months to get to beta. That’s a long time…

And what’s the killer app for my language? Ruby was around for a long time before Rails came out and made people notice it. Rails is the killer app for Ruby. (More about Rails later… I’m already going off on a tangent.) But what’s my killer app for my language?

I guess I don’t have one. I don’t want a language that languishes. So I guess I’ll just not write one. It’s just not practical! It makes no sense to write my own language, and I don’t have the time. But it won’t stop nagging me! “Coooodddde meeeeee,” says the perfect language. “Coooddddde Meeeeeee!

Flex 3

If you haven’t played with Flex yet, maybe you should. It’s Adobe’s new way of making Flash programs. But instead of using an animation editor, you get to actually code in a real programming language. And it’s even a good language!

The Flex language has two parts. First there’s Actionscript, which is extremely similar to Javascript, but get this: it’s compiled! This makes it much easier to work with for large projects.

The other part of Flex is an XML format that lets you layout forms and whatnot. You can also embed Javascript right into the XML file, and most people do. So in a sense you might think of it as a replacement for HTML+Javascript — it’s XML+Actionscript. But it’s compiled, and without any browser compatibility issues.

97% of web users have Flash Player 9 and can run your Flex app. THAT is what’s so powerful about it.  Nothing else comes close to that level of acceptance. (Java is around 80%.)

The big weakness, to me, is the lack of 3D primitives in the Flash VM. But the next version of Flash (in beta now) has basic hardware 3D primitives! Which means the next version of Flex will be able to use those primitives. It’s a bit early to code your amazing game in Flex just yet, but in a year or two I think we will see an awful lot of them.

Flex is going to be really big with online games. And I am so there.

Haskell

Okay, I just want somebody to explain to me, in two paragraphs, what Haskell can do for me that is so awesome. I’m not being mean about this, I really want to know, and I think there’s a valid answer. But nobody will tell it to me. I understand that functional programming is very different. I get that it makes it easy to write recursive programs and re-think problems as recursive solutions. But I can’t see how that makes it a useful real-world language. It’s like the first time I saw OOP programming back in 1990 — “yes, but what is it GOOD for?” The books had a hell of a time telling me why I should bother learning OOP.

So to answer these questions, I bought “Programming in Haskell” by Graham Hutton. It’s the highest-rated Haskell book I could find on Amazon.com. And man if it didn’t make me hate Haskell pretty hard.

First off, the author doesn’t use actual code samples. He uses little random math symbols like arrows that point various ways, set operators, little wire diagram thingies, and so on. He calls them “Haskell symbols”, but the technical term for them is “pedantic fuck-you symbols” to anybody who already knows a programming language and wants to learn Haskell. I mean, you can’t even type in the samples! You have to refer to appendix B to translate each symbol into something typeable.

The ironic thing is that the actual typed symbols are very traditional. The conjunction operator is not a little hat, it is “&&”. The negation operator is not a line with a thingy on it, it is “not”. And so on. In general Haskell seems to use operators that are consistent with what programmers would expect. But if you read this book, you’ll think it’s a totally alien language.

So I am studying the book and he gets to the first interesting thing, curried functions. This is a very difficult concept to explain, but there’s only three paragraphs on curried functions and no relevant examples, so I just had to sit there studying those three paragraphs over and over. I still don’t think I really understand. I can smell power here but I can’t taste it. The book has failed me.

Eventually I stopped reading. I guess the target audience is mathematicians who want to learn to program. But if they start with this book, boy won’t they be surprised at how hard programming is! And if they already know any programming, this book will just confuse and mystify them. It’s a turd. Written by a professor who hasn’t actually written any real code in his life.

Unfortunately it’s also the most highly-recommended Haskell book available, so I’ve given up trying to learn Haskell for now. My impression of Haskell has been soiled by this book, but that’ll fade after a month or two.

If a book publisher is reading this, let me tell you what I want. In the first chapter I want you to tell me how Haskell can change the way I think about programming. Don’t just say that it will change everything… give me an example. I know that’s hard. But I know you can do it if you try. Hype it to me. Make me want to learn this language! I’m an easy mark. I love learning languages. I just need to be able to put it in context of what I can do with it.

Also, consider writing your examples with actual typeable Haskell syntax, because otherwise I’m not buying your book.