Foundgine

HomeDocumentationContributingCode Style

Code Style

Contents


General Principles

Code should be:

When faced with two implementations of equal performance, always choose the simpler one.


Architecture First

Every implementation should respect project boundaries.

Foundation

↑

Runtime

↑

GraphQL

Never introduce shortcuts that violate dependency direction.

Architectural consistency is more important than reducing a few lines of code.


File Organization

One public type per file.

Example:

EntityMetadata.cs

QueryPlanner.cs

GeneratedMetadataProvider.cs

Avoid grouping unrelated public types in the same file.


Naming

Names should describe intent.

Prefer:

ResolveJoinMetadata()

BuildMutationPlan()

WriteConflictClause()

Instead of:

Resolve()

Build()

Write()

Variables should also be descriptive.

Good:

entityMetadata

columnReference

joinMetadata

Avoid:

x

tmp

obj

data

Method Size

Methods should generally perform one logical task.

Large methods should be decomposed into private helpers.

Instead of:

BuildEverything()

Prefer:

ResolveMetadata()

BuildProjection()

BuildOrdering()

BuildFilters()

Small methods are easier to understand and test.


Immutability

Prefer immutable types.

Example:

public sealed class EntityMetadata
{
    public ushort Id { get; }

    public string Name { get; }

    public ImmutableArray<ColumnMetadata> Columns { get; }
}

Mutable state should be limited to execution-specific objects.


Exceptions

Throw exceptions only for exceptional situations.

Validation errors should occur during planning or generation whenever possible.

Runtime should rarely encounter invalid metadata.


Comments

Comments should explain why, not what.

Good:

// Preserve deterministic alias ordering for snapshot stability.

Avoid:

// Increment i.
i++;

Code should be self-explanatory whenever possible.




← Previous: Contributing Next: Testing