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.
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.
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.
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 .
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.
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/
While I don't know if a single function which does this, you can obtain similar results with a few keystrokes:
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:
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
).
All done, now you have your variables with their respective type, one per line:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With