tags:

views:

130

answers:

4

Quickie pseudo-aesthetics question:

For field declarations, the following is allowed:

int i, j, k;

providing three variables for integers. Does anything similar exist for method declarations, ala:

public int getI() {/* ... */},
getJ() {/* ... */},
getK() {/* ... */};

so that the method accessibility, return type, etc don't have to be redundantly specified? I can imagine some cases where this sort of syntax would seem really beneficial (e.g., methods with an assortment of argument sets allowed), so I'm hoping it's in there somewhere. I've tried the above, and it doesn't seem to work.

EDIT: relative to KLE's question about needs, there isn't a requirement, it's more about wants. The most annoying thing I'm facing is that I have a class of a bunch algorithms that are static (and should be static, as they don't depend on anything but their arguments) but take a generic argument with some restrictions. The restrictions are the same for every method, and it really uglies up the code to have to repeat them for every method, but putting them in the class definition (i.e., public class Foo<U extends Bar>) seems to preclude making the methods static.

ALSO: Can someone elaborate on why using sharing the fields declaration is considered bad practice? I think I can appreciate that perspective in the commercial application realm, but it seems a little strange in the scientific application realm - sharing types fields seems an obvious and simple means of indicating when things should be the same kind of thing.

+9  A: 

No, it does not exist, and no, you should not do it for variables either. Reason: Maintainability, readability.

gimpf
Can you elaborate on "maintainability, readability"? For example, if I were implementing a 3D vector, I'd find private double x,y,z; more readable than private double x; private double y; private double z;
Carl
+1  A: 

It is seen as a bad practice for fields, you won't find it for methods.


Could you dig deeper into your need, maybe we can propose a nice alternative? :-)

UPDATE


Why is it a bad practice to define several variables on the same line ?
Readability is really poorer.

Defining them each on its own line is no big deal, it's really fast in an IDE with completion, and it becomes a standard. With a standard in place, everyone will start gaining a lot of time.

Also, defining them on the same line makes it harder to change some of them in one second ! Like commenting one...

Also, defining them on the same line means you cannot give a javadoc (or other comment) for them... You're losing possibilities...


About the algorithms being static because they depend on nothing but their arguments :

  • It is not more costly to have them dynamic. You can have a single instance of each if you want to avoid the overhead of object creation (new) and garbage collection.
  • Over time and refactoring, they might become linked to one of their arguments. So the method could move to that class, and become the ideal of OO : a method that work on data of the same class.
  • We even have patterns about this, like the Command pattern, where a processing is captured in an object. Many good things can be gained using this wisely...
KLE
added some more detail, including an example annoyance
Carl
good points, though I'm not sure I agree on your "ideal OO statement" - certainly, abstraction of algorithms to work on types of classes instead of repeating the algorithm within each class is also a principle of OO.
Carl
+2  A: 

The sharing of declarations is considered bad practice as it's easy to miss a variable in a list; with one-declaration per line the eye scans down, without needing to stop and examine some lines more than others. The other reason is that it encourages people to declare all their variable at the top of the method / class, rather than declaring them late to assist the GC.

Jim Downing
I totally agree with precluding the one-line-of-death perspective, but that can be accomplished with the comma-delimited declaration as well (and of course, people can put ; separated declarations on a single line as well). Spreading around field declarations is also good, but I'm not convinced it encourages bad habits (since I declare late, but also stick fields together).
Carl
Can't argue with that - I'd say it is a balance between your own feelings about aesthetics and the "greater good" in complying with idiomatic best practice. Personally, I care more about working well with SCM than how the file looks, so I go for whatever makes code more pliable to diff (one declaration per line in this case).
Jim Downing
+2  A: 

It is considered bad form for fields, especially once generics are involved. Consider this (real world case) of pre generic code:

private Map a = new HashMap(), b = new HashMap();

Now a and b had different contents (their generic types would be different). This was java 1.3/1.4 code, so it didn't matter. But now, when I have to add generics, that has to be split apart, and it becomes a more involved change. The declaration of int i, j; would probably not exist if not for the fact that C/C++ has them.

So for your method declarations, sure it saves some boilerplate, but at the sacrifice of readability and maintainability. Readability in the sense that the declaration could be tens or hundreds of lines away from the rest of the method. Maintainability in terms of what if one of those methods in the middle of that chain had to change their generic type? Now you have a whole bunch of code that has to be moved around, and you have to get those commas right.

Now this isn't the biggest deal, especially with a modern IDE, but the bottom line is really is that Java is just not terse language, and it values expressiveness and clarity over concise expression.

In some cases, that hurts DRY, but in this case the fact that these methods have the same return type is simply coincidental and it isn't a true repetition to re-declare them.

Anyway, a macro expansion, if anything, would be a more appropriate way to solve this problem (not that Java is getting that any time soon).

Yishai
In the example you give, the Maps might have the same structure, but are actually different things, so while sticking them together was allowed (and still is, I suppose, if one likes casts), it didn't really make sense.Personally, I wouldn't stick those two together, because of that difference. But, for example, I'd probably stick together sets representing regions in a Venn diagram - that seems to improve readability to me.Properly used, it seems like the distinction can be useful.
Carl
@Carl, I certainly agree that it is conceivable that there are cases where it improves readability. But in general it doesn't, which is why it is considered bad form. If it works and helps your code's readability, go for it. But in the general case, if it was used, it would just be as in the example above, to save some cheap repetition and just made things harder to read and maintain.
Yishai
righto, thanks. best answer overall.
Carl