• Post Reply Bookmark Topic Watch Topic
  • New Topic

Which approach is better & why?  RSS feed

 
Shravan Payasam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Approach 1:




Approach 2:



Are there any real advantages of choosing one over the other?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
performOperation(1,2,'@');

Does that help you decide?
 
Shravan Payasam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can change the method this way. Other than poorly documented method signature, is there any other difference?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shravan Payasam wrote:I can change the method this way.


I think the point was that if you had taken approach #2, this would not even be an issue.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since when is 5 / 0 equal to 0? Just return Double.NaN for doubles and let the ArithmeticException be thrown for ints.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shravan Payasam wrote:I can change the method this way.

And you feel that is acceptable?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shravan Payasam wrote:I can change the method this way. Other than poorly documented method signature, is there any other difference?

Yes. One's procedural and the other is (at least partially) Object-oriented.

Consider a new "power of" ('^') function. Your second class's method could be easily written in terms of its others (reuse), but you can't get around that extra case in your first.

Basically, both are flawed, but the first one is doubly so, because it is a classic case of "dispatch code". And dispatch code is precisely what object-orientation was designed to eliminate.

Winston
 
Shravan Payasam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Since when is 5 / 0 equal to 0? Just return Double.NaN for doubles and let the ArithmeticException be thrown for ints.


Point taken.

Although that is not the subject of the question.
 
Shravan Payasam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Shravan Payasam wrote:I can change the method this way.

And you feel that is acceptable?


By changing the method that way, what I meant is, I know that it will be break with that kind of input.
But I wanted to get more differences like the one that Winston cited.

@Winston: I guess, I'm convinced with your answer. But what did you mean by
Your second class's method could be easily written in terms of its others.

Would you mind explaining with an example?

Also, you said, both are flawed, I'm curious to know what is wrong with the Approach 2? What am I missing?

Thanks in advance
Shravan
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shravan Payasam wrote:By changing the method that way, what I meant is, I know that it will be break with that kind of input.

Which is a problem. A big one.

But I wanted to get more differences like the one that Winston cited.

There are many problems with the first approach -- I'm pointing out just one of them.

The 1st rule of user interface, which also happens to be the first rule of APIs: don't let them do it wrong in the first place!

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shravan Payasam wrote:@Winston: I guess, I'm convinced with your answer. But what did you mean by
Your second class's method could be easily written in terms of its others.

Would you mind explaining with an example?

OK, but it's probably not the best:(I leave you to work out what to do with negative powers and 0⁰ )

TBH, it doesn't make much sense with primitives; but do you see that the code would be the same for almost any generic "integer" class? - indeed, arg1 could be any kind of real number.

Also, you said, both are flawed, I'm curious to know what is wrong with the Approach 2? What am I missing?

Have a look at the BigInteger class, because it has exactly the same methods as your 2nd example. The only difference is that they're built into the behaviour of the class, rather than being "utilities".

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!