Global Naming Conventions
General Principles
#naming #conventions #readability #clarity
Clear, descriptive names are more important than brevity. Code is read far more often than written.
Variables and Functions
Good Examples:
// Rust
let user_session_timeout = from_secs;
async user_session_timeout = 3600
async def fetch_user_by_email // TypeScript
const userSessionTimeout = 3600;
async function fetchUserByEmail: Bad Examples:
let ust = from_secs; // Too abbreviated
async Rules:
- Use full words, not abbreviations (except widely known:
id,url,html) - Variables: noun or noun phrase
- Functions: verb phrase indicating action
- Booleans: predicate form (
is_valid,has_permission,can_edit)
Language-Specific Conventions
Rust
#rust #naming #snake-case #kebab-case
- Variables/Functions:
snake_case - Types/Traits/Enums:
PascalCase - Constants:
SCREAMING_SNAKE_CASE - Crate names:
kebab-case - Lifetimes: short lowercase (
'a,'static)
// Correct
const MAX_CONNECTIONS: usize = 100;
// Incorrect
const max_connections: usize = 100; // Should be SCREAMING_SNAKE_CASE
// Should be PascalCase
// Should be snake_casePython
#python #naming #pep8
- Variables/Functions:
snake_case - Classes:
PascalCase - Constants:
SCREAMING_SNAKE_CASE - Private: prefix with
_ - Magic methods:
__double_underscore__
# Correct
= 3
pass
pass
# Incorrect
# Should be PascalCase
# Should be snake_case, snake_case paramTypeScript
#typescript #naming #camelcase
- Variables/Functions:
camelCase - Classes/Interfaces/Types:
PascalCase - Constants:
SCREAMING_SNAKE_CASEorcamelCase - Private: prefix with
#or_ - Type parameters: Single uppercase letter or
PascalCase
// Correct
const MAX_RETRIES = 3;
interface UserSession { }
class UserManager { }
type ResponseData<T> = { }
function fetchUserData(userId: number): Promise<User> { }
// Incorrect
function FetchUserData(user_id: number) { } // Should be camelCase
interface userSession { } // Should be PascalCaseBoolean Naming
#boolean #naming #predicates
Boolean variables and functions should read like questions or assertions.
Good Examples:
// Rust
let is_authenticated: bool;
let has_permission: bool;
let can_edit: bool;
let should_retry: bool;
is_authenticated: bool
has_permission: bool
can_edit: bool
should_retry: bool
def is_valid_email // TypeScript
let isAuthenticated: boolean;
let hasPermission: boolean;
let canEdit: boolean;
let shouldRetry: boolean;
function isValidEmail: booleanBad Examples:
let authenticated: bool; // Ambiguous: state or action?
let valid: bool; // Valid what?
Prefixes:
is_/is: State check (is_empty, is_active)has_/has: Possession (has_permission, has_data)can_/can: Capability (can_edit, can_delete)should_/should: Recommendation (should_retry, should_cache)will_/will: Future action (will_expire, will_redirect)
Collection Naming
#collections #naming #plurality
Use plural nouns for collections, singular for items.
Good Examples:
// Rust
let users: ;
let active_sessions: ;
for user in users.iter
// Python
users: list
active_sessions: dict
for user in users:
// TypeScript
const users: User;
const activeSessions: ;
for Bad Examples:
let user_list: ; // Redundant _list suffix
let user: ; // Confusing singular for collectionFunction Naming by Purpose
#functions #naming #intent
Name functions by what they do, not how they do it.
Query Functions (Read-only)
#query #getter #accessor
// Good
Command Functions (Mutating)
#command #mutation #setter
// Good
Conversion Functions
#conversion #transformation
// Good
Validation Functions
#validation #checking
// Good
Constants and Configuration
#constants #config #magic-numbers
Give meaningful names to magic numbers and configuration values.
Good Examples:
// Rust
const DEFAULT_TIMEOUT_SECS: u64 = 30;
const MAX_RETRY_ATTEMPTS: usize = 3;
const CACHE_TTL_MINUTES: u64 = 15;
const MIN_PASSWORD_LENGTH: usize = 8;
// Python
DEFAULT_TIMEOUT_SECS = 30
MAX_RETRY_ATTEMPTS = 3
CACHE_TTL_MINUTES = 15
MIN_PASSWORD_LENGTH = 8
// TypeScript
const DEFAULT_TIMEOUT_SECS = 30;
const MAX_RETRY_ATTEMPTS = 3;
const CACHE_TTL_MINUTES = 15;
const MIN_PASSWORD_LENGTH = 8;Bad Examples:
const TIMEOUT: u64 = 30; // Timeout for what? What unit?
const MAX: usize = 3; // Max what?
const FIFTEEN: u64 = 15; // Why is this a constant?Units in Names:
- Include units when ambiguous:
_secs,_ms,_bytes,_mb - Use standard abbreviations:
ttl,max,min,avg
Type Parameters and Generics
#generics #type-parameters #templates
Rust:
// Single letter for simple cases
TypeScript:
// Single letter for simple cases
interface Array<T> { }
function map<T, U>(value: T, f: (val: T) => U): U
// Descriptive for complex cases
interface Repository<TEntity, TId> { }
class Cache<TKey, TValue, TStorage> { }Convention:
- Single letters:
T(type),E(element),K(key),V(value),R(result) - TypeScript prefix: Use
Tprefix for descriptive types (TEntity,TUser)
Module and File Naming
#modules #files #organization
Rust:
- Files:
snake_case.rs - Modules:
snake_case - Binaries:
kebab-case
// Good
src/user_session.rs
src/http_client.rs
bin/terraphim-server
// Bad
src/UserSession.rs
src/HTTPClient.rsPython:
- Files:
snake_case.py - Packages:
snake_case
# Good
/
/
# Bad
/
/TypeScript:
- Files:
camelCase.tsorkebab-case.ts(choose one per project) - Components:
PascalCase.tsx
// Good (if using camelCase)
src/userSession.ts
src/httpClient.ts
src/components/UserProfile.tsx
// Good (if using kebab-case)
src/user-session.ts
src/http-client.ts
src/components/UserProfile.tsxAbbreviations and Acronyms
#abbreviations #acronyms #readability
Common Acceptable Abbreviations:
id(identifier)url(uniform resource locator)uri(uniform resource identifier)html(hypertext markup language)json(JavaScript object notation)api(application programming interface)db(database)ttl(time to live)uuid(universally unique identifier)
Acronym Casing:
// Rust: Treat acronyms as words
class HttpClient
class UrlParser
function parseJsonResponse
// Constants: All caps
const MAX_HTTP_REDIRECTS = 10;
const DEFAULT_API_TIMEOUT = 30;Avoid:
// Bad
// Should be HttpClient
Related Patterns
See also:
- [[documentation-standards]] - Documenting code
- [[code-organization]] - Structuring modules and packages
- [[api-design]] - Naming in public APIs