naming-conventions

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

naming-conventions

命名规范

MCP tool: When available in your environment, also query the Sui documentation MCP server (
https://sui.mcp.kapa.ai
) for up-to-date answers. Use it for verification and for details not covered by these reference files.
MCP工具: 如果你的环境中可用,也可以查询Sui文档MCP服务器(
https://sui.mcp.kapa.ai
)获取最新答案。用它来验证以及获取这些参考文件未涵盖的细节。

Overview

概述

Move on Sui has specific naming conventions that differ from what AI agents typically generate from training data. This skill covers every naming pattern from the official code quality checklist.
Sui上的Move语言有特定的命名规范,与AI代理通常从训练数据生成的规范不同。本技能涵盖了官方代码质量检查清单中的所有命名模式。

Error Constants: EPascalCase with
#[error]

错误常量:带
#[error]
的EPascalCase命名法

Error constants MUST use PascalCase with an
E
prefix. Do NOT use SCREAMING_SNAKE_CASE.
Use the
#[error]
attribute to attach human-readable messages to error constants. When an abort occurs, the message is included in the error output, making debugging much easier for users and support.
move
// WRONG
const NOT_AUTHORIZED: u64 = 0;
const INSUFFICIENT_BALANCE: u64 = 1;

// CORRECT — with #[error] for readable abort messages
#[error]
const ENotAuthorized: vector<u8> = b"Caller is not authorized to perform this action";
#[error]
const EInsufficientBalance: vector<u8> = b"Insufficient balance for this operation";

// Also valid — u64 without #[error] (less informative on abort)
const ENotAuthorized: u64 = 0;
const EInsufficientBalance: u64 = 1;
When using
#[error]
, the constant type is
vector<u8>
(a byte string message) instead of
u64
. The compiler assigns numeric codes automatically. Prefer
#[error]
for all new code — it produces clearer error output in explorers, wallets, and logs.
错误常量必须使用带E前缀的PascalCase命名法,禁止使用SCREAMING_SNAKE_CASE(全大写下划线分隔)。
使用
#[error]
属性为错误常量附加人类可读的消息。当发生abort(终止)时,该消息会包含在错误输出中,极大地方便用户和支持人员调试。
move
// WRONG
const NOT_AUTHORIZED: u64 = 0;
const INSUFFICIENT_BALANCE: u64 = 1;

// CORRECT — with #[error] for readable abort messages
#[error]
const ENotAuthorized: vector<u8> = b"Caller is not authorized to perform this action";
#[error]
const EInsufficientBalance: vector<u8> = b"Insufficient balance for this operation";

// Also valid — u64 without #[error] (less informative on abort)
const ENotAuthorized: u64 = 0;
const EInsufficientBalance: u64 = 1;
使用
#[error]
时,常量类型为
vector<u8>
(字节字符串消息)而非
u64
。编译器会自动分配数字编码。所有新代码优先使用
#[error]
——它能在浏览器、钱包和日志中生成更清晰的错误输出。

Regular Constants: ALL_CAPS

普通常量:全大写命名法

Non-error constants use uppercase snake_case. This is the opposite of error constants.
move
// WRONG
const MyConstant: vector<u8> = b"hello";
const feeNumerator: u64 = 3;

// CORRECT
const MY_CONSTANT: vector<u8> = b"hello";
const FEE_NUMERATOR: u64 = 3;
非错误常量使用大写蛇形命名法(ALL_CAPS),这与错误常量的命名规则相反。
move
// WRONG
const MyConstant: vector<u8> = b"hello";
const feeNumerator: u64 = 3;

// CORRECT
const MY_CONSTANT: vector<u8> = b"hello";
const FEE_NUMERATOR: u64 = 3;

Capability Structs:
Cap
Suffix

能力结构体:以
Cap
结尾

Any struct that represents a capability (authorization to perform actions) MUST be suffixed with
Cap
.
move
// WRONG
public struct Admin has key, store { id: UID }
public struct MintAuthority has key, store { id: UID }

// CORRECT
public struct AdminCap has key, store { id: UID }
public struct MintCap has key, store { id: UID }
任何代表能力(执行操作的权限)的结构体必须以
Cap
作为后缀。
move
// WRONG
public struct Admin has key, store { id: UID }
public struct MintAuthority has key, store { id: UID }

// CORRECT
public struct AdminCap has key, store { id: UID }
public struct MintCap has key, store { id: UID }

Events: Past Tense

事件:过去式命名

Event struct names MUST use past tense to indicate something that already happened.
move
// WRONG
public struct RegisterUser has copy, drop { user: address }
public struct CreatePool has copy, drop { pool_id: ID }
public struct LevelUp has copy, drop { new_level: u64 }

// CORRECT
public struct UserRegistered has copy, drop { user: address }
public struct PoolCreated has copy, drop { pool_id: ID }
public struct LeveledUp has copy, drop { new_level: u64 }
事件结构体名称必须使用过去式,以表示已经发生的事情。
move
// WRONG
public struct RegisterUser has copy, drop { user: address }
public struct CreatePool has copy, drop { pool_id: ID }
public struct LevelUp has copy, drop { new_level: u64 }

// CORRECT
public struct UserRegistered has copy, drop { user: address }
public struct PoolCreated has copy, drop { pool_id: ID }
public struct LeveledUp has copy, drop { new_level: u64 }

Getter Functions: Field Name, Not
get_

Getter函数:使用字段名称,不带
get_
前缀

Getter functions should be named after the field they return, without a
get_
prefix. Mutable getters add
_mut
suffix.
move
// WRONG
public fun get_name(u: &User): String { u.name }
public fun get_balance(u: &User): u64 { u.balance }

// CORRECT
public fun name(u: &User): String { u.name }
public fun balance(u: &User): u64 { u.balance }
public fun details_mut(u: &mut User): &mut Details { &mut u.details }
Getter函数应以其返回的字段命名,不带
get_
前缀。可变Getter函数添加
_mut
后缀。
move
// WRONG
public fun get_name(u: &User): String { u.name }
public fun get_balance(u: &User): u64 { u.balance }

// CORRECT
public fun name(u: &User): String { u.name }
public fun balance(u: &User): u64 { u.balance }
public fun details_mut(u: &mut User): &mut Details { &mut u.details }

Hot Potato Structs: No
Potato
in Name

烫手山芋结构体:名称中不含
Potato

Hot potato types (structs with no abilities) should NOT include "Potato" in the name. The absence of abilities already signals the pattern.
move
// WRONG
public struct FlashLoanPotato {}
public struct PromisePotato {}

// CORRECT
public struct FlashLoanReceipt {}
public struct Promise {}
烫手山芋类型(无能力的结构体)的名称中不应包含“Potato”。缺少能力这一点本身就表明了该模式。
move
// WRONG
public struct FlashLoanPotato {}
public struct PromisePotato {}

// CORRECT
public struct FlashLoanReceipt {}
public struct Promise {}

Dynamic Field Keys: Positional Struct with
Key
Suffix

动态字段键:带
Key
后缀的位置结构体

Dynamic field key types should use positional struct syntax (empty parentheses) with a
Key
suffix.
move
// WRONG
public struct DynamicField has copy, drop, store {}
public struct ItemSlot has copy, drop, store { name: String }

// CORRECT
public struct DynamicFieldKey() has copy, drop, store;
public struct ItemKey(String) has copy, drop, store;
动态字段键类型应使用位置结构体语法(空括号)并以
Key
作为后缀。
move
// WRONG
public struct DynamicField has copy, drop, store {}
public struct ItemSlot has copy, drop, store { name: String }

// CORRECT
public struct DynamicFieldKey() has copy, drop, store;
public struct ItemKey(String) has copy, drop, store;

Quick Reference

快速参考

ElementConventionExample
Error constants
E
+ PascalCase
ENotAuthorized
Regular constantsALL_CAPS
FEE_NUMERATOR
CapabilitiesSuffix with
Cap
AdminCap
EventsPast tense
PoolCreated
GettersField name, no
get_
balance()
Mutable gettersField name +
_mut
balance_mut()
Hot potatoesDescriptive, no
Potato
FlashLoanReceipt
Dynamic field keysPositional +
Key
suffix
ItemKey()
元素命名规范示例
错误常量
E
+ PascalCase
ENotAuthorized
普通常量全大写(ALL_CAPS)
FEE_NUMERATOR
能力结构体
Cap
结尾
AdminCap
事件过去式
PoolCreated
Getter函数字段名称,不带
get_
前缀
balance()
可变Getter函数字段名称 +
_mut
后缀
balance_mut()
烫手山芋结构体描述性名称,不含
Potato
FlashLoanReceipt
动态字段键位置结构体 +
Key
后缀
ItemKey()