kostja
kostja

Reputation: 61558

Conditionals simplification in eclipse

I have inherited code laden with ternary conditionals. This style is disallowed by our coding standards (which were introduced after the code has been written).

I am looking for a way to avoid rewriting all these statements manually. Is there a setting/plugin/sth. else in eclipse for this job?

Thank you.

PS. Though I am explicitly looking for a eclipse-based solution, I am not entirely averse to a nice ruby script for this ;)

EDIT : The "press Ctrl+1" approach by Joachim Sauer works for the example case, but does not not for a typical eclipse-generated hashCode

int prime = 31;         
int result = 1;
result{Ctrl+1} = prime * result + ((id == null) ? 0 : id.hashCode());

Pressing Ctrl+1 on the marked spot shows the following options only: Rename in file, rename in workspace, Extract local, Copy to criteria Editor, Extract to local variable

If it would work universally, that would be great. Better still would be a solution that does not involve visting each occurrence.

I am using eclipse 3.7 on OpenSUSE 11, in case this is relevant.

Upvotes: 1

Views: 803

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308061

Assuming this sample code:

    boolean someCondition = true;
    int a = someCondition ? 1 : 2;

Place the cursor after the a and press Ctrl-1. Then select "Replace conditional with 'if-else'".

That results in this code:

    int a;
    if (someCondition)
        a = 1;
    else
        a = 2;

Use Refactor > Inline (Ctrl-Shift-I) as applicable to reduce unnecessary variable declarations.

Upvotes: 3

Related Questions