`
bma
  • 浏览: 56584 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

Naming a Method

阅读更多
How to name a method? This simple question can trigger a heat discussion and contention. As the old Bell Lab saying goes: "library design is language design", the name of method plays a vital role in the API design, and the quality of method naming distinguishes DSL-like design from unreadable ones.

The Java Way

In Java, we were educated to use a verb + noun to name a method. With the knowledge of design patterns, we really have a bunch of verbs to select, createXxx or makeXxx for factory, buildXxx for builder, just to name some. This naming scheme makes it clear that it's a method, or more casually, an action, which "does something".

But is this kind of naming scheme unquestionable?

In one recent Java project, we need to create lots of stub data to mock the response from web services for unit testing. In one unit test, the code is just like this:
java 代码
 
  1. AbstractResponse<AResponseType> response =  
  2.   new AbstractResponse<AResponseType>(createStubAResponseType()); 

And in method createStubAResponseType, code goes like this:
java 代码
 
  1. AResponseType response = new AResponseType();  
  2.   
  3. resposne.setPartOne(createStubPartOne());  
  4. resposne.setPartTwo(createStubPartTwo());  
  5. resposne.setPartThree(createStubPartThree());  
  6.   
  7. return response;  

Pretty straightforward. But the problem with me is that they read NOT so naturally. Why the prefix "create" for every stub data creation method?

Wouldn't
java 代码
 
  1. new AbstractResponse<AResponseType>(stubAResponseType());    
and
java 代码
  1. resposne.setPartOne(stubPartOne());    
  2. resposne.setPartTwo(stubPartTwo());    
  3. resposne.setPartThree(stubPartThree());  
be better?

It's a small step, but I think it makes difference. For the reason, read on.

The Ruby Way

For Ruby newbies, it takes quite a while to get used to the method naming convention used in Ruby (and especially Rails). Initially, it appears confusing to tell what is a field, what is a method, or what on earth it is (this definitely occurred to me)!

E.g., here's a dummy Rails generator excerpted from book Rails Recipe:
ruby 代码
 
  1. class TumblepostGenerator < Rails::Generator::NamedBase  
  2.   def manifest  
  3.     record do |m
  4.       m.class_collisions class_name  
  5.       m.template "app/controllers/controller_template.rb" ,  
  6.                  "app/controllers/#{file_name}_controller.rb"  
  7.       m.template "app/models/model_template.rb" ,  
  8.                  "app/models/#{file_name}.rb"  
  9.       m.directory File.join('app/views' , file_name)  
  10.       m.template "app/views/form_template.rhtml" ,  
  11.                  "app/views/#{file_name}/_form.rhtml"  
  12.       m.template "app/views/view_template.rhtml" ,  
  13.                  "app/views/#{file_name}/_view.rhtml"  
  14.       m.readme "POST_GENERATION_REMINDER"  
  15.     end  
  16.   end  
  17. end  

Notice how methods are named:
  • class_collisions instead of check_class_collisions
  • class_name instead of get_class_name
  • directory instead of make_directory
  • readme instead of set_readme (or set_readme_file)
  • And the most beautiful one: record (a protected method defined in Rails::Generator::Base) instead of create_record or new_record.
If you take a look at class Generator::Base, you will find all methods are named this way:
manifest instead of define_manifest, usage_message instead of show_usag_message, etc.

In Ruby, methods are named after the return value, using a noun.

Compared with the scheme used in Java, this means we don't care about whether it's an action we need to carry out or it's already there, we just "read" it out.

Ruby's method naming convention is more result-oriented and, from Bertrand Meyer's (the author of Object-Oriented Software Construction) point of view, more object-oriented, cause it treats method and field in unified way. And this is one of the reasons why Ruby is so suitable for define internal DSL (the other reasons are closure/iterator, hash, etc.).
分享到:
评论

相关推荐

    a_byte_of_python

    Identifier Naming Data Types Objects Output How It Works Logical and Physical Lines Indentation Summary 5. Operators and Expressions Introduction Operators Operator Precedence Order of ...

    Python Cookbook英文版

    5.4 Calling a Superclass Implementation of a Method 5.5 Implementing Properties 5.6 Implementing Static Methods 5.7 Implementing Class Methods 5.8 Delegating Automatically as an Alternative ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Naming General Naming Rules File Names Type Names Variable Names Constant Names Function Names Namespace Names Enumerator Names Macro Names Exceptions to Naming Rules Comments Comment Style File ...

    Professional C# 3rd Edition

    Naming Conventions 732 Summary 734 Chapter 22: Viewing .NET Data 735 The DataGrid Control 735 Displaying Tabular Data 735 Data Sources 738 DataGrid Class Hierarchy 746 Data Binding 750 Simple Binding ...

    StyleCop(Microsoft Source Analysis for C#)

    (参数)Placement of method parameters within method declarations or method calls (元素排列)Standard ordering of elements within a class (注释格式)Formatting of documentation within element ...

    VCLZip Pro v3.03

    VCLZip supplies a default implementation of this method so for most purposes you won‘t need your own. - method DefaultFileNameForSplitPart - VCLZip calls this internally if you don‘t define your ...

    NET代码复杂度检查工具

    参数位置(Placement of method parameters within method declarations or method calls ) 元素标准排列(Standard ordering of elements within a class ) 注释格式(Formatting of documentation within ...

    VclZip pro v3.10.1

    - property BlockMode - determines whether VCLZip uses PKZip/WinZip standard naming convention or VCLZip classic method. - method DefaultGetNextDisk - VCLZip calls this internally if you don't define...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    2.2.2. Naming a component-scanned bean 2.2.3. Setting a base package for component scanning 2.2.4. Annotating beans to be automatically wired 2.2.5. Verifying automatic configuration 2.3. Wiring beans...

    图片压缩免安装小工具JPEG Resizer

    * Safer file adding method * Improved incremental file naming * Log is scrolled down automatically * Added file counter * Fixed a typo * Ships with JPEG Resizer CMD 0.1 JPEG Resizer 2.04 * Got...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part2

    Q526060 - Memory leaks when destroying a form if the View's BeginUpdate method is called without calling a matching EndUpdate method Q520431 - Naming mechanism for newly created View items is changed ...

    DevExpress VCL 13.1.4(v2013vol1.4) 源码-例子-帮助-part1

    Q526060 - Memory leaks when destroying a form if the View's BeginUpdate method is called without calling a matching EndUpdate method Q520431 - Naming mechanism for newly created View items is changed ...

    The Java EE 6 Tutorial Basic Concepts 4th Edition

    Referencing a Backing Bean Method 154 Chapter 9: Developing with JavaServer Faces Technology 159 Backing Beans 159 Writing Bean Properties 162 Writing Backing Bean Methods 170 Using Bean ...

    Oracle.ManagedDataAccess.7z

    我测试发现:c、Easy Connect Naming Method:PROTOCOL等一些选项好像就没地方配置了 "user id=scott;password=tiger;data source=//sales-server:1521/sales.us.acme.com" 这种连接方式可用 不需要客户端了。和...

    Obfuscator 2.0.8

    IL2CPP builds are much harder to reverse engineer but strings and member information (class, method names etc) are visible in the global-metadata.dat file. Obfuscation will apply to this file adding ...

    vs 2005 10款插件

    The goal of GhostDoc is to automate the tedious parts of writing XML comments by looking at the name of your class or method, as well as any parameters, and making an educated guess as to how the ...

    Java2核心技术卷I+卷2:基础知识(第8版) 代码

    Naming Patterns for Bean Properties and Events 698 Bean Property Types 701 BeanInfo Classes 710 Property Editors 713 Customizers 723 JavaBeans Persistence 732 Chapter 9: Security 755 Class ...

    Effective Java 3rd edition(Effective Java第三版英文原版)附第二版

    Item 2: Consider a builder when faced with many constructor parameters Item 3: Enforce the singleton property with a private constructor or an enum type Item 4: Enforce noninstantiability with a ...

    spring-boot-reference.pdf

    Naming 46.5.2. autoconfigure Module 46.5.3. Starter Module 47. Kotlin support 47.1. Requirements 47.2. Null-safety 47.3. Kotlin API 47.3.1. runApplication 47.3.2. Extensions 47.4. Dependency ...

    google api php client

    The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs ...

Global site tag (gtag.js) - Google Analytics