13

Adding too many fields into a class it becomes a mess like this:

private static final int UNAVAILABLE = -1;
private static final int EXTERNAL_BUFFER_SIZE = 4000 * 4;
private static final int SKIP_INACCURACY_SIZE = 1200;
private Thread thread = null;
private Object dataSource;
private AudioInputStream audioInputStream;
private AudioInputStream encodedAudioInputStream;
private int encodedAudioLength = -1;
private AudioFileFormat audioFileFormat;
private SourceDataLine sourceDataLine;

I want Eclipse to reformat the code and make it better to read so I modified default formatter and have achieved this:

private static final int UNAVAILABLE = -1;
private static final int EXTERNAL_BUFFER_SIZE = 4000 * 4;
private static final int SKIP_INACCURACY_SIZE = 1200;
private Thread thread = null;
private Object dataSource;
private AudioInputStream audioInputStream;
private AudioInputStream encodedAudioInputStream;
private int encodedAudioLength = -1;
private AudioFileFormat audioFileFormat;
private SourceDataLine sourceDataLine;

But it's still messy and I can't find how to modify the editor further to achieve this (you can see the difference in static)

(Found this piece of code here and I think it can be even more improved to create and an empty column for final):

enter image description here

Image of the Editor in Eclipse:

enter image description here

How can I do this?

Tunaki
138k46 gold badges372 silver badges446 bronze badges
asked Sep 30, 2016 at 5:43
11
  • 2
    The first thought was "switch to COBOL", but I can understand. However not even 0.01% of the fellow java programmers will have experienced such a formating and appreciate it. If you want to work with others better live with the scrubby java code. Commented Sep 30, 2016 at 6:56
  • @Joop Eggen It's clear and beautiful for eyes.For the usual code i have to open search window :). Commented Sep 30, 2016 at 7:06
  • 1
    "Modified the Tags cause the code contains also JavaFX Classes." If only the code that you want to format contains JavaFX classes, but not the formatter and you're not trying to achieve some special formatting for JavaFX classes, the javafx tag is misleading here, since it would not matter, if the code contains ObjectProperty<Color> or List<BigInteger>. Commented Sep 30, 2016 at 7:15
  • Possible duplicate of Formatting Field Declarations in Eclipse Commented Sep 30, 2016 at 7:17
  • 2
    It's likely far from trivial. I just fiddled around with this. The option to select a block of variable declarations and format them ("nicely"!) with a dedicated menu command may be reasonable. In any case, there are open issues: What should happen for int final x=3; final int y=3;. Should modifiers be reordered? But for a real formatter, one would have to dive into git.eclipse.org/c/jdt/eclipse.jdt.core.git/tree/… etc. I tried it, but ... not the slightest chance of getting anywhere there. Commented Oct 2, 2016 at 10:08

2 Answers 2

1
My suggestion would be to write your own formatting program that takes a file location as input and writes formatted content back to the same file. It can be made as an eclipse plugin that can run on a particular key stroke.
The upside is that you can customize the way you want.
The downside is that it would be re-inventing the wheel if someone already had done it.
answered Dec 29, 2016 at 13:08
Sign up to request clarification or add additional context in comments.

1 Comment

Yes creating a Plugin would be great but it needs time and good knowledge , except of complexity . This will be the last solution if someone hasn't already done it...
0

When I run into formatting issues like this my first reaction is "is this class doing too much?".

Through repeated application of the Single Responsibility Principle you should be able to trim down the number of fields to a readable number - and such that no special formatting is required.

answered Oct 3, 2016 at 18:37

1 Comment

James thanks for the answer,i readed your link.Sometimes a class which exists to do only one think can have a lot of variables.Here in this question i want a general formatter to make the code more wonderfull and human readable.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.