Skip to content

Coding nomenclature

Allan Brunner edited this page Sep 22, 2023 · 4 revisions

Coding Nomenclature

💡 Why using it: This file is here to give a nomenclature for every elements. This way, everyone will have quite the same way of coding and it will be much easier to read and understand each other code.
💡 How to use it: For each element of code, there will be an explanation and an example on how to use it. If there is no information about a thing you want to make, propose your version by writing it and notifying @Allan Brunner or @DOLBA3B

Namespace

A namespace is a way to organize related code elements and manage the scope of names used in code.
Naming Convention : ZC.AppName.Dir
Example: ZC.TimeCalculator.Utils


Classes

A class is a blueprint for creating objects that have properties, methods, and events. It defines the structure and behavior of objects that can be created from that class.

Regular classes: Used to define properties, methods, and events for objects that will be created from that class.
Naming Convention: PascalCase
Example: public class Person {...}

Private classes: Used to define a class that can only be accessed within the containing class.
Naming Convention: PascalCase, starting with an underscore.
Example: private class Person {...}

Internal classes: Used to define a class that can be accessed within the same assembly.
Naming Convention: PascalCase
Example: internal class Person {…}

Abstract classes: Used to define common functionality that will be used by derived classes, and cannot be instantiated directly.
Naming Convention: PascalCase
Example: public abstract class Animal {...}

Sealed classes: Used to prevent further modification or specialization of a class.
Naming Convention: PascalCase
Example: public sealed class Circle {...}

Static classes: Used to define methods and properties that can be called directly from the class itself, without the need to create an instance of the class.
Naming Convention: PascalCase, often ending with "Helpers" or "Utility".
Example: public static class MathHelpers {...}

Partial classes: Used to split a class into multiple files, allowing different parts of the class to be defined in different places.
Naming Convention: PascalCase, same name across all files defining the partial class.
Example:

public partial class Person
{
    public string FirstName { get; set; }
}

public partial class Person
{
    public string LastName { get; set; }
}

Nested classes: Used to group related functionality together or to provide a more organized structure for a complex system.
Naming Convention: PascalCase, often describing the relationship between the nested class and the outer class.
Example: public class Person {... public class Address {...} }


Method / Functions

A method or function is a block of code that does something specific, like performing a computation or returning a result. They're used to make programs easier to manage and reuse code across different parts of the program.

Instance methods: Methods that are called on an instance of a class and operate on that instance's data.
Naming Convention: PascalCase
Example: public void DoSomething() {...}

Static methods: Methods that are called on the class itself and do not operate on any instance data.
Naming Convention: PascalCase, often starting with a verb.
Example: public static int Add(int x, int y) {...}

Extension methods: Methods that extend the functionality of an existing class, typically used to add functionality to classes that you cannot modify directly.
Naming Convention: PascalCase, often ending with "Extension".
Example: public static class StringExtension {... public static bool IsNumeric(this string input) {...} }

Async methods: Methods that run asynchronously and do not block the calling thread, typically used when waiting for I/O operations or long-running operations.
Naming Convention: PascalCase, often ending with "Async".
Example: public async Task<int> GetResultAsync() {...}

Lambda functions: Functions that are defined inline and can be passed as arguments or assigned to variables, typically used in LINQ queries or to define event handlers.
Naming Convention: N/A (the naming convention for the containing variable or parameter should be followed).
Example: Func<int, bool> isEven = x => x % 2 == 0;


Variables

Variables are used to store data of different types in memory during program execution.

Local variables: Variables that are declared within a method or block of code, and can only be accessed within that method or block.
Naming Convention: camelCase
Example: int age = 25;

Instance variables: Variables that are declared within a class, and are unique to each instance of that class. They should always be private, if it’s needed to be in public, use a property for it.
Naming Convention: camelCase
Example: private int age = 25;

Static variables: Variables that are declared within a class, and are shared across all instances of that class.
Naming Convention: camelCase
Example: private static int count = 0;

Readonly variables: Variables that cannot be reassigned once they have been initialized.
Naming Convention: PascalCase
Example: private readonly string Name = "John";

Const variables: Variables that are constant at compile-time and cannot be changed during program execution.
Naming Convention: PascalCase
Example: public const int MaxCount = 100;


Properties

Properties are used to encapsulate the state of an object, providing controlled access to that state.

Instance properties: Properties that are associated with an instance of a class and provide access to the data associated with that instance.
Naming Convention: PascalCase
Example: public int Age { get; set; }

Static properties: Properties that are associated with a class itself and provide access to the data associated with that class.
Naming Convention: PascalCase
Example: public static int Count { get; private set; }

Read-only properties: Properties that can be read but not modified. Naming Convention: PascalCase
Example: public int TotalCount { get { return count; } }

Write-only properties: Properties that can be modified but not read.
Naming Convention: PascalCase, starting with "Set" to indicate that it is a write-only property.
Example: public int SetValue { set { value = value; } }


Enums

Enums are used to define a set of named values.

Regular enums: Enums that define a set of named values.
Naming Convention: PascalCase
Example: public enum Colors { Red, Green, Blue }

Flags enums: Enums that define a set of bit flags, allowing for easy combination and comparison of values.
Naming Convention: PascalCase, ending with "Flags".
Example: [Flags] public enum Permissions { Read = 1, Write = 2, Execute = 4 }


Interfaces

An interface defines a contract that classes can implement. It specifies a set of methods, properties, and events that a class must provide, without specifying how those members should be implemented.

Regular interfaces: Used to define a contract that a class can implement.
Naming Convention: Start with "I", followed by PascalCase.
Example: public interface ILogger {...}

Inheritance in interfaces: Used to define an interface that extends the functionality of another interface.
Naming Convention: Start with "I", followed by PascalCase, ending with the name of the base interface.
Example: public interface ILoggingRepository : IRepository<LogEntry> {...}

Implementing interfaces: Used to define a class that implements the members of an interface.
Naming Convention: PascalCase, the class name should describe the behavior of the class.
Example: public class FileLogger : ILogger {...}

Explicit interface implementation: Used to implement an interface's members that have the same name but different signatures, to avoid naming conflicts.
Naming Convention: N/A
Example:

public interface IAuditable
{
    void Audit();
}

public class Order : IAuditable
{
    void IAuditable.Audit()
    {
        // Implementation here
    }
}

Delegates

A delegate is a type that represents references to methods with a particular parameter list and return type. Delegates are used to pass methods as arguments to other methods or to store methods as data members.

Regular delegates: Used to define a delegate that can reference any method with a matching signature.
Naming Convention: PascalCase, ending with "Handler" or "Delegate".
Example: public delegate void ButtonClickHandler(object sender, EventArgs e);

Multicast delegates: Used to combine multiple delegates into a single delegate, which can then be called to invoke all the delegates in the chain.
Naming Convention: Same as the underlying delegate type. Example: public delegate void CalculationPerformed(int result); ... CalculationPerformed calculation = AddNumbers; calculation += MultiplyNumbers; calculation(42);


Attributes

An attribute is a way to add declarative information to your code, specifying metadata that can be used by the compiler, runtime, or other tools.

Regular attributes: Used to add declarative information to a class, method, property, or other code element.
Naming Convention: PascalCase, often ending with "Attribute".
Example: [Serializable] public class Person {...}

Conditional attributes: Used to specify that a method should only be called under certain conditions.
Naming Convention: PascalCase, often ending with "Attribute".
Example: [Conditional("DEBUG")] public void DebugLog(string message) {...}

Obsolete attributes: Used to mark a code element as obsolete, indicating that it should not be used and providing a message explaining why.
Naming Convention: PascalCase, often ending with "Attribute".
Example: [Obsolete("Use the new method instead.")] public void OldMethod() {...}

Metadata attributes: Used to add metadata to a class, typically used by tools like the debugger or code analyzers.
Naming Convention: PascalCase, often ending with "Attribute".
Example: [DebuggerDisplay("{FullName,nq}")] public class Person {...}**


Generics

Generics are a way to write classes and methods that can work with any type, allowing you to create reusable and type-safe code.

Generic classes: Classes that can work with any type, specified by a type parameter.
Naming Convention: PascalCase, often using "T" as the type parameter name.
Example: public class List<T> {...}**

Generic methods: Methods that can work with any type, specified by a type parameter.
Naming Convention: PascalCase, often using "T" as the type parameter name.
Example: public void DoSomething<T>(T value) {...}**


Comments

Comments are used to document and explain code to make it more readable and understandable.

Simple comments: A simple comment is a way to add human-readable text to your code that is not executed by the compiler. It is used to provide additional information to the person reading the code to make it easier to understand.
Naming Convention: Space between the comment delimiter and the text.
Example: // I'm a comment!**

XML Documentation comments: An XML Documentation comment is a special type of comment used to document code elements such as classes, methods, properties, and fields. It is structured in a way that allows tools to extract and display information in a readable format.
Naming Convention: Space between the comment delimiter and the rest.
Example: /// <summary> I'm an XML Documentation comment! </summary>

Clone this wiki locally