Clean Code : Keep code clean :: Class and Method Name
Class and Method Name |
Class Names
- Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, or Info in the name of a class.
- A class name should not be a verb.
Method Names
Methods should have verb or verb phrase names like postPayment, deletePage, or save.
Accessors, mutators, and predicates should be named for their value and prefixed with get, set, and is according to the javabean standard.
String name = customer.getName();
customer.setName("James");
if (paycheck.isPosted()).....
When constructors are overloaded
When constructors are overloaded, use static factory methods with names that describe the arguments.
For example
Complex fulcrumPoint = Complex.FromRealNumber(43.0);
is generally better than
Complex fulcrumPoint = new Complex(43.0);
Generally happens as below
We are authors. And one thing about authors is that they have readers. Indeed, authors are responsible for communicating well with their readers. The vast majority of the playback was scrolling and navigating to other modules!
Bob enters the module.
He scrolls down to the function needing change.
He pauses, considering his options.
Oh, he's scrolling up to the top of the module to check the initialization of a variable.
Now he scrolls back down and begins to type.
Ooops, he's erasing what he typed!
He types it again.
He erases it again!
He types half of something else but then erases that!
He scrolls down to another function that calls the function he's changing to see how it is
called.
He scrolls back up and types the same code he just erased.
He pauses.
He erases that code again!
He pops up another window and looks at a subclass. Is that function overridden?
. . .
You get the drift. Indeed, the ratio of time spent reading vs. writing is well over 10:1.
We are constantly reading old code as part of the effort to write new code.
Final Words
People are also afraid of renaming things for fear that some other developers will object.
Follow some of these rules and see whether you don't improve the readability of your code.
If you are maintaining someone else's code, use refactoring tools to help resolve these problems.
It will pay off in the short term and continue to pay in the long run.
Recent Comments