Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question is really based on PHP, but could be valid for other dynamically typed, interpreted languages and specifically the methods of generating code insight and object browsing in development environments.

We use PHPStorm, and find intellisense invaluable, but it is provided by some limited static analysis and parsing of doc comments.

Obviously this does not lend well to obtaining dependencies through a container, as the IDE has no idea of the type returned, so the developer loses out on a plethora of (in the case of our framework anyway) rich documentation provided through the doc comments.

So we start to see stuff like this:

$widget = $dic->YieldInstance('WidgetA', $arg1, $arg2, $arg3, $arg4...));
/**
 * @var $widget WidgetA
 */

So that code insight works.

In effect the comments are tightly bound, but worse they come out of sync when code is modified but not the comments:

$widget = $dic->YieldInstance('WidgetB', $arg1, $arg2, $arg3, $arg4...));
/**
 * @var $widget WidgetA
 */

Obviously the comment could be improved by referencing a Widget interface, but then we might as well use a factory and avoid the requirement for the extra typing hints in the comments, and dic complexity / boiler plating.

Which is more important to the average developer, code insight / intellisense or 'nirvana' decouplement?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

First of all, complete decoupling is not necessarily an ideal condition. There will always be some coupling between classes, methods and properties, otherwise it would be impossible to communicate any meaningful information. Taken to its logical extreme, complete decoupling resolves to passing a pure object (i.e. a pointer/reference) with no shape whatsoever, which must be cast to the correct type before any meaningful information transfer can take place, and the only way that can occur is by transferring information about the type to cast to.

The same tradeoff occurs with statically typed and dynamically typed languages. Consider the dynamic keyword in C#. The dynamic keyword allows late binding of method calls, deferring resolution of those calls from compile time to runtime. Intellisense stops working. Why? Because there's no way to predict what method signatures are going to be available on the dynamic object until runtime. But lack of Intellisense doesn't mean that the dynamic keyword is useless; it just means that you have to accept the absence of Intellisense as a tradeoff for the benefits of using dynamic.

In the C# world, the way one would solve this problem is to inject an object that conforms to a specific interface. This allows enough shape to draw meaningful information from the object, while still allowing different implementations of that interface to have different underlying behavior. The interface still benefits from intellisense, while providing the flexibility to change the behavior by swapping the implementation.

share|improve this answer
    
Excellent explanation. Having read your final paragraph I have a picture forming in my head of typed containers. This is likely a fair trade-off, the coupling is indirect and a good block of intellisense will be available implicitly. –  Gavin Howden Oct 21 '12 at 1:12
add comment

This is exactly what PHP interfaces are for: presumably there is some functionality (methods) that are shared by both WidgetA and WidgetB, otherwise you could not substitute them. So those methods go into an interface and are documented there, and you can declare the $widget variable to have that interface as its type.

Of course this basically means that you're doing the same work as you'd do with a statically typed language like Java, so you might as well use one since you've already found that you're missing the benefits of static typing...

share|improve this answer
    
Correct about the interfaces, that is what is happening in some cases, it all depends on who writes the code and if / how 'they' decorate it with the type comment. So I think I am going to go with a typed container solution (specific container for specific interface) so that intellisense works implicitly as you state from Interface level, but implicitly without the extra 'type' comments. I am no stranger to the benefits of static typing, but I don't think my suggestion to redevelop the system in Java would be well received, by the execs or the PHP team :) –  Gavin Howden Oct 21 '12 at 9:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.