1

I was going through the documentation of Facebook's Hack language and it said one of the advantages is refactorability. Why is Hack more easily refactorable than PHP just because it is partially typed?

gog
  • 10,367
  • 2
  • 24
  • 38
user1210233
  • 2,730
  • 5
  • 24
  • 31
  • 1
    I'm not necessarily sure that it is – Mark Baker May 27 '14 at 10:35
  • Note that virtually all languages are *typed*. There are very few languages which have no type system at all. PHP certainly is typed too. With the right tools and possibly annotations it can be almost as strictly typed as other "typed" languages. Certainly for the purposes of refactoring. – deceze May 27 '14 at 10:38

2 Answers2

2

First of all, PHP is typed. It is not statically typed, but it certainly has a well defined type system. Different type systems have different goals, but generally types are there to label parts of your code as being a certain "thing" and label other parts of your code to work with certain "things". E.g. a variable is labeled as "this is an int" and another function is labeled as "this parameter must be an int".

How and where and when those labels are used differs between languages. PHP is pretty lenient, allowing you to pass strings to functions labeled as requiring ints, because PHP's type juggling will handle such incompatibilities on the fly (to some extend or another). That still means you need to pass values which are generally compatible with the expected type though. You can't pass an array to a function expecting an int, because this makes no sense.

Now, tools can use this type information to help you in constructing valid code. Your IDE can warn you if you're trying to pass what it knows to be an array to a function it knows requires an int. That's a bug caught very early on. If your IDE doesn't do it, a compiler or static analysis tool may. That's a bug caught a little later, but still caught. Though PHP is not typically run through a compiler before being executed. The last resort is the runtime system throwing an error when it encounters incompatible types, which is quite late and may be hard to catch and debug.

Therefore, languages which have strict type annotations provide a lot of information to many tools along this chain and help catch errors early. Certainly not all error in all circumstances, but they can help eliminate a large class of errors early on. When you refactor code, this can be very helpful to avoid trying to stick two incompatible pieces of code together. Very good tools may even be able to reason about your code structure based on those compatibility meta-information and suggest simplifications or restructuring.

But, even in plain old PHP you can get the benefits of type annotations. Many IDEs support comment annotations such as:

/**
 * @param int $bar
 * @return string
 */
function foo($bar) {
   ...
}

This provides the same type information as this in Hack:

function (int): string

Except it's not baked into the language itself and may or may not actually be correct. But it provides the same benefits in that tools like your IDE can help you with your code based on this information. Good IDEs will even detect types on their own based on reasoning about the code and even point out discrepancies between annotations and your actual code.

So, a language like Hack, which has strict type annotations baked in, comes with the benefits of an explicit type system out of the box and enforces these types as part of the compilation process, which helps catch certain problems earlier before the code is actually run. Plain PHP only has informal type annotations* which external tools may or may not use and will only catch actual problem at runtime. However, these tools do exist, so overall it's pretty much a wash, IMO.


* PHP does have full formal type hinting support for its OO components, and for some complex types like arrays and callables. But again, those are only used during runtime by PHP itself; you're dependent on 3rd party tools for pre-runtime analysis.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

I don't use Hack, but as far as I can tell from their documentation, they allow very precise type annotations, like

function(int, int): string // function that takes two ints and returns string

or

array<string, int> // array with string keys and int values

Together with parametric types, this makes a rich type system, comparable with C# or Haxe.

The opinions as to whether static typing actually helps refactoring are mixed. Definitely, it enables IDEs to do fancy things with your code.

gog
  • 10,367
  • 2
  • 24
  • 38