编程命名约定
每种编程语言都有关于如何命名变量、函数、类等的既定约定。遵循这些约定可以提高代码可读性。
JavaScript 约定
变量和函数:小驼峰命名 const userName = "John"; function getUserData() { } 类和构造函数:帕斯卡命名 class UserProfile { } const user = new UserProfile(); 常量:大写蛇形命名(用于真正的常量) const MAX_RETRY_COUNT = 3; const API_BASE_URL = "https://api.example.com"; 私有字段(约定):前缀下划线 class MyClass { _privateField = 42; } 文件名:驼峰命名或烤肉串命名 userProfile.js 或 user-profile.js
Python 约定(PEP 8)
变量和函数:蛇形命名 user_name = "John" def get_user_data(): pass 类:帕斯卡命名 class UserProfile: pass 常量:大写蛇形命名 MAX_RETRY_COUNT = 3 API_BASE_URL = "https://api.example.com" 模块(文件):小写,罕见的下划线 userprofile.py 或 user_profile.py 私有:前缀单下划线(约定) _internal_variable = 42
其他语言
Java:驼峰命名(变量)、帕斯卡命名(类)、大写蛇形命名(常量) String userName = "John"; class UserProfile { } final int MAX_SIZE = 100; C#:帕斯卡命名(公共成员)、驼峰命名(私有) public string UserName { get; set; } private int _maxRetryCount; Ruby:蛇形命名(变量)、帕斯卡命名(类)、大写蛇形命名(常量) user_name = "John" class UserProfile; end MAX_SIZE = 100 Go:驼峰命名(私有)、帕斯卡命名(公共) var userName string // 私有 var UserName string // 公共,导出 SQL:蛇形命名,小写 SELECT user_id, email_address FROM users;
试用工具
文本大小写转换器