Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what makes this loose coupling?

 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this code snippet in the Sierra and Bates java certification book. From what I can garner from the book, it is loose because the first class, DoTaxes, has knowledge of a member of the second class, SalesTaxRates (salesRate)--I'm sure that's not right. Or is it because the second class has called the doColorado() method of the DoTaxes class? Which brings me to my other question: Since both classes are using instances, variables and methods from each other, what gets called or processed first? It's like going around in circles.



 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this piece of code is the opposite of loose coupling -> tight coupling...

the two classes are highly dependent from each other (they share instance variables and methods).

In good code it is easy to just change an implementation of something with something else without breaking other code. Now imagine you want to implement a new version of SalesTaxRates. You will have to reimplement DoTaxes as well. And now, think of 5000 classes ;-)
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes, I meant tight coupling and said loose in error. Thank you.

But how about my other question, the one about which process occurs first, "salesRate = new DoTaxes().doColorado();" or "SalesTaxRates str = new SalesTaxRates();" or is this all part of the tight coupling trauma? I mean, if the classes access members from each other, what happens first?

For instance, when class SalesTaxRates creates the salesRate float variable, has the DoTaxes() class been processed yet? Because if it hasn't, then how would the salesRate even be created, since it comes from "new DoTaxes().doColorado()"? And if it has, then how did it create the SalesTaxRates object str without first having the second object? This circuitous thinking is driving me batty!

I think understanding this logic would go a long way towards my Java education.

One last question: Why is there a "new" in front of DoTaxes().doColorado() if salesRates is a float variable?

Thanks for any help at all.

Vonique
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no problem here.

At the time getSalesRate get's called and calls doColorado which then accesses salesRate, this variable will simply be null since it has not been initialized.

Your doColorado method should return a value, by the way.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50225
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see you again, Vonique. A method called something like "doColorado" looks very suspect to me. It should be something like "foo(colorado)".
 
Vonique Leary
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to come back here, Campbell. I always get good answers----so interesting. I finally decided to enrol in a real Java class at the community college, starting in two weeks. Studying on my own is too confusing. It's been great having this site as a resource, though.

I'm sure I will be back, as usual. Thanks for all the help in the past, Campbell. And thanks Sebastian for your explanation. Sometimes I know something but need another explanation to confirm it.

Vonique
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic