简体   繁体   English

使用导入 static 字段是一种好的编程习惯吗?

[英]Is it a good programming practice to use import static fields?

I declare some constant variables in my SQLiteOpenHelper class:我在我的SQLiteOpenHelper class 中声明了一些常量变量:

public static final String USERNAME = "user_name";
public static final String PASSWORD = "password";

In an Activity where I create SQL queries, I import these fields:在我创建 SQL 查询的Activity中,我导入这些字段:

import com.mygame.ui.login.LoginInfoSQLiteOpenHelper.*;

Is this a good practice?这是一个好习惯吗?
Or the traditional way of referring to constants better?还是更好地引用常量的传统方式?

LoginInfoSQLiteOpen.USERNAME

If you look at someone's code and see a field like如果您查看某人的代码并看到类似的字段

foo.do(baz, USERNAME); 

wut(,)?武特(,)? where did that var come from ?那个 var 是从哪里来的?
search, grep, where is it declared?搜索一下,grep,在哪里声明的?

Using it as ClassName.FIELD makes things much clearer and cleaner.将其用作ClassName.FIELD会使事情变得更加清晰和清晰。 You avoid confusion, and sometimes it makes more sense to have a proper classname that denotes the field, than a field that came out of nowhere.您可以避免混淆,有时使用一个正确的类名来表示该字段比使用一个凭空出现的字段更有意义。


well, not everyone uses an IDE, and not everyone reads code through an IDE(maybe through a repository on the web), and even some consider VIM an IDE, and I do use vim a lot(although I don't think of it as an IDE). well, not everyone uses an IDE, and not everyone reads code through an IDE(maybe through a repository on the web), and even some consider VIM an IDE, and I do use vim a lot(although I don't think of it作为 IDE)。
So, it's not about what an IDE can or can't do, but more of what code reading is about.所以,这不是关于 IDE 能做什么或不能做什么,而是更多关于代码阅读的内容。 Code reading, code quality, expressing ideas in your programming language of choice, in a way through abstractions that make sense and tie well together.代码阅读、代码质量、用您选择的编程语言表达想法,通过有意义且紧密结合的抽象方式。

A few years late to the party... but I feel it is worth providing the opposite point of view.晚了几年……但我觉得提供相反的观点是值得的。 There is a reason why Java was designed to have static field imports, and the reason is to hide how the class is implemented to users of the class. There is a reason why Java was designed to have static field imports, and the reason is to hide how the class is implemented to users of the class. This is an important design principle for outwardly facing code.这是面向外代码的重要设计原则。 I would agree with c00kiemon5ter take on it, however, there might be situations in which it is worthwhile.我同意 c00kiemon5ter 的做法,但是,在某些情况下它可能是值得的。

More info on static field importing can be found here: https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html More info on static field importing can be found here: https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

I recommend the second method, only importing classes not fields.我推荐第二种方法,只导入类而不是字段。 So prefixing your constants with the owning class, like LoginInfoSQLiteOpen.USERNAME .因此,使用拥有的 class 为常量添加前缀,例如LoginInfoSQLiteOpen.USERNAME It can become highly redundant, but it is much more readable and maintainable in the long run.它可能变得高度冗余,但从长远来看,它更具可读性和可维护性。

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

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