20 Tips for Better Naming

Names are everywhere in code. As programmers, we name our classes, our variables, our functions, our arguments, our namespaces and more. Given that we do so much of it, we should spend time making sure we do it well. Here are 20 tips to help you improve.

1. Use intention revealing names:

A name should tell you what it does, why it exists, and how it is used. Choosing names that reveal intent makes it much easier to understand and change code.

    
int d; // elapsed time in days

int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
int fileAgeInDays;
    

In the snippet above, we can only tell what the variable d refers to from the comment. A reader would have to search for it's instantiation to get any clue as to it's meaning. In contrast, if we were to use one of the other names for this variable, a reader should be able to intuit what this variable refers to instantly.

2. Don’t be afraid to spend time choosing a name:

You shouldn't feel guilty about taking your time and trying several different names until you find one that you feel is adequately descriptive. Everyone who ends up reading your code in the future (including you) will benefit from this initial outlay of time. In addition, coming up with a descriptive name will often help clarify the design of the module in your mind. Good naming takes time but in the long term, will save more than it takes.

3. Refactoring names:

If you think of a better name later in the development process, don’t hesitate to change it. Modern IDE’s make refactoring names incredibly easy. It is not uncommon to change a name only for a favourable restructuring of the code to become apparent.

4. Avoid noise words in your names:

Noise words like ManagerProcessorData, or Info and are synonyms for “I don’t know what to call this”. If you find yourself wanting to use one of these words it may be that the thing you are trying to name is doing too much.

5. Be wary of hard to name classes/functions:

A hard to name class or function is a possible code smell. If you are finding it difficult to give a class or function an expressive name this could be a sign that:

  • the offending code is doing too much.
  • the offending code is not doing enough.
  • you do not understand the domain well enough and need to get more information before continuing.

6. Class names:

Classes should have noun or noun phrase names like CustomerWikiPageAccount, and AddressParser. When using inheritance superclasses should have short and punchy names. Subclasses should have longer names containing adjectives that describe how this class differs from it’s superclass e.g.SavingsAccount derives from Account.

7. Variable names:

Variables often hold instances of classes so they should be nouns too. They are often derived from the name of the class that they refer to. Boolean variables should be written as predicates e.g. isEmpty orisTerminated so that they read well in if statements.

8. Method names:

Methods should have verb or verb phrase names like postPayment()deletePage() or save(). Accessors and mutators should be prefixed with get and set respectively. Methods that return a boolean should be prepended with ‘is’ such as isPostable() so that they read well in if statements.

9. Scope size to variable name length:

For variables the length of the name should correspond to the size of it’s scope. If the variable is used in only a very short scope then the length of the variable name should be very short. Conversely, if the variable is within scope for a long time, the name of the variable should be more descriptive and so the name should be longer.

10. Scope size to method/class name length:

For methods and classes the length of the name should inversely correspond to the size of it’s scope. For public methods, shorter names are usually better as they will be called many times. Private methods will only be called within the scope of the class and so longer names are preferred that will act as documentation. The exception to this rule are derived class names. The more derived a class, the more adjectives are applied to the base class name and the longer it will be.

11. Pick one word per concept:

Pick one word for one abstract concept and stick to it. For example it’s confusing to have get(),fetch() and retrieve() as equivalent methods of different classes. A consistent lexicon is an important tool for programmers that must work with your code.

12. Avoid using the same term for two disparate concepts:

If you follow the one word per concept rule then you will end up with many classes that have the same method names. This is fine as long as the parameter lists and return values of the various methods are semantically equivalent. Where problems will occur is if you use the same term for two disparate concepts.

For example, we could use an add() method in multiple classes to denote the creation of a new value by adding or concatenating two existing values. If we then introduce an add method to a class in which it is used to add a single parameter into a collection this will cause problems as the semantics are different. A better term for this new method would be insert().

13. Use solution domain names:

The code we write will be viewed by other programmers. Since this is the case it is beneficial to use technical terms where appropriate such as algorithm names, pattern names and maths terms given that these should evoke the correct concepts in the minds of the reader.

14. Use problem domain names:

When there is no technical name for a concept then it is wise to use a name from the problem domain. This will provide the option for future readers of your code to consult a domain expert if they are unsure what something in the code is doing.

15. Add meaningful context:

Most names are not meaningful in and of themselves and would need to be placed within a context (class/function/namespace) for a reader to understand what they refer to. In some cases it may be necessary to prefix names to add some context. For example, if we have a number of variables that are used to represent an address firstNamelastNamestreethouseNumbercitystate and zip. If you just saw the state variable it would be hard to automatically infer what it was referring to, a better solution would be to encapsulate the variables in an Address class.

16. Don’t add gratuitous context:

Short names are generally better than longer ones, so long as it is clear, don’t add context to a name that does not need it. Names should not be prefixed with unnecessary information that can be inferred from the class/package/namespace that the name is within.

17. Avoid encodings:

Given the power of modern day IDE’s, we do not need to encode type or scope information into the names of our variables or classes. This includes not appending I to interfaces, the users of our code do not need to know that their classes are being passed an interface. If you have to use encoding then it is better to encode the implementation than it is to encode the interface.

18. Avoid disinformation:

Do not leave false clues that misinform the reader as to the meaning of the code. If you have a variable named accountList that actually holds an array this may lead to false conclusions. A name should say what it means, and mean what it says.

19. Use pronounceable names:

Programming can be a social activity, using unpronounceable names hampers our ability to discuss the code that we are working on with others.

20. Use searchable names:

Using short generic names hampers our ability to search for things within our codebase. This is important for manoeuvring around the code and in our ability to refactor.

If you have any other tips please leave a comment below.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
水资源是人类社会的宝贵财富,在生活、工农业生产中是不可缺少的。随着世界人口的增长及工农业生产的发展,需水量也在日益增长,水已经变得比以往任何时候都要珍贵。但是,由于人类的生产和生活,导致水体的污染,水质恶化,使有限的水资源更加紧张。长期以来,油类物质(石油类物质和动植物油)一直是水和土壤中的重要污染源。它不仅对人的身体健康带来极大危害,而且使水质恶化,严重破坏水体生态平衡。因此各国都加强了油类物质对水体和土壤的污染的治理。对于水中油含量的检测,我国处于落后阶段,与国际先进水平存在差距,所以难以满足当今技术水平的要求。为了取得具有代表性的正确数据,使分析数据具有与现代测试技术水平相应的准确性和先进性,不断提高分析成果的可比性和应用效果,检测的方法和仪器是非常重要的。只有保证了这两方面才能保证快速和准确地测量出水中油类污染物含量,以达到保护和治理水污染的目的。开展水中油污染检测方法、技术和检测设备的研究,是提高水污染检测的一条重要措施。通过本课题的研究,探索出一套适合我国国情的水质污染现场检测技术和检测设备,具有广泛的应用前景和科学研究价值。 本课题针对我国水体的油污染,探索一套检测油污染的可行方案和方法,利用非分散红外光度法技术,开发研制具有自主知识产权的适合国情的适于野外便携式的测油仪。利用此仪器,可以检测出被测水样中亚甲基、甲基物质和动植物油脂的污染物含量,为我国众多的环境检测站点监测水体的油污染状况提供依据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值