CompilerTech

导航

Good Routine Names

Describe everything the routine does  描述子程序所作的所有事情

  • describe all the outputs and side effects. If a routine computes report totals and opens an output file, ComputeReportTotals() is not an adequate name for the routine. ComputeReportTotalsAndOpen-OutputFile() is an adequate name but is too long and silly.

Avoid meaningless, vague, or wishy-washy verbs 避免使用无意义的,模糊的,表述不清晰的动词

  • Routine names like HandleCalculation(), PerformServices(), OutputUser(), ProcessInput(), and DealWithOutput() don't tell you what the routines do.
  • Sometimes the only problem with a routine is that its name is wishy-washy; the routine itself might actually be well designed. If HandleOutput()is replaced with FormatAndPrintOutput(), you have a pretty good idea of what the routine does
  • In other cases, the verb is vague because the operations performed by the routine are vague. The routine suffers from a weakness of purpose, and the weak name is a symptom. If that's the case, the best solution is to restructure the routine and any related routines so that they all have stronger purposes and stronger names that accurately describe them.

Don't differentiate routine names solely by number 不要仅通过数字来形成不同的子程序的名字

Make names of routines as long as necessary 根据需要确定子程序的名字

  • Research shows that the optimum average length for a variable name is 9 to 15 characters. Routines tend to be more complicated than variables, and good names for them tend to be longer.

To name a function, use a description of the return value 给函数命名时要对返回值有所描述

  • A function returns a value, and the function should be named for the value it returns. For example, cos(), customerId.Next(), printer.IsReady(), and pen.CurrentColor() are all good function names that indicate precisely what the functions return.
  • In object-oriented languages, you don't need to include the name of the object in the procedure name because the object itself is included in the call. document.Print(), orderInfo.Check(), and monthlyRevenues.Calc().document.PrintDocument() are redundant and can become inaccurate when they're carried through to derived classes. If Check is a class derived from Document, check.Print() seems clearly to be printing a check, whereas check.PrintDocument() sounds like it might be printing a checkbook register or monthly statement, but it doesn't sound like it's printing a check.

Use opposites precisely 准确使用对仗词

Using naming conventions for opposites helps consistency, which helps readability. Opposite-pairs like first/last are commonly understood. Opposite-pairs like FileOpen() and _lclose() are not symmetrical and are confusing. Here are some common opposites:

add/remove

increment/decrement

open/close

begin/end

insert/delete

show/hide

create/destroy

lock/unlock

source/target

first/last

min/max

start/stop

get/put

next/previous

up/down

get/set

old/new

 

posted on 2013-02-21 15:45  compilerTech  阅读(330)  评论(0编辑  收藏  举报