Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exit Sub code in Java

 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends..

iam an ex newbie vb6 programmer.. and i become a newbie Java Programmer today..

in vb6 i often use "Exit Sub" Method which would exit from a procedure..
how can i do like that in Java?..

i use that to avoid using Nested if..

Thanks in advance..
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just use "return;". Similarly, "return value;" is the equivalent of
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try not to do that. See our style guide particularly about avoiding return break and continue, for an explanation.

Try to organise your control statements so they are controlled by a boolean flag; when that flag becomes "false" the control statement will terminate and then the method will terminate. And, as Rob says, return any values required as the last line of the method.
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have created like this in my application :




i uses that to avoid nested if..
its could run well..

is there any a better way?, to make my code more readable.. sorry i confused..

Thanks in advance..
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you always returning null? If you return nulls you are risking problems with NullPointerExceptions later on. If you always return null, why don't you give the method a void return type?
Don't use == true or == false ever.
Never
. . . if (b == true) . . .
Only write . . . if (b) . . .

Never
. . . if (b == false) . . .
Only write . . . if (!b) . . .

Never write an empty catch; that will hide any Exceptions, so you can conceal errors and not know they have happened.

You are calling a doSave method which calls doMyTransaction so you seem to have confusing method names.

What you actually want isIf you have a String.equals method, putting a literal first prevents possible null pointer problems.
If you are a beginner in Swing, don't get NetBeans to write your code; write it yourself by hand.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another take:(Although the stack trace is probably more appropriately logged using a real logging mechanism.)
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@David Newton and @Campbell Ritchie

Thanks for your advice..
i just know that the void could has "return;"

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:/* Only one "if" because both previous "if"s were identical to each other. */


I know you're getting old Campbell, so pop your reading glasses on

 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one thing worse than having Rob catch me making that sort of mistake . . .


And that's Joanne catching me out

Sorry.















Probably better to implement those "if" blocks with the || operator.
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try not to do that. See our style guide particularly about avoiding return break and continue, for an explanation.

That's a whole discussion on its own, probably as old as the VI vs Emacs discussion*. I personally return in the middle of a method quite often. Agreed, refactoring is a bit harder, but most of my methods are so good they simply don't need refactoring
(In other words: I'll tackle that problem when it arises)


* VI of course!
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . most of my methods are so good they simply don't need refactoring
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We'd better get this thread back on subject
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that style guide, one of the codes is this (its related to having braces in if blocks with only one statement)



Why is this style given as wrong and the following right



The only difference I can see is the position of the opening curly brace and I don't think the first style would be considered wrong. Maybe it is a mistake or I'm missing something...
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JavaRanch style guide is very restrictive on some points. Whoever wrote it was quite definite that braces should match vertically, which I personally find much easier to read. I think (when I looked on Wikipedia) that is called BSD-Allman indentation but might be mistaken. The Sun style guide uses K&R indentation, so only one of those examples in correct in each style guide.

Anyway, we have had enough digressions on this thread already.
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Whoever wrote it was quite definite that braces should match vertically, which I personally find much easier to read.


Sorry for continuing on this topic but I couldn't stop myself. I also like the BSD-Allman indentation but I adapted myself to K&R indentation because the K&B book used that indentation so I thought that it was the standard. But now I feel relaxed that I'm not the only one who likes BSD-Allman indentation ...
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a thread. Ankit changed the topic to {}, the one subject which causes more controversy than two returns in the same method (also included). Joanne caught me out over a silly mistake, and Rob said his methods are all wonderful.

The whole of JavaRanch in a single thread
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic