It is a pure question of style, here.
Java conventions (not enforced by the compiler!) are to name the classes with an initial upper case letter and the methods (functions) with an initial lower case letter.
Coming from C and C++ world (mostly on Windows), I kept the usage of starting my methods with an initial upper case letter (annoying Java purists!
). Likewise, I prefer to align my braces...
Another convention I kept (but I don't apply it strictly in Processing) is to use a m_ prefix for the members of my classes. This way, even with a dumb editor or when posting my code, I can easily distinguish these members from parameters or local variables.
Some people like to add an underscore at the start or end of parameters.
I think all people can choose whatever convention they are at ease with, as long as they are consistent in their use.
So, you can write:
Code:// Classical Sun way
class SomeClass
{
int importantData;
SomeClass(int importantData)
{
this.importantData = importantData;
}
}
// Simple alternative
class SomeClass
{
int importantData;
SomeClass(int id)
{
importantData = id;
}
}
// Not uncommon
class SomeClass
{
int importantData;
SomeClass(int _importantData) // or importantData_
{
importantData = _importantData;
}
}
// My way
class SomeClass
{
int m_importantData;
SomeClass(int importantData)
{
m_importantData = importantData;
}
}
Why "
should i use the same variable in class's fields and contructor's parameter"
Two reasons:
- Lack of imagination!
- Consistency (strong ties between given data and corresponding member)
Of course, you can call it data, aData, theData, someData or whatever. Choosing same name (with or without a decoration) is fast and no brainer... :-D