Naming conventions: Difference between revisions
Content added Content deleted
Line 1,525: | Line 1,525: | ||
global variables. |
global variables. |
||
=={{header|Zig}}== |
|||
Zig is a new (pre-1.0) language, so it hasn't had opportunity to gather naming cruft. |
|||
Here are the naming conventions taken from the Zig documentation: |
|||
Roughly speaking: camelCaseFunctionName, TitleCaseTypeName, snake_case_variable_name. More precisely: |
|||
If x is a type then x should be TitleCase, unless it is a struct with 0 fields and is never meant to be instantiated, in which case it is considered to be a "namespace" and uses snake_case. |
|||
If x is callable, and x's return type is <code>type</code>, then x should be TitleCase. |
|||
If x is otherwise callable, then x should be camelCase. |
|||
Otherwise, x should be snake_case. |
|||
Acronyms, initialisms, proper nouns, or any other word that has capitalization rules in written English are subject to naming conventions just like any other word. Even acronyms that are only 2 letters long are subject to these conventions. |
|||
File names fall into two categories: types and namespaces. If the file (implicitly a struct) has top level fields, it should be named like any other struct with fields using TitleCase. Otherwise, it should use snake_case. Directory names should be snake_case. |
|||
;Examples: |
|||
<lang Zig> |
|||
const namespace_name = @import("dir_name/file_name.zig"); |
|||
const TypeName = @import("dir_name/TypeName.zig"); |
|||
var global_var: i32 = undefined; |
|||
const const_name = 42; |
|||
const primitive_type_alias = f32; |
|||
const string_alias = []u8; |
|||
const StructName = struct { |
|||
field: i32, |
|||
}; |
|||
const StructAlias = StructName; |
|||
fn functionName(param_name: TypeName) void { |
|||
var functionPointer = functionName; |
|||
functionPointer(); |
|||
functionPointer = otherFunction; |
|||
functionPointer(); |
|||
} |
|||
const functionAlias = functionName; |
|||
fn ListTemplateFunction(comptime ChildType: type, comptime fixed_size: usize) type { |
|||
return List(ChildType, fixed_size); |
|||
} |
|||
fn ShortList(comptime T: type, comptime n: usize) type { |
|||
return struct { |
|||
field_name: [n]T, |
|||
fn methodName() void {} |
|||
}; |
|||
} |
|||
// The word XML loses its casing when used in Zig identifiers. |
|||
const xml_document = |
|||
\\<?xml version="1.0" encoding="UTF-8"?> |
|||
\\<document> |
|||
\\</document> |
|||
; |
|||
const XmlParser = struct { |
|||
field: i32, |
|||
}; |
|||
// The initials BE (Big Endian) are just another word in Zig identifier names. |
|||
fn readU32Be() u32 {} |
|||
</lang> |
|||
These are general rules of thumb; if it makes sense to do something different, do what makes sense. For example, if there is an established convention such as <code>ENOENT</code>, follow the established convention. |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
* Conventions are for the user to [create and] follow, no enforcement. |
* Conventions are for the user to [create and] follow, no enforcement. |