Home > Uncategorized > Hack, a template for improving code reliability

Hack, a template for improving code reliability

My sole prediction for 2014 has come true, Facebook have announced the Hack language (if you don’t know that HHVM is the Hip Hop Virtual Machine you are obviously not a trendy developer).

This language does not follow the usual trend in that it looks useful, rather than being fashion fluff for corporate developers to brag about. Hack extends an existing language (don’t the Facebook developers know about not-invented-here?) by adding features to improve code reliability (how uncool is that) and stuff that will sometimes enable faster code to be generated (which has always been cool).

Well done Facebook. I hope this is the start of a trend of adding features to a language that help developers improve code reliability.

Hack extends PHP to allow programmers to express intent, e.g., this variable only ever holds integer values. Compilers can then check that the code follows the intent and flag when it doesn’t, e.g., a string is assigned to the variable intended to only hold integers. This sounds so trivial to be hardly worth bothering about, but in practice it catches lots of minor mistakes very quickly and saves huge amounts of time that would otherwise be spent debugging code at runtime.

Yes, Hack has added static typing into a dynamically typed language. There is a generally held view that static typing prevents programmers doing what needs to be done and that dynamic typing is all about freedom of expression (who could object to that?) Static typing got a bad name because early languages using it were too disciplinarian in a few places and like the very small stone in a runners shoe these edge cases came to dominate thinking. Dynamic languages are great for small programs and showing off to spotty teenagers students, but are expensive to maintain and a nightmare to work with on 10K+ line systems.

The term gradual typing is a good description for Hack’s type system. Developers can take existing PHP code and gradually give types to existing variables in a piecemeal fashion or add new code that uses types into code that does not. The type checker figures out what it can and does not get too upperty about complaining. If a developer can be talked into giving such a system a try they quickly learn that they can save a lot of debugging time by using it.

I would like to see gradual typing introduced into R, but perhaps the language does not cause its users enough grief to make this happen (it is R’s libraries that cause the grief):

  • Compared to PHP’s quirks the R quirk’s are pedestrian. In the interest of balance I should point out that Javascript can at times be as quirky as PHP and C++ error messages can be totally incomprehensible to everybody (including the people who wrote the compiler).
  • R programs are often small, i.e., 100 lines’ish. It is only when programs, written in dynamically typed languages, start to exceed around 10k+ lines that they start to fall in on themselves unless that one person who has everything in his head is there to hold it all up.

However, there is a sort of precedent: Perl programs tend to be short (although I don’t think they are as short as R) and it gradually introduced the option of stronger typing. But Perk did/does have one person who was the recognized language designer who could lead the process; R has a committee.

Tags: , , , ,
  1. Oliver
    March 24th, 2014 at 17:28 | #1

    I got bogged down at about 2.5k lines of code in R. Mostly I blamed the dynamic typing and constant need to vectorize. I must not be a good R programmer.

  2. March 24th, 2014 at 18:07 | #2

    @Oliver
    R lacks any support for writing code in the large. Perhaps 2.5k is the R bounds, or perhaps it was your first experience of writing a large program (there are structuring techniques that make life a little easier and it usually takes a few goes to get the hang of them)?

  3. Andrew Dolman
    March 25th, 2014 at 11:53 | #3

    I guess strong typing acts like unit testing in a way – just that it only does one kind of test, and you don’t have to write extra code.

  4. Tel
    April 5th, 2014 at 08:23 | #4

    Oliver, the vectoring in R should work for you when you sit and think about it a bit. However R is designed for a particular type of numerical computing, so maybe the problem you are solving could be better suited to another language.

    You can of course just write a FORTRAN, C or C++ module to plug into R and isolate the difficult code inside that. Then you get the benefits of strong typing and more complex nested loop structures that might but useful to you.

  1. No trackbacks yet.


− (minus 0b000l ) = 71