Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to neatly format if..else without an else-if
How to neatly format if..else without an else-if [message #810777] Thu, 01 March 2012 14:47 Go to next message
Mark Jeronimus is currently offline Mark JeronimusFriend
Messages: 3
Registered: March 2012
Junior Member
In the formatter I can specify to place 'then' statements on the next line

if (false)
	return;
else
	return;

and I can specify to keep 'else if' on the same line, which is kind of an exception (because the second if-else structure is in fact a single 'then' statement).

if (true)
	return;
else if (false)
	return;
else
	return;

But whenever this second if structure doesn't have an else, things get weird.

I want if statements without an else to be on the same line, which I can specify with the 'simple if' setting

if (true) return;

however, in the case an 'else' statement is a simple if, I would like to have it on the next line as if it is a single big if-elseif structure:

if (true)
	return;
else if (false)
	return;


How can I configure the formatter so it will keep the following example exactly as it is (no change when I execute the formatter)?

if (true) return;

if (true)
	return;
else if (false)
	return;

If it is not possible, I would like to request this feature.

[Updated on: Thu, 01 March 2012 14:50]

Report message to a moderator

Re: How to neatly format if..else without an else-if [message #811229 is a reply to message #810777] Fri, 02 March 2012 04:26 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
In the 'Control Statements' block only turn on 'Keep simple 'if' on one line' and 'Keep 'else if' on one line. Turn off all the options.
Re: How to neatly format if..else without an else-if [message #811261 is a reply to message #811229] Fri, 02 March 2012 05:28 Go to previous messageGo to next message
Mark Jeronimus is currently offline Mark JeronimusFriend
Messages: 3
Registered: March 2012
Junior Member
Doesn't work. It turns the last part into this ugly being:
if (true)
	return;
else if (false) return;
Re: How to neatly format if..else without an else-if [message #811447 is a reply to message #811261] Fri, 02 March 2012 10:58 Go to previous messageGo to next message
Bob Sadarka is currently offline Bob SadarkaFriend
Messages: 40
Registered: February 2012
Member
It's a good practice to put the open and close brackets for if-else controls, even when the body has only one statement.
Re: How to neatly format if..else without an else-if [message #811640 is a reply to message #810777] Fri, 02 March 2012 16:29 Go to previous message
Eclipse UserFriend
In the 'Control Statements' block turn OFF 'Keep simple 'if' on one
line' and turn ON 'Keep 'else if' on one line. Turn off all the other
options.

This would have

if (true)
return;

if (true)
return
else if (false)
return
else
return


Personally I don't like if (true) return. I find it hard to read. So
turning off makes it nice to read in all cases including by itself and
in the chained if-else cases.

Rich
Previous Topic:PatternFilter and CNF
Next Topic:Eclipse - unable to see New Java Project button as described by Cheat Sheet
Goto Forum:
  


Current Time: Thu Apr 25 07:29:33 GMT 2024

Powered by FUDForum. Page generated in 0.03701 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top