Socket.D v2.5.11

跨语言命名规范

</> markdown

Socket.D 的协议实现,涉及了很多语言与平台。比如 javascript ,不能有同名的属性和方法,不能有同名的方法;像 python,习惯上用 _ 作命名间隔。

所以定义和申明个规范,为跨语言实现尽量风格统一提供参考。也可以让用户了解情况。

一、基本规则

如果可以,直接使用 java 的命名(它是首发语言,方便文档复用)。否则:

  • 保持各语言平台的使用风格
    • 大驼峰命名法
    • 小驼峰命名法
    • 下划线命名法
  • 取名上,基于 java 的进行变形(java 是首发语言)。如:
    • getXxxYyy()
    • GetXxxYyy()
    • get_xxx_yyy()
  • 名字本身不要变。如:
    • 不要 xxxYyy() 变成 getXxxYyy()
  • 如果 java 本身定义或设计不适合迁移。则:
    • 修改 java 的定义或设计

二、关键规范定义

1、方法名规范(设置、增量设置、添加、获取方法)

  • 普通设置:setXxx():void
  • 链式设置:xxxSet():self
  • 普通放置:putXxx():void
  • 链式放置:xxxPut():self
  • 普通添加:addXxx():void
  • 链式添加:xxxAdd():self
  • 普通获取:getXxx()
  • 分组获取:xxxGet()
  • 简化获取:xxx() //目前主要用在"实体"、"消息"、"帧"的接口上

2、指导原则(这个很重要,对命名有较大的影响)

  • 外部用的,优先简洁!!!
  • 相同意义,风格优先相同

三、规范定义缘由(及调整案例)

1、如果主接口是获取

原 java 里可以:

public interface Demo{
    String tag();
}
public class DemoDefault implements Demo{
    String tag;

    public Demo tag(String tag) { //这种风格,在 Builder 模式里算常见
        this.tag = tag;
        return this;
    }

    public String tag() { //它是主接口(优先简洁)
        return this.tag;
    }
}

在 js 里就没办法用了,需要改成:

//js
class DemoDefault {
    _tag: string; //字段名不能与函数名同

    tagSet(tag: stirng): Demo { //与主接口冲突,改成 tagSet 链式设置风格
        this._tag = tag;
        return this;
    }

    tag(): string { //它是主接口(优先简洁)
        return this._tag;
    }
}
//java
public class DemoDefault implements Demo{
    String tag;

    public Demo tagSet(String tag) { //与主接口冲突,改成 tagSet 链式设置风格
        this.tag = tag;
        return this;
    }

    public String tag() { //它是主接口(优先简洁)
        return this.tag;
    }
}

2、如果主接口是配置(或设置)

原 java 里可以:

public interface Demo{
    Demo tag(String tag);
}
public class DemoDefault implements Demo{
    String tag;

    public Demo tag(String tag) { //它是主接口(优先简洁)
        this.tag = tag;
        return this;
    }

    public String tag() { //这种风格,在 Builder 模式里算常见
        return this.tag;
    }
}

在 js 里就没办法用了,需要改成:

//js
class DemoDefault implements Demo {
    _tag: string; //字段名不能与函数名同

    tag(tag: stirng): Demo {  //它是主接口(优先简洁)
        this._tag = tag;
        return this;
    }

    getTag(): string { //与主接口冲突,改成 getTag 普通获取风格
        return this._tag;
    }
}
//java
public class DemoDefault implements Demo{
    String tag;

    public Demo tag(String tag) { //它是主接口(优先简洁)
        this.tag = tag;
        return this;
    }
    
    public String getTag() { //与主接口冲突,改成 getTag 普通获取风格
        return this.tag;
    }
}