• Post Reply Bookmark Topic Watch Topic
  • New Topic

If Else Statement Vs. Return  RSS feed

 
Joseppi Juanlopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if this is the right section in the forum for this (sorry this is my first time here). But I'm trying to practice java and I came upon this one problem. Again, I'm just starting out, but for the life of me I can't figure out what's wrong with my solution compared to theirs. I was just wondering if maybe anyone had an explanation as to what was wrong with mine, or why their solution is better.

I uploaded a screenshot of the problem and my solution vs. their solution as an attachment.

Thanks.

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please post the text with the problem; the screenshot is illegible and I shall delete it.
 
Joseppi Juanlopez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that. I will be sure to post text in the future.

In the meantime I have discovered the problem. It was a bracket problem. In case anyone's super curious anyway here it is:

The Problem: Given 2 ints, a and b, return true if one if them is 10 or if their sum is 10.

My Solution




Theirs:





 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted

If you look in the old Sun style guide, you will see that one of those forms is regarded as better than the other. I presume the solution included a + b rather than a+b; the spaces are important too.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joseppi Juanlopez wrote:I'm just starting out, but for the life of me I can't figure out what's wrong with my solution compared to theirs.

Nothing. except that yours involves a lot more typing ... and at my age, saving my hands from CTS is a major consideration.

It also allows for the possibility of more mistakes.

However, examples like this are usually given to show you that the result of that expression IS a boolean - otherwise you couldn't use it in an if() statement - and since your method returns a boolean, why not just return the result of the expression?

HIH

Winston
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not just about more typing though. Your version is redundant, the other one is straight-to-the-point.

It's the difference between saying "If it makes 10, tell me true, otherwise tell me false" vs "Tell me if it makes 10."
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!