How do you feel about type-hinting

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

You've misunderstood the problem with LINQ to SQL, and no, it's impossible to mock, the 5th article talks about testing stored procedures - not unit testing, and the 4th doesn't achieve what you might think.. notable at this point:
But we can't do this as IQueryable<T> does not support Add. DataContext actually gives us a Table<T> which both implements IQueryable<T> and supports the required functionality for add and remove; but we can't convert our List<T> into a Table<T> so we look to be out-of-luck with this.
It then refers to injecting a very low level mock and then completely altering your use of LINQ to SQL to make it testable.. something which is nothing more than a hack to work around the real problem.

So what has this got to do with PHP type hinting? A lot. Given PHP is copying this type of hinting. This is also a clear example of why hinting is a paint in the neck, and also why it is just developer paranoia to use them (to restrict flexibility in case of the unfathomable horror of someone making a mistake.)
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: How do you feel about type-hinting

Post by allspiritseve »

Jenk wrote: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.
Nor would you have the problem if they type hinted interfaces. With single inheritance you might not be able to extend from a type hinted class name, but any class can implement an interface. If you disagree, show me a PHP example that type hints to an interface, and show me how you can't inject a mock that satisfies the type hint.
Jenk wrote: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?
I know nothing about PHPUnit but SimpleTest extends the mocked class, and thus is type-safe.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: How do you feel about type-hinting

Post by alex.barylski »

This is also a clear example of why hinting is a paint in the neck, and also why it is just developer paranoia to use them (to restrict flexibility in case of the unfathomable horror of someone making a mistake.)
Sorry but I didn't find that example *really* conducive to my example.
So what has this got to do with PHP type hinting? A lot. Given PHP is copying this type of hinting.
I'm not sure I understand the problem clearly or how it pertains to my own example, but, generics are similar to C++ templates. Templates attempt to give a strictly typed language some dynamic ability, by making types 'generic'.

Something like the adapter/provider relationship in a DB abstraction layer. You implement a stack object as a template, and inject (for lack of a better word -- this is not accurate at least not in C++) the type you want that stack to assume, so if you wanted to store a list of integers, you might:

Code: Select all

ints<integer> = new Stack;
Likewise one might do something like:

Code: Select all

floats<float> = new Stack;
I wouldn't say PHP is copying this type of behavior, on the contrary PHP intrinsically supports this type of behavior, in fact, you must work in reverse of what PHP does by default by using a 'type-hint'.

I do not understand the problem example you provided well enough to comment further, but it sounds like the issue was being caused by the stricter type enforcement in languages like C# or .NET languages offer...

I think generics and type-hinting are quite different, enough so, that it is unfare to compare them.

Type-hinting (technically) simply triggers an error when you attempt to pass a function parameter the wrong 'type' (a safety net and nothing more) -- whereas generics do much more, as I described above.

I can successfully mock any object which I pass to a function in my own code, so long as the mock is derived from the base type or the actual argument 'type' everything should go as planned...

Cheers,
Alex
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

The problem with LINQ to SQL is explicitly because their methods type-hint concrete classes, and most of those classes are also 'final.'

Yes, it's a crap implementation, but type-hinting still allows this crap implementation to happen.

Why was it ever introduced? Compilers inability to handle unknown methods? Developer paranoia of run-time errors (indirectly due to compilers inability to handle those run-time errors, maybe?) Fancy IDE widgets?

Not having type-hinting allows FAR more flexibility and reduces maintenance immeasurably.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

as for PHP copying/not copying.. it most definitely is copying, given it's going from a language that never supported type-strict in anyway, to now having type-hinting, interfaces, etc.

It really does boil down to "because other languages also do it."

Don't get me started on namespaces.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: How do you feel about type-hinting

Post by alex.barylski »

Why was it ever introduced? Compilers inability to handle unknown methods? Developer paranoia of run-time errors (indirectly due to compilers inability to handle those run-time errors, maybe?) Fancy IDE widgets?
Because some people believe that constraints, when applied effectively are a good thing.

Letting developers go buck wild, clearly doesn't work either, especially with no education, they tend to build transation style scripts, with tight coupling and zero to none separtation of concerns.

Throw in an framework and the separations are at least promoted, but IMHO should never be forced.

Language tools, like type-hinting, const (in C/C++) or others all have ther place. I cannot think of a single instance where type-hinting in PHP would cause an issue, as it's quite limited in scope (only function arguments).
It really does boil down to "because other languages also do it."
That is pretty much every language available to man...in some way or another...but yea it's very likely that was the reason, not sound practical experience.
Don't get me started on namespaces.
I won't or I can't...I'm not a fan of namespaces...I prefer prefixing the class names with the parent directory named after the project anyways (Zend, PEAR, etc). I think it makes more sense and accomplishes the only thing I would use namespaces for, preventing namespace collisions between one or more frameworks. :P

Cheers,
Alex
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: How do you feel about type-hinting

Post by allspiritseve »

Jenk wrote:Yes, it's a crap implementation, but type-hinting still allows this crap implementation to happen. Not having type-hinting allows FAR more flexibility and reduces maintenance immeasurably.
Your argument that type-hinting "allows this crap implementation" to happen is not a very good one. SQL allows N+1 queries to happen among other things, do you complain about that? Technology can't always stop people from being stupid.

Not type hinting compared to type hinting a concrete, final class might reduce maintenance. But how much do you seriously have to maintain a properly type-hinted interface compared to no type hints? I would argue it probably reduces your maintenance, most of the time. If you need to pass in a class, implement the interface. If it collides with class's existing interface, wrap it in a decorator that implements the interface. In that situation you'd want to decorate it anyways. Just because the methods are the same doesn't mean they are used in the same way.

An interface is a contract between classes, and if you don't type hint, the contract is still there but it's not explicit. You may not understand why this and this method is required in an interface, but if the developer did their job right, they are making the contract between classes explicit and clear so you know exactly what you need to implement.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: How do you feel about type-hinting

Post by alex.barylski »

Not type hinting compared to type hinting a concrete, final class might reduce maintenance
I missed that point...in this case I can see type-hinting being a PITA if you were using a library of existing code.

You might then derive an class from ObjectA and pass in ObjectB but because the type is of type ObjectA and it's declared as final, you wouldn't be able to derive ObjectB from ObjectA in the first place.

In this case, I'm not sure I would blame type-hinting alone so much as I would a bad judgement call on behalf of the original developer(s).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

allspiritseve wrote:
Jenk wrote:Yes, it's a crap implementation, but type-hinting still allows this crap implementation to happen. Not having type-hinting allows FAR more flexibility and reduces maintenance immeasurably.
Your argument that type-hinting "allows this crap implementation" to happen is not a very good one. SQL allows N+1 queries to happen among other things, do you complain about that? Technology can't always stop people from being stupid.

Not type hinting compared to type hinting a concrete, final class might reduce maintenance. But how much do you seriously have to maintain a properly type-hinted interface compared to no type hints? I would argue it probably reduces your maintenance, most of the time. If you need to pass in a class, implement the interface. If it collides with class's existing interface, wrap it in a decorator that implements the interface. In that situation you'd want to decorate it anyways. Just because the methods are the same doesn't mean they are used in the same way.

An interface is a contract between classes, and if you don't type hint, the contract is still there but it's not explicit. You may not understand why this and this method is required in an interface, but if the developer did their job right, they are making the contract between classes explicit and clear so you know exactly what you need to implement.
I hate that term "An interface is a contract" .. not it's not. It's literally a specification of an interface, nothing more, nothing less. I've seen it countless times where someone just stubs methods because the interface they must implement to achieve one thing is too big, i.e. they just simply don't require the other methods/properties on the interface required by a type-hint. That's not a contract, that's just an interface (a messy one, too) which again is a fault of type-hinting (or bad use of type-hinting to be semantic.)

Yes, my woes are with improper use of type-hints, yet if type-hints didn't exist there wouldn't be any of these problems, and the fact these problems exist only highlight the issues that do exist with type-hinting. There would be a few run-time errors (if they slip past tests ;)) and well.. so what if there is? It's no different than seeing a type-hint error.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: How do you feel about type-hinting

Post by allspiritseve »

Jenk wrote:I hate that term "An interface is a contract" .. not it's not. It's literally a specification of an interface, nothing more, nothing less. I've seen it countless times where someone just stubs methods because the interface they must implement to achieve one thing is too big, i.e. they just simply don't require the other methods/properties on the interface required by a type-hint. That's not a contract, that's just an interface (a messy one, too) which again is a fault of type-hinting (or bad use of type-hinting to be semantic.)
Again, that's not a fault of the type hint, it's a fault of the developer who wrote that interface. Type hinting to an interface should be used when a certain amount of methods are required in order to use the injected class. If there's no type hinted interface, the requirements aren't explicit. If there are too many requirements, the interface is too explicit. There can be a middle ground, it just has to be used with care.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

It's semantics.. if the type-hint were not there, it wouldn't constrain anything. That's my argument.

To simply summarise what I am trying to point out:

To refactor a poorly written application that has poorly implemented type-hints is far, far harder than a poorly written application that which has no type-hints at all.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: How do you feel about type-hinting

Post by allspiritseve »

Jenk wrote:To refactor a poorly written application that has poorly implemented type-hints is far, far harder than a poorly written application that which has no type-hints at all.
Agreed. But type hints have their uses.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: How do you feel about type-hinting

Post by Jenk »

Which is where I disagree :)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: How do you feel about type-hinting

Post by alex.barylski »

type-hinting is just a tool...to use it effectively prevents one type of potential error (which I hilighted initially).

It's rare that occassion would occur, as passing in two wildly different object types would cause an error at runtime when you called a method which doesn't exist, the problem becomes immediatelyc lear.

My problem was that registry and and request both implemented a method called getValue() with identical interfaces -- so no error was triggered -- notta -- zip -- so I was stuck with a weird bug which no matter what I did to the registry nothing would work.

In most cases, if you pass an A object to a B parameter and call A's unique methods you would trigger an method does not exist error.

Unit testing in my case would not have helped only an explicit test for the type inside the method or type-hinting if you wanted the check to be implicit. In this case I would favour implicit as it's less lines of codes and clearly makes more sense.
User avatar
jgadrow
Forum Newbie
Posts: 22
Joined: Wed Jun 17, 2009 7:56 pm
Location: Cincinnati, Ohio
Contact:

Re: How do you feel about type-hinting

Post by jgadrow »

I'm definitely FOR type-hinting. I wish they'd have gone a step further and allowed to restrict to the integral types as well. As a security-minded individual, I like being able to write functions that perform some 'auto-validation' for me. Saves me from typing 'instanceof' and 'is_array ()' all over the place. But, I still have to do 'is_int,' 'is_string,' 'is_bool,' etc.

I know this is supposed to be a 'typeless' language. However, by being able to restrict in the function definition, you're essentially creating a contract which improves code security.
Post Reply