Redian新闻
>
JavaBean variable name standard
avatar
JavaBean variable name standard# Java - 爪哇娇娃
m*y
1
Does anyone happen to know the official JavaBean naming standard? Such as,
can I make my variable name like "my_name", instead of "myName"?
Thanks a lot.
avatar
g*g
2
myName is the recommended coding standard by Sun.
But you can use my_name if you really want to, just use
getMy_name and setMy_name for function name

【在 m*****y 的大作中提到】
: Does anyone happen to know the official JavaBean naming standard? Such as,
: can I make my variable name like "my_name", instead of "myName"?
: Thanks a lot.

avatar
b*y
3
没见过用_的
用my_name的,会被team member们背后耻笑或者当面质问

【在 g*****g 的大作中提到】
: myName is the recommended coding standard by Sun.
: But you can use my_name if you really want to, just use
: getMy_name and setMy_name for function name

avatar
g*u
4
my_name是做Database的人的习惯
avatar
g*g
5
Well, I joined this company, and all the instances
names are m_xxx, there are over 10K
source files. So what can you do? You have to do the
same even if it's ugly. Consistent style is more important
than convention.
avatar
m*t
6
Well if all of them start with m_, at least they are consistent. 8-)
m_* originated from MSVC practice, I think.
For a couple of years I had all of my instance variables starting
with _, which actually makes the code much more readable if
you have to read the code in an editor without syntax highlight.

【在 g*****g 的大作中提到】
: Well, I joined this company, and all the instances
: names are m_xxx, there are over 10K
: source files. So what can you do? You have to do the
: same even if it's ugly. Consistent style is more important
: than convention.

avatar
g*g
7
It's maybe slightly more readable, but a headache on refactoring.
Think of a dumb value class, with IDE like eclipse you only need
to put down instance names and generate getter/setters out of it.
Now some manual changes are inevitable. You don't want to live with
function name like getM_blah

【在 m******t 的大作中提到】
: Well if all of them start with m_, at least they are consistent. 8-)
: m_* originated from MSVC practice, I think.
: For a couple of years I had all of my instance variables starting
: with _, which actually makes the code much more readable if
: you have to read the code in an editor without syntax highlight.

avatar
w*g
8
m_
_
是我常用的。但到了公司以后,要看公司的规范
avatar
m*t
9

Oh no no, I was just talking about _starting_ instance variables with "_"
to distinguish them from local variables, not having "_" in the middle.
I totally agree with you. It's pointless and calling for trouble to do that.
And it's not even necessary to have starting _'s with modern IDEs.

【在 g*****g 的大作中提到】
: It's maybe slightly more readable, but a headache on refactoring.
: Think of a dumb value class, with IDE like eclipse you only need
: to put down instance names and generate getter/setters out of it.
: Now some manual changes are inevitable. You don't want to live with
: function name like getM_blah

avatar
h*0
10
this is c++ convention. java people don't use it.

【在 w*****g 的大作中提到】
: m_
: _
: 是我常用的。但到了公司以后,要看公司的规范

avatar
c*t
11
nope.
It is pretty common to have instance variable prefixes. It prevent
one obvious error:
var = var
when it should be
this.var = var
this. is pretty painful to type and not everyone do it.
Particularly when you are quick viewing a source code, having m_ is
very helpful to distinguish instance variables from params.
Eclipse / IDEA all support prefixes.

【在 h*****0 的大作中提到】
: this is c++ convention. java people don't use it.
avatar
g*g
12
when you do var = var, Eclipse gives you a warning.
this.var isn't really that difficult to type with modern IDE.
The problem with prefix is refactoring, it just doesn't follow
java bean convention for example. Many convention over configuration
libraries now count on the right instance name to do the magic via
annotation.

【在 c*****t 的大作中提到】
: nope.
: It is pretty common to have instance variable prefixes. It prevent
: one obvious error:
: var = var
: when it should be
: this.var = var
: this. is pretty painful to type and not everyone do it.
: Particularly when you are quick viewing a source code, having m_ is
: very helpful to distinguish instance variables from params.
: Eclipse / IDEA all support prefixes.

avatar
h*0
13
in 水木社区,java people believe that m_ is unneccesary and reduces
readability. they think when there is a this.var = var, you shold always
type this. explicitely to inhance readability.
and they said that good IDEs, like IDEA will automatically show member
variable and local variable in different colors, there is totally no need to
use m_. and they also believe that the extra m_ makes the automatic
completion feature slower to show up (you need to type m_ then another
character to trigger this)

【在 c*****t 的大作中提到】
: nope.
: It is pretty common to have instance variable prefixes. It prevent
: one obvious error:
: var = var
: when it should be
: this.var = var
: this. is pretty painful to type and not everyone do it.
: Particularly when you are quick viewing a source code, having m_ is
: very helpful to distinguish instance variables from params.
: Eclipse / IDEA all support prefixes.

avatar
h*0
14
i think for member variable known to outside(e.g. with setXX and getXX
method), we should never use any pre/sufix.

【在 g*****g 的大作中提到】
: when you do var = var, Eclipse gives you a warning.
: this.var isn't really that difficult to type with modern IDE.
: The problem with prefix is refactoring, it just doesn't follow
: java bean convention for example. Many convention over configuration
: libraries now count on the right instance name to do the magic via
: annotation.

avatar
c*t
15
Both Eclipse and IDEA can auto generate setXXX and getXXX for variables
with prefixes automatically. I don't think that's a problem at all.
Of course, this rule does not apply to PUBLIC/package variables (which
should only be used in cases of very simple data structures). Using
protected is a bad design. So using m_ is really for private instance
variables.
In my experience, one often times cannot always open a Java file in
an IDE and read. For example, you wanted to quickly checkout the
sou

【在 h*****0 的大作中提到】
: i think for member variable known to outside(e.g. with setXX and getXX
: method), we should never use any pre/sufix.

avatar
h*0
16
it doesn't matter if you use var later...
if it matters, e.g. you changed the value of var after the same function
where you set this.var = var, then this design itself has some flaw... and
you should be very careful in this case, of course. typing this. isn't that
painful, just a habit, and it actually helps checking the code as you
mentioned.

【在 c*****t 的大作中提到】
: Both Eclipse and IDEA can auto generate setXXX and getXXX for variables
: with prefixes automatically. I don't think that's a problem at all.
: Of course, this rule does not apply to PUBLIC/package variables (which
: should only be used in cases of very simple data structures). Using
: protected is a bad design. So using m_ is really for private instance
: variables.
: In my experience, one often times cannot always open a Java file in
: an IDE and read. For example, you wanted to quickly checkout the
: sou

avatar
s*n
17
I find "camel case" uneasy for eyes. it exists only because underscore is
too hard to type. programmers should really have specially designed
keyboards; standard keyboards were designed for secretaries.

【在 m*****y 的大作中提到】
: Does anyone happen to know the official JavaBean naming standard? Such as,
: can I make my variable name like "my_name", instead of "myName"?
: Thanks a lot.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。