• Post Reply Bookmark Topic Watch Topic
  • New Topic

I don't know what this simple line of code does  RSS feed

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can someone explain this line of code. Does it mean if the string is empty, do nothing?
Thank you very much.
 
Saloon Keeper
Posts: 3332
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like an attempt to determine if a string is empty but because it is comparing references it will probably fail. To see if a string is empty use:
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, ian!

In addition to what Carey said, the intent of the code (not how it would actually work) was to return from the method enclosing that code if the string was empty. Returning from the method isn't the same as doing nothing.
 
ian bilgaen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can someone explain this line of code. Does it mean if the string is empty, do nothing?
Thank you very much.


#edit 1
This is the whole method. So if the string display is empty, stay in this method?
Thanks.

 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's impossible to tell what that line of code will do. I would consider it to be a bug.

However, for copyright and other reasons could you tell us where you got that code from?
 
Carey Brown
Saloon Keeper
Posts: 3332
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ian bilgaen wrote:

Can someone explain this line of code. Does it mean if the string is empty, do nothing?
Thank you very much.



#edit 1
This is the whole method. So if the string display is empty, stay in this method?
Thanks.


That is a horrible piece of code and probably shouldn't be disected too deeply because you'd likely walk away with the wrong idea about how to write code.

That said, see comments.

It will not "stay in the method". That would require a loop.
 
ian bilgaen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got this piece of code from a project on how to make a calculator. Just trying to understand all of the code. Since this is bad code I'll just leave it
and go on. Thanks everyone for the help.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks to me like it's Android code and not Java code. Did you know that? (You posted in a Java forum and not in our Android forum, but that's totally excusable for somebody new to the Ranch.)
 
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
That said, see comments.


Just a comment on the comments.



This is testing whatever result is.  The earlier one is testing whatever display is.


The first assignment is setting _display (the local variable) to result.
The second is setting display to _display.
Why?
Who knows.
At a guess, clear() clears whatever the value of result is, so result needs to be set stored somewhere (_display)...maybe.

But yes, this is not good code, and surely doesn't actually work?
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where that tutorial comes from, partially to prevent copyright problems, as somebody has already said, and partially so we know to avoid it (except possibly for ridicule ‍). Line 27 is a WTF all by itself.
Another thing about the comments: they are not at all nice to read in ALL UPPER CASE. In Java®, reserve ALL_UPPER_CASE writing (and underscore _ characters) for names of constants.
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those were Carey's comments, pointing out the issues.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Dave Tolls
Ranch Foreman
Posts: 3074
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, though I'm sure I'll be corrected if wrong, the code seems tocome from here:
https://github.com/victorgrego/MyCalculator/blob/master/app/src/main/java/com/vnsilva/mycalculator/Calculator.java

This seems to be relying on the String pool, and "" always being the one in the pool.
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before checking for emptiness using one of String's methods, probably would be a good idea to check for null reference first.

Anyway, code looks pretty much crammed in 10 minutes or so without paying attention to details. Seem to be calculator's program.
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:This seems to be relying on the String pool, and "" always being the one in the pool.

Correct. But, I'd say it is a wrong way to approach this check. It is fortunate user gets true in case of emptiness, but user isn't checking for emptiness actually, so the semantics are screwed, which made around 90% of readers of this thread to think it is a bug.
 
Liutauras Vilda
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also there is method isOperator(), which also does confusing stuff, it makes switch statement to go fall-through. Probably developer thought to save some lines of code for the price of readability.
operate() method throws Exception rather than concrete Arithmetic exception. Also code follows bad formatting style, inconsistent. Also there are bugs in that program, but probably not worth to expand on that.

Agree with Campbell, isn't the best program to copy blindly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!