• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Which approach is better & why?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Approach 1:




Approach 2:



Are there any real advantages of choosing one over the other?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
performOperation(1,2,'@');

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

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shravan Payasam wrote:I can change the method this way.


And you feel that is acceptable?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic