Dev Guide
WebsiteGitHubGitBook
  • 📝Dev Intro
    • Using GitHub
    • Using Unity
    • Licensing
    • File Naming & Organization
      • File Naming
      • File Organization
        • Object Type
  • 📡Networking
    • Introduction to Game Networking
    • FishNet Networking
      • Server
      • Client
      • Server RPC
      • ObserversRPC
      • Network Message
  • 🖊️Guidelines
    • The C# Style Guide
      • Nomenclature
        • Namespaces
        • Classes & Interfaces
        • Methods
        • Fields
        • Parameters
        • Delegates
        • Events
        • Misc
      • Declarations
        • One declaration per source file
        • General class structure
        • Access Level Modifiers
        • Spacing
        • Brace Style
        • Switch Statements
        • Language
        • Common Patterns and Structure
          • Applying attributes on all network related methods.
    • Code Design Definitions
      • Actor
      • System
      • View
      • Events
        • Event Bus
        • Action
      • System Locator
      • Tweening
    • Asset Criteria
      • External Criteria
        • Animations
        • Fonts
        • Models
        • Textures
        • Graphics
        • Audio
      • Importing Criteria
        • 3D Models
        • 3D Animations
        • Textures
        • Graphics
        • Audio
      • Internal Criteria
        • 3D Models
        • 3D Animations
        • Textures
        • Graphics
        • Audio
    • SS3D's coder good practices
    • Code design patterns
  • 📖Guides
    • Application Settings
    • Running the Project
      • Configure your firewall and antivirus
      • Building the game
      • Joining a server
      • Hosting a server
      • Setting up a dedicated server
    • Debugging SS3D
    • Maintainer Guide
      • Pull request review process
    • Testing SS3D
      • Assets audit tests
      • Edit mode tests
      • Play mode tests
        • Inventory test
        • Health tests
    • Working with animations
  • 🛣️Roadmap
    • Releases
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Guidelines
  2. The C# Style Guide
  3. Declarations

Common Patterns and Structure

This section includes some rules of thumb for design patterns and code structure

Error handling

Avoid throwing exceptions. Instead log and error. Methods returning values should return null in addition to logging an error

BAD:

public List<Transform> FindAThing(int arg){
    ...
    if (notFound) {
        throw new NotFoundException();
    }
}

GOOD:

public List<Transform> FindAThing(int arg){
    ...
    if (notFound) {
        Debug.LogError("Thing not found");
        return null;
    }
}

Finding references

Don't use Find or in other ways refer to GameObjects by name or child index when possible. Reference by reference is less error prone and more flexible. Expect people to set the fields in the inspector and log warnings if they don't.

BAD:

private GameObject _someMember;

private void Start() {
    _someMember = GameObject.Find("ObjectName");
}

RequireComponent

Prefer RequireComponent and GetComponent over AddComponent. Having the components in the inspector let's us edit them. AddComponent limits us.

BAD:

public class : MonoBehaviour
{
    private AudioSource _audioSource;

    private void Start() {
        _audioSource = gameObject.AddCompenent<AudioSource>();
    }
}

GOOD:

[RequireComponent(typeof(AudioSource))]
public class : MonoBehaviour
{
    private AudioSource _audioSource;

    private void Start() {
        _audioSource = GetCompenent<AudioSource>();
    }
}

Properties with backing fields

Properties can be used for access control to fields, and when using backing fields they can be private and let us change them in the inspector. Consider when a fields should be public and prefer properties with backing fields.

Sometimes it's just nice to see them for debugging, even if we don't change them, so consider making more of your private fields visible.

OKAY:

public GameObject SomeMember;

BETTER:

[SerializeField] private GameObject _someMember;
public GameObject SomeMember => _someMember;
PreviousLanguageNextApplying attributes on all network related methods.

Last updated 2 years ago

Was this helpful?

🖊️