开发者

Need inspiration for naming classes and methods [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicates:

Anyone else find naming classes and methods one of the most difficult part in programming?

What’s the best approach to naming classes?

Finding appropriate abstraction names can sometime be time-consuming, especially if your mother tongue is not english. I was wondering if there exists somewhere some sort of online glossary/dictionary describing program-related concepts. We all know about Design Patterns from GoF but I'm looking for a much more complete lexicon, includi开发者_如何学Cng relationships between concepts.

Thanks


You know that there are conventions for naming artifacts in a program?

For example, Java has come up with a pretty strong and reliable way of naming classes, methods and attributes.

Wikipedia has this to say about naming convention (not too helpful, but still interesting) http://en.wikipedia.org/wiki/Naming_convention_(programming)

Here is a page from Central Washington University (with Java, but still helpful for other programming languages) : http://www.cwu.edu/~gellenbe/javastyle/naming.html

Other article here : http://drupal.star.bnl.gov/STAR/comp/sofi/soft-n-libs/standards/NamingAdvice

But basically, naming a method usually start with a action, then a noun, etc.

For example:

$obj.addObservers(...);  // action, noun
$obj.setPrice();         // action, noun
$obj.getModelFromDb();   // action, noun, preposition, noun
$obj.setActive(...);     // action, noun
$obj.isActive();         // yes (true) or no (false) statement
$obj.canEdit();          // yes (true) or no (false) statement
$obj.setCanEdit();       // action, attribute
// etc.

Avoid using negative method naming, ex: $obj.cannotConnect(); which will simply confuse everyone. (This is also true when prompting, ex: "You are about to delete this file, do you want to abort?" ... choosing 'no' thinking you are going to delete a file and it was a mistake will do the opposite...)

Back to method naming;

  • your method names should be as short as possible, but avoid using acronyms, unless the method name is too long (this is a judgement call here);
  • methods should be self explanatory and self documented;
  • avoid using prefixes, unless you are working with non-OOP languages (pretty rare these days). Your prefix should be your class, or namespace, etc.
  • each method should have only one function (one purpose) if you method does more than one thing, consider splitting that method into many, and call each of them inside a method (like doActionBatch(), where 'ActionBatch' is the name of the actual action to perform; Ex: doHttpConnect()
  • etc.

A tip I may suggest is to read programs written by the community; they usual adopt best practices in naming conventions and you will get more familiar with "how methods are named"


The main thing is, be consitent with your naming conventions.

I'm a .NET developer, so I found it best to follow the design guidelines used by the .NET Framework.

http://msdn.microsoft.com/en-us/library/ms229042.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜