• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unreachable Code  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why is this classed as unreachable code (try statement onwards) only after I place my last bracket of the code in to complete the class body?

 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the best possible answer is "because the developers of whatever IDE you're using chose to implement it that way".

I'd say it's a legitimate design choice for an IDE to postpone certain semantic checks (like reachability) until the code is syntactically correct (meaning, all the parentheses are present).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I think the best possible answer is "because the developers of whatever IDE you're using chose to implement it that way".

And, à propos of nothing in particular:

@Alice: can be replaced with:

return file.exists();

which saves a lot of indentation - and braces.

Winston
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah good point Winston, thank you ;)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write
if (...)
   return something;
else
   return somethingDifferent;


If the old Sun style guide still works, you can find out what you should write in section 10.5.2.

Don't close your {} when you finish your method. Write backwards, as shown here. If you use an IDE, chances are it will balance the {} automatically. You write
public void foo(Bar bar)
and push enter. Then the IDE will probably insert a pair of braces placed correctly according to the indentation convention you are using. At least Eclipse does.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
And, à propos of nothing in particular:

@Alice: can be replaced with:

return file.exists();

which saves a lot of indentation - and braces.

Winston

But what if I find

much more easy to read? My first reaction when seeing:

is: but what if file doesn't exist? And it takes me some time to realize
that we are dealing with a boolean that could be false.

A somewhat equal remark to Campbells reply (right before this reply):
Why must we 'don't write (...)'? What is a compelling reason for that?
And personal style is not a compelling reason for me.
Often, you would do something like:

and no one would complain.

Anyway, after this short digression, back to Alice.

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:But what if I find [the other way] much more easy to read? My first reaction when seeing:is: but what if file doesn't exist?

Well, one alternative then is:
return file.exists() == true;
but you might get more grumbles from Campbell.

The other thing that's worth considering is that checks take time. Admittedly not much in this case, but you could run into a situation where they do - or you're running the method a LOT - so if you can avoid them, why not?

But you're probably right. There's no real "right" or "wrong" about this.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:A somewhat equal remark to Campbells reply (right before this reply):
Why must we 'don't write (...)'? What is a compelling reason for that?

"incorrect use of booleans" is one of Campbell's pet peeves - I've actually had a few run-ins with him about it myself.

Personally, I'm not a huge fan of the '!' operator, because it's quite easy to miss - or to read as a letter - when you're scanning; which is why I generally put a space after it when I'm writing code.

And on occasion I'll even write:
if (somethingOrOther == false)
which really gets his dander up.

He does have a point though. It's very easy to miss out one of those '=' which turns the condition into an assignment - which still might be a valid condition, but ends up setting a field that you never intended to be set.

Winston
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd second that there's nothing wrong with following a personal style that makes sense to the developer. If that is different from what some code style guide says, one can take note of that, but then decide to proceed anyway.

It's a different matter if one works in a team where people would be expected to use the same style in order to make it easy for everyone to understand everyone else's code.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write the same as Winston suggested:-

return file.exists();

Apart from the fact that in that instance you are terminating the method prematurely. Agree the bang sign ! is easy to miss. Some languages have the keyword NOT but that would have used two to four more keystrokes and would have upset Kernighan and Ritchie in the days when a megabyte of memory cost about the same as a large London house.
So people advised me to try to use booleans which evaluate to true. It is a lot easier to read
if (needsDoing) ...
than
if (!completedAlready) ...
Just as
if (i > 3) ...
is easier to read than
if (i >= 4) ...

PS's suggestion like… is not equivalent because of the side‑effects. You can of course reduce that to a single return like thisUlf is right. You should have code conventions and stick to them.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes,

you all have valid points, I know, but my intention was to show that
these are conventions, not always my conventions.
And, admittedly, part of this is also some frustration on my part that
this '?:' ternary thing refuses to become standard use of mine, don't
know how come, for the times that I do use it, I like it, especially in the
form:

Anyway, I certainly don't want to pollute this topic about 'Unreachable
Code' any more than I already have. So, back to code beyond
reachability.

greetz,
Piet
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's been insightful to read your opinions - thanks for all the comments!Everyone is really helpful on here
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn'tThe ?: is an operator which separates three expressions not two statements. It should be see asYou often have to put () round it, not because they are part of the expression, but because ?: has such a low precedence. It is possible to nest :? expressions as long as you remember that :? associates to the right. There are ways to indent such multiple expressions so they look good, but I can't remember how to do it.… is equivalent toYou do use that sort of thing in real lifeSomebody show me how to indent that sort of expression please!
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whilst we're still on the matter of unreachable code - can you not put System.out.println(""); in a boolean constructor?
like so:
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Agree the bang sign ! is easy to miss. Some languages have the keyword NOT but that would have used two to four more keystrokes and would have upset Kernighan and Ritchie in the days when a megabyte of memory cost about the same as a large London house.

What about '¬'?

It was good enough for PL/1, and used to be on all IBM keyboards. Think it's still part of the standard US-Ascii one.

And it's much more difficult to miss.

Alice Hampton wrote:It's been insightful to read your opinions - thanks for all the comments! Everyone is really helpful on here

Actually, it's just all blether. Gives us something to do before the second cup of coffee.

But glad you found it useful.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alice Hampton wrote:Whilst we're still on the matter of unreachable code - can you not put System.out.println(""); in a boolean constructor?
. . .
It isn't System.out.println(""); It is System.out.println();

No, you can't because boolean is a keyword so you can't use it as the name of a class, and the primitive type doesn't have a constructor. Boolean would be permissible but you oughtn't to write a Boolean class otherwise you will get no end of confusion when you try to use this class.

Youi can put System.out.println(); in a constructor if you want to, but why would you want it? Except for testing and to see what happens if … which is actually a valid justification for almost any kind of code however daft
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . What about '¬'? . . .
But ¬ is a real operator which is used in real Boolean algebra and real predicate logic. You can't use a real operator. Otherwise they would have had ÷ and × on keyboards too
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:But ¬ is a real operator...

Right. Can't have that.

I'm pretty darn sure it means "NOT" though.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does mean not and even my ChromeBook has a ¬ at the very top left.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably you have this layout, right?



Is UK layout popular outside of UK?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More or less the same. No Windows key and four arrow keys at the bottom right. No characters in blue but, not using Windows, I can get many other characters with AltGr or shift‑AltGr.
The US layout is almost the same; double quote and at sign are different in the US however.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It isn't <very long snip>

Jeez, you don't know when to stop, do you? So, here's a famous quote:
"In der Beschränkung zeigt sich erst der Meister".

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:"In der Beschränkung...

Blimey. Goethe? Too cerebral man...

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says here that the Goethe means
Experts know that less is more
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Piet Souris wrote:"In der Beschränkung...

Blimey. Goethe? Too cerebral man...

Winston


"ich lieb dich nicht du liebst mich nicht (aha)"...that's more my level.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Completely off topic.

Winston, I just realized you had a link to articles you wrote. I started reading them and they are very helpful. Thanks for writing them.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought it was time for 'something completely different' than cows and
ranches and Java and topics and replies, so why not some culture?
'Less is more' is, I think, not 100% correct, but for me, correct enough.

Greetz,
Piet
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Bishop wrote:Completely off topic.

Winston, I just realized you had a link to articles you wrote. I started reading them and they are very helpful. Thanks for writing them.

You're welcome. Glad you're enjoying them.

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It says here that the Goethe means
Experts know that less is more

Yeah, and I believe that Victor Borge also once said that his "inflationary language" was based on German.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!