使用“最终”适用于java的修饰符 [英] Using "final" modifier whenever applicable in java

查看:117
本文介绍了使用“最终”适用于java的修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,有一个实例是声明每个变量(local或class),参数final
,如果它们真的是。
尽管这样使得代码更加冗长,
这有助于轻松阅读/掌握代码,并且还可以防止错误
作为意图被清楚标记。

In Java, there is a practice of declaring every variable (local or class), parameter final if they really are. Though this makes the code a lot more verbose, this helps in easy reading/grasping of the code and also prevents mistakes as the intention is clearly marked.

你有什么想法,你在做什么?

What are your thoughts on this and what do you follow?

推荐答案

我认为这一切都做好编码风格。当然,你可以编写好的,健壮的程序,而不用在任何地方使用大量的 final 修饰符,但是当你考虑...

I think it all has to do with good coding style. Of course you can write good, robust programs without using a lot of final modifiers anywhere, but when you think about it...

final 添加到不应该的所有内容中,简单地缩小您(或下一个程序员,在您的代码)将误解或误用导致您的代码的思维过程。至少它应该响起一些钟声,当他们现在想改变你以前不可改变的东西。

Adding final to all things which should not change simply narrows down the possibilities that you (or the next programmer, working on your code) will misinterpret or misuse the thought process which resulted in your code. At least it should ring some bells when they now want to change your previously immutable thing.

起初,看起来很尴尬,看起来很多 final 关键字在你的代码,但很快你会停止注意这个词本身,只会想,这个东西永远不会从这个点 - 在(你可以从我那里拿来); - )

At first, it kind of looks awkward to see a lot of final keywords in your code, but pretty soon you'll stop noticing the word itself and will simply think, that-thing-will-never-change-from-this-point-on (you can take it from me ;-)

我认为这是很好的做法。我一直没有使用它,但是当我可以标记某些东西 final 时,我会这样做。

I think it's good practice. I am not using it all the time, but when I can and it makes sense to label something final I'll do it.

这篇关于使用“最终”适用于java的修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆