Page 1 of 5

How do you feel about type-hinting

Posted: Thu Jun 11, 2009 10:06 pm
by alex.barylski
I know some of you are hardcore type-less and others the opposite and others still, somewhere in-between. Personally I am somewhere in-between right now, and I'm always an extremist so while I am employing extensive type-hinting in my current framework, I have to constantly question myself if I am making the right move.

1. It's makes the dependencies explicit and clear so I can more accurately re-factor code later should I need to.
2. Makes static analysis and documentation a little easier
3. Makes type requirements explicit -- I know if I pass a registry to a request argument by accident (which I have done before and wondered what the bug was) type hinting will let me know and I won't be stuck wondering WTF is going on.

The negatives are of course, the explicit dependecy in a otherwise type-less language. What if someone wants to inject their own registry from framework ABC into the front controller? They would be forced into deriving from my base registry object, which isn't nessecarily a bad thing as they need to conform to my interface anyways...

I'm very strict so the more constraints I can impose on myself to ensure bug-free code the happier I usually am with the end resulting project.

Re: How do you feel about ytpe-hinting

Posted: Fri Jun 12, 2009 1:06 am
by Christopher
I say do which ever makes you happy. It has not been my experience that type hinting exposes errors or that not type hinting cause more errors. Obviously if you absolutely require that a parameter is of a class and duck typing in never allowed then type hint.

Re: How do you feel about ytpe-hinting

Posted: Fri Jun 12, 2009 1:04 pm
by alex.barylski
type hinting exposes errors or that not type hinting cause more errors
Hinting has never "actually" exposed an error for me... :P

If I gave that impression I'm sorry my bad...but in retrospect I can say I once passed a registry object to a response (or visa-versa) and because they both implemented the same abstract class with a getValue() method no errors were raised in the controller when I pulled on $request->getValue('name');

The two objects were identical in this regard and no errors were seen, but obviously a bug was introduced, which took me forvever to track down and fix.

When I came to the realization what had happened (like all errors) I sit down and try and discover the true root of the problem and employ practices which prevent that from ever happening again (if I wasn't so diligent in testing that error may not have been discovered for quite some time).

Type-hinting (I think) would prevent that from happening in the future, as the types would be different, despite both being derived from the same abstract class, the interface looked something like:

Code: Select all

Front::setRegistry(Request $request);
Front::setRegistry(Registry $registry)
Passing a object in reverse order would be picked up by type-hinting and an error triggered no???

Perhaps one of the other pros would be that of explicit dependencies...I can run a script like PHPDocumentor over my classes and figure out statically what objects my class is dependent on and refactor accordingly, without pulling out a class, test, debug, trial and error, repeat.

For those two reasons, I'm leaning towards a type-hinted design...
Obviously if you absolutely require that a parameter is of a class and duck typing in never allowed then type hint.
Having never read about duck-typing, from the Wiki:
In a duck-typed language, the equivalent function would take an object of any type and call that object's walk and quack methods. If the object does not have the methods that are called then the function signals a run-time error. It is this action of any object having the correct walk and quack methods being accepted by the function that evokes the quotation and hence the name of this form of typing.
It's not "absolutely require" so much as trying to prevent future errors from occuring due to human error...and the explicit-ness of it (ie: Documentation).

I dunno I'm on the fence...it'll make my sexy code look potentially nasty...when I go as far to make sure variables are similar length, pronounciation, etc...throwing in a type hint in a function like:

Code: Select all

function($name1, $name2, $name3);
Looks sexy, whereas:

Code: Select all

function($name1, Some_Type_Interface_Instance $name2, $name3)
Looks nasty and really bugs me. :P

Cheers,
Alex

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 2:19 am
by Ollie Saunders
Type hints are one of the worst features of PHP along with statics. Programmers like the safe certainty that you get from type hinting and the extra typing you have to do that makes you feel like your getting lots of serious work done. Ooh that feels good! It's almost as good as declaring getters and setters. Yummy yum yum.

*cough* Despite my tone, I am actually making a serious point here. I think the attraction people have to type hints is entirely psychological. What do they actually do for you? A type hint is a constraint. It prevents you from doing something. If you like the documentation value it adds why not write a comment instead? Only you won't, because once you write /* expects SomeClass */ you'll realise that there's a possibility that comment will go stale and become untruthful. And if you acknowledge that, then you acknowledge that at some point your going to have to go back to a type hint and change it or remove it so you can actually do what you want to.

Of course the really obvious problems with type hints occur when you start mocking. You can't pass in mocks to type hinted things unless you declare an interface, type hint the interface, and create the mock from the interface. Which for a while -- like a good little boy -- I obligingly did; until I realised the madness of the magnitude of the maintenance burden I was manufacturing. The typing felt good though. :-D

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 4:53 am
by allspiritseve
Ollie Saunders wrote:Of course the really obvious problems with type hints occur when you start mocking. You can't pass in mocks to type hinted things unless you declare an interface, type hint the interface, and create the mock from the interface. Which for a while -- like a good little boy -- I obligingly did; until I realised the madness of the magnitude of the maintenance burden I was manufacturing. The typing felt good though. :-D
Or just use SimpleTest's Mock::Generate with an existing class. Then you don't even have to write a mock.

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 6:20 am
by Ollie Saunders
Or just use SimpleTest's Mock::Generate with an existing class. Then you don't even have to write a mock.
I do. It still won't get past type hints, unless you mock an interface.

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 8:33 am
by allspiritseve
Ollie Saunders wrote:
Or just use SimpleTest's Mock::Generate with an existing class. Then you don't even have to write a mock.
I do. It still won't get past type hints, unless you mock an interface.
You should be type hinting an interface anyways. If the class you're generating from implements the interface, you should be fine. Does that not work for you?

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 10:12 am
by Weirdan
Ollie Saunders wrote:Of course the really obvious problems with type hints occur when you start mocking. You can't pass in mocks to type hinted things unless you declare an interface, type hint the interface, and create the mock from the interface.
In PHPUnit you just generate mock from existing class (unless class is defined as final) :

Code: Select all

 
class A {
   public function b(C $e) {
       $e->doD();
   }
}
class C {
   public function doD() {
   }
}
 
class ATest extends PHPUnit_Framework_TestCase {
      public function setUp() {
           $this->c = $this->getMock('C');
           $this->a = new A;
      }
      public function testAAcceptsMock() {
           $this->c->expects($this->once())->method('doD')->will($this->returnValue(false));
           $this->a->b($this->c);
      }
}
 

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 12:23 pm
by alex.barylski
I think the attraction people have to type hints is entirely psychological.
I disagree. The technical benefit is that the engine picks out type-mismatches automatically for you. Like in the case I gave above, would not have happened had I been using type-hints.
A type hint is a constraint. It prevents you from doing something.
Thats kind of the point, yup. :)
If you like the documentation value it adds why not write a comment instead?
I haven't commented my code in years...it's practice I feel has become obsolete...it obfuscates my code...self describing code is how I roll. :)

Besides comments are much harder to extract, whereas reflection would make figuring out a function parameter a breeze.

Re: How do you feel about ytpe-hinting

Posted: Sun Jun 14, 2009 6:23 pm
by Jenk
"Type safety" sucks donkey balls, period. It's all a messy work around for a lazy compiler, and PHP are jumping on the bandwagon just "because other languages do it."

Re: How do you feel about type-hinting

Posted: Sun Jun 14, 2009 6:31 pm
by allspiritseve
Jenk wrote:"Type safety" sucks donkey balls, period.
Care to back up that statement? What specifically do you think are the disadvantages of type hinting?

Re: How do you feel about type-hinting

Posted: Mon Jun 15, 2009 5:36 am
by Jenk
Any and all that are listed any and everywhere on the web. There is no proof it helps, only that it hinders.

Re: How do you feel about type-hinting

Posted: Mon Jun 15, 2009 7:59 am
by alex.barylski
Any and all that are listed any and everywhere on the web. There is no proof it helps, only that it hinders.
Well I would have to disagree as my own experience has at least 'hinted' (no pun intended) otherwise

Re: How do you feel about type-hinting

Posted: Mon Jun 15, 2009 8:17 am
by Jenk
A specific, and definitive example of problems from type hinting:

Microsoft's LINQ to SQL library.

http://www.google.co.uk/search?q=linq+to+sql+testing

Impossible to mock because of type hinting. There are cries of "It's improperly implemented because it type hints concrete final classes, not interfaces" but boo hoo. If .NET did not so arbitrarily and absolutely require type hinting, you'd not have this problem.

Re: How do you feel about type-hinting

Posted: Tue Jun 16, 2009 7:57 am
by alex.barylski
Impossible to mock because of type hinting. There are cries of "It's improperly implemented because it type hints concrete final classes, not interfaces" but boo hoo. If .NET did not so arbitrarily and absolutely require type hinting, you'd not have this problem.
The 4th and 5th articles/blogs apparently describe how to do exactly this, don't they?

Anyways, I'm not sure I understand why an interface that used type hinting in PHP would fail to mock.

Code: Select all

setRegistryObject(MyApplication_Registry $registry)
When you mock an object, does PHPUnit not just wrap the original object using extension or do they usually create the mock using composition and delegates?

As I understand/experienced so long as the mocked object (or any object) is of 'type' MyApplication_Registry everything should be hunky dory...

Cheers,
Alex