Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert multiple variable declarations to one per line?

In Eclipse, is there an automatic way to convert this:

int myX, myY, myZ;

... to this...

int myX;
int myY;
int myZ;

I thought I could do this from the 'Clean Up' and 'Members Sort Order' preferences, but it does not break up the multiple declarations into one per line. Is there a way to do it?

Edit: To clarify, this is in regards to member variables.

like image 529
martinez314 Avatar asked Mar 12 '14 18:03

martinez314


People also ask

Can we declare multiple variables of same data type one line?

Every declaration should be for a single variable, on its own line, with an explanatory comment about the role of the variable. Declaring multiple variables in a single declaration can cause confusion regarding the types of the variables and their initial values.

How can you assign the same value to multiple variables in one line?

Assign Values to Multiple Variables in One Line When assigning multiple variables in a single line, different variable names are provided to the left of the assignment operator separated by a comma. The same goes for their respective values except they should be to the right of the assignment operator.

How do you assign multiple variables to one line in Java?

Declaring and Assigning Variables int a, b, c; You can also assign multiple variables to one value: a = b = c = 5; This code will set c to 5 and then set b to the value of c and finally a to the value of b .

Can you declare multiple variables on the same line in JavaScript?

Finally, you can also use destructuring assignment to declare and initialize multiple variables in one line as follows: const [name, age, message] = ["Nathan", 28, "Hello there!"]; console. log(name); // "Nathan" console. log(age); // 28 console.


2 Answers

Eclipse does not provide this formatting feature out of the box. You can use Checkstyle plugin to check for this and many more coding standards.

In Checkstyle configuration file, use module MultipleVariableDeclarations like this,

<module name="Checker">
    <module name="TreeWalker">
        <property name="cacheFile" value="target/cachefile"/>
        <property name="tabWidth" value="4"/>
        ...
        <module name="MultipleVariableDeclarations"/>
        ...
    </module>
</module>

If you are OK with command line tool, then astyle and GC GreatCode are two good code formatters.

GC GreatCode is given as C/C++ code formatter but it will format your code in this scenario well.

In GC the option to split variable declaration is -code_split_decl_style

Original code:

String fileName, a, b, c;

With -code_split_decl_style-1 option:

String  fileName,
        a,
        b,
        c;

With -code_split_decl_style-2 option:

String fileName;
String a;
String b;
String c;

You can play around with different command line formatting tools and configurations for various languages in one GUI tool using UniversalIndentGUI.


Checkstyle: http://checkstyle.sourceforge.net/

Astyle: astyle.sourceforge.net/

GC GreateCode: http://sourceforge.net/projects/gcgreatcode/

UniversalIndentGUI: universalindent.sourceforge.net/

like image 134
sadiq.ali Avatar answered Oct 24 '22 01:10

sadiq.ali


While I don't know if a single function which does this, you can obtain similar results with a few keystrokes:

  1. Highlight the line which contains the multiple variable definitions, and use the Find/Replace function (keystroke: Ctrl-F) to replace the comma-space sequence to a semicolon, space and the type of the variable. Make sure the scope is set to "selected lines" as in the following screenshot:

    Find replace dialog

  2. Now you'll still have a line with all the variable declarations in a single line. This can be fixed by highlighting the same line and the Source > Format function (keystroke: Shift-Ctrl-F).

    Source format menu

  3. All done, now you have your variables with their respective type, one per line:

    All done

like image 22
Denis Fuenzalida Avatar answered Oct 24 '22 02:10

Denis Fuenzalida