简体   繁体   English

Java语法:方法声明中的快捷方式?

[英]Java Syntax: Shortcuts in Method Declarations?

Quickie pseudo-aesthetics question: Quickie伪美学问题:

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 (eg, 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. 编辑:相对于KLE关于需求的问题,没有要求,更多是关于需求。 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 (ie, public class Foo<U extends Bar> ) seems to preclude making the methods static. 每个方法的限制都是相同的,并且确实使每个方法都必须重复执行代码,但是确实使它们很丑陋,但是将它们放在类定义中(即, public class Foo<U extends Bar> )似乎排除了制作方法的麻烦。静态的。

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. 我认为我可以理解在商业应用领域中的这种观点,但是在科学应用领域中似乎有点奇怪-共享类型字段似乎是一种指示事物何时应该是同一种事物的明显而简单的方法。

No, it does not exist, and no, you should not do it for variables either. 不,它不存在,不,您也不应该为变量使用它。 Reason: Maintainability, readability. 原因:可维护性,可读性。

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. 另一个原因是,它鼓励人们在方法/类的顶部声明其所有变量,而不是在帮助GC时声明较晚。

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). 现在a和b的内容不同(它们的泛型类型会有所不同)。 This was java 1.3/1.4 code, so it didn't matter. 这是Java 1.3 / 1.4代码,所以没关系。 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; int i,j的声明; would probably not exist if not for the fact that C/C++ has them. 如果不是因为C / C ++具有它们的事实,则可能不存在。

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. 现在,这并不是最大的问题,尤其是对于现代IDE而言,但最重要的是,Java并不是简洁的语言,它重视表达性和清晰度,而不是简洁的表达。

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. 在某些情况下,这会伤害DRY,但是在这种情况下,这些方法具有相同的返回类型这一事实仅仅是偶然的,并且重新声明它们并不是真正的重复。

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). 无论如何,宏扩展(如果有的话)将是解决此问题的更合适的方法(不是Java很快就会实现)。

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. 在各自的行上定义它们没什么大不了的,在具有完成功能的IDE中这确实非常快,并且它已成为标准。 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... 同样,在同一行上定义它们意味着您无法为其提供Javadoc(或其他注释)...您正在失去可能性...


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 . 因此,该方法可以转到该类中,并成为OO的理想选择一种处理同一类数据的方法
  • We even have patterns about this, like the Command pattern, where a processing is captured in an object. 我们甚至有与此相关的模式,例如Command模式,其中在对象中捕获了处理。 Many good things can be gained using this wisely... 明智地使用此方法可以获得许多好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM