PHP in Action

Type hints are more useful for scalars than objects

dagfinn | 06 September, 2008 23:46

Max Horvath has implemented a library for type hinting scalars. That interests me, since I find that type hinting for objects has limited usefulness.

I tried using type hints extensively from an early beta version of PHP 5. I mostly gave up on them for three reasons:

  1. They make refactoring harder since you get lots of dependecies on a particular class name.
  2. Making sure an object is the right class has limited usefulness. If you pass an object of the wrong class to a method without type hinting, you're still alerted to the error the moment you try to call a non-existent method (one belonging to the class it was supposed to be as opposed to the class it actually is). This typically happens early.
  3. My experience is I hardly ever see actual bugs in practice that would have been caught earlier if there had been a type hint. I (nearly) always have good unit test coverage. It may be different if you don't, but that's not an advisable way to live.

I admit that these jugdments are hard to make. I could be wrong, more or less. Type hints are probably useful when code becomes stable enough and at the boundaries between modules. But I still tend to avoid using them until I get an actual bug that might have been prevented by a type hint. Their usefulness is and has to be an empirical question. The purpose of using them has to be catching errors earlier, so if they don't have that effect, there's no point.

For the purposes of this blog post, reason 2 is the important one. The idea is that type hints are more useful for arrays and scalars than for objects. Why? Because they have the potential to catch errors that would otherwise be hard to find or escape unnoticesd. As I said, if you pass an object of the wrong class, you will usually get a "non-existent method" error quickly. The same thing happens if you try to pass an array or scalar instead of an object. But if you try to pass an object instead of an array, an array instead of a scalar or vice versa, you can keep using the passed array or scalar for some time before it blows up. The problem becomes harder to track down. I've used type hints for arrays, and found them more meaningful and less troublesome than object type hints.

One case I've seen in which scalar type hinting would be useful is a simple homegrown date and time object that is initialized by passing a Unix timestamp as an argument to the constructor. A couple of times I've made the mistake of passing an object instead. The ensuing error can be hard to figure out. If I could type hint to make sure it's an integer, that would be helpful.

Comments

would have to heartily disagree

Mike Lively | 07/09/2008, 08:36

I suppose to some extent you are right about limited usefulness but in my opinion the value of them is that it can

1. point you in the right direction alot sooner. Imagine you passed the wrong type of object but it just HAPPENS to have a function of the same name? That could be relatively annoying, capable of passing unit tests, and quite possible wouldn't throw any errors. It would just result in strange behavior that may or may not be immediately apparent.

2. A call to a non-existing method is a fatal error. A call where the correct type is not passed in is a "catchable" fatal error. Now in practice this too probably doesn't mean much, but it does provide for more opportunities to recover or alert people to what is going on.

Also, to address point 1. I think a mistake alot of people make is to not use interfaces as much. If you have a method call that expects a certain type of object that has say 10 public methods. If you only really need access to 2 of those methods then there is a very good chance that you are only trying to utilize one aspect of that class that could be segregated into an interface. This would make your code much more flexible while saving you from the potential headaches in my previous point.

That all being said, scalar type hints would also be nice if they were native :).

interfaces

Mathias | 07/09/2008, 10:12

My team is working on a framework, and we're making more and more use of interfaces and type hinting. This way the method is basically saying: I don't care what the class the object has, as long as it's abiding by these rules. Although haven't actually encountered a lot of bugs that could have been prevented by object type hinting, using it gives me a warm feeling of 'less can go wrong'.

And yes, I don't understand either why php doesn't have native scalar hinting :-)

Re: Type hints are more useful for scalars than objects

Koen | 07/09/2008, 13:05

If you're not using the interface/type hint you have the dependency also. The difference is that now it is not explicit.

Re: Type hints are more useful for scalars than objects

dagfinn | 07/09/2008, 13:55

As I said it's a matter of judgment. It's not black and white. Interfaces (in the syntactical sense) may be a necessity if you really want to use type hinting, but ideally an interface should express a meaningful abstraction, and that abstraction is not likely to be stable in the beginning. In that case, I'm inclined to say it's better to avoid both the interface and the type hints rather than have an interface that's just a technical trick. I suggest that type hinting may be more useful for a stable interface, and that's just the kind of thing you will be seeking if you're developing a framework. As for dependency, I said dependency on the class (or interface, I should have added) name. There is a dependendency if you don't use a type hint, and it's less specific, not just less explicit.

Re: Type hints are more useful for scalars than objects

Alan Pinstein | 11/09/2008, 14:00

I was excited about type hinting when it first came out, but quickly abandoned using it.

The main problem I ran into was that you get a FATAL error if your argument doesn't match. This causes problems for applications that have highly dynamic runtimes and may at times generate the wrong objects. You can't easily catch the error since it's FATAL.

Instead, I do asserts at the top of my functions that throw exceptions if the improper data is passed.

Re: Type hints are more useful for scalars than objects

Mike | 12/09/2008, 01:10

One thing that (PHP-)developers tend to miss is that OOP is a lot about describing and strictly defining your architecture or class or function and not just about "let's pack some functions into a file and call it a class".

With a strictly defined code, you are able to just read and understand another developer's piece of code simply by looking at it. The other developer could also be you, revisiting your own code after months, weeks or just days.

And, more importantly, you can't break anything all too easily.

 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb