PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > A Question Of Coupling
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
A Question Of Coupling

Réponse
 
LinkBack Outils de la discussion
Vieux 02/04/2008, 19h46   #1
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut A Question Of Coupling

I have a choice to make about a class interface. This is a very very
simplified "foo" demo of that choice.

#1

class Foo
def initialize(bar)
@bar = bar
end

def okay?
bar.okay?
end
end

bar = Bar.new
Foo.new(bar)

Or #2

class Foo
def initialize( opts )
@okay = opts[kay]
end

def okay?
@okay
end
end

bar = Bar.new
Foo.new(kay => bar.okay?)

Now, my inner OOP Angel is telling me to take door #2 in order to
avoid strong coupling between Bar and Foo. But, my Red Ruby Devil is
saying "Duck typing, you fool. It doesn't matter anymore!"

Is he right? Has Ruby completely changed the game? If so, why aren't
more Rubyists using OStruct-esque option passing?

T.

  Réponse avec citation
Vieux 02/04/2008, 19h55   #2
MenTaLguY
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A Question Of Coupling

On Thu, 3 Apr 2008 03:46:49 +0900, Trans <transfire@gmail.com> wrote:
> Now, my inner OOP Angel is telling me to take door #2 in order to
> avoid strong coupling between Bar and Foo. But, my Red Ruby Devil is
> saying "Duck typing, you fool. It doesn't matter anymore!"
>
> Is he right? Has Ruby completely changed the game?


I don't see why that would be true in this case. In #1, Foo's
implementation depends on Bar's public interface, whereas in #2,
there is no coupling at all between them.

Put another way: duck typing is still, in a sense, typing. In #1,
you aren't constrained to pass in a Bar, specifically, but you must
still pass in an object which satisfies the same un-named object
protocol as instances of Bar do.

(Perhaps we should call it "ninja typing"?)

> If so, why aren't more Rubyists using OStruct-esque option passing?


Ruby doesn't make it as frictionless as it could be, particularly if
you want default values for your options.

-mental


  Réponse avec citation
Vieux 02/04/2008, 19h59   #3
MenTaLguY
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A Question Of Coupling

That said, I'm not sure that the issue of coupling necessarily settles
the question. The more actual behavior (rather than POD) embodied by Bar,
the more #1 could potentially be desirable as an embodiment of the
Strategy Pattern.

-mental


  Réponse avec citation
Vieux 02/04/2008, 20h17   #4
Jason Roelofs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A Question Of Coupling

My issue with #2 is that the return value of bar.okay? might change,
but with this implementation Foo#okay? won't ever change.

Jason

  Réponse avec citation
Vieux 02/04/2008, 23h28   #5
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: A Question Of Coupling



On Apr 2, 3:17 pm, "Jason Roelofs" <jameskil...@gmail.com> wrote:
> My issue with #2 is that the return value of bar.okay? might change,
> but with this implementation Foo#okay? won't ever change.


That's a good point. I guess that crystalizes the coupling.

Coupled:

class Foo
def initialize(bar)
@bar = bar
end

Decoupled:

class Foo
def initialize(bar)
@bar = bar.dup
end

T.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h28.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10579 seconds with 13 queries