Using underscores in Java variables and method nam

2019-01-06 11:33发布

Even nowadays I often see underscores in Java variables and methods, an example are member variables (like "m_count" or "_count"). As far as I remember, to use underscores in these cases is called bad style by Sun.

The only place they should be used is in constants (like in "public final static int IS_OKAY = 1;"), because constants should be all upper case and not camel case. Here, the underscore should make the code more readable.

Do you think using underscores in Java is bad style? If so (or not), why?

15条回答
狗以群分
2楼-- · 2019-01-06 12:09

it's just your own style,nothing a bad style code,and nothing a good style code,just difference our code with the others.

查看更多
Melony?
3楼-- · 2019-01-06 12:12

"Bad style" is very subjective. If a certain conventions works for you and your team, I think that will qualify a bad/good style.

To answer your question: I use a leading underscore to mark private variables. I find it clear and I can scan through code fast and find out what's going on.

(I almost never use "this" though, except to prevent a name clash.)

查看更多
Root(大扎)
4楼-- · 2019-01-06 12:12

There is a reason why using underscores was considered being bad style in the old days. When a runtime compiler was something unaffordable and monitors came with astonishing 320x240 pixel resolution it was often not easy to differentiate between _name and __name.

查看更多
登录 后发表回答