Code Guidelines: Difference between revisions
Johnkieser. (talk | contribs) m Johnkieser. moved page Help:Code Guidelines to Code Guidelines: Misspelled title |
Johnkieser. (talk | contribs) mNo edit summary |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
Code Guidelines | |||
This is not a full guide on how to write yaml or C# for ss14!!! These are just guidelines meant to catch the most common issues seen with PRs. | |||
Use the helpful guides that wizden already provides if you are an absolute beginner wanting to code: | |||
https://docs.spacestation14.com/en/general-development/setup/howdoicode.html | |||
== YAML PR Guide == | |||
=== Simple Yaml PRs === | |||
These are one of the most common type of PRs we get. I am describing PRs that modify a small amount of existing yaml or add a small amount of yaml. So think renaming some id, changing the number values of some weapons/armor, adding new components to something ect... Basically simple changes that can be described in a few sentences. | |||
'''THESE ARE NOT ALLOWED IN MOST CASES!!!''' Why? Because yaml is really easy to do(ideally) and so if something obvious and easy needed to be changed but isn't, then that is on purpose and we do not need a random trying to merge simple changes for us. | |||
So how do you get something changed in yaml? A lot of things do require "small" yaml changes and we cant be aware of everything. | |||
Well you need to first go through the discord and actually suggest the change or point out to a dev what needs to be changed and why. So basically try discussing your PR before PRing it to make sure if we actually need it or want it. | |||
BUGFIXES/OBVIOUS MISTAKES, are an exception to this. Still need to go through normal procedures and explain the bug, the fix, what the fix does(if you know the underlying C#), and ect... | |||
=== 'ADVANCED' Yaml PRs === | |||
These include large yaml changes that add new content, restructure/organizes existing yaml, or adds some other feature that requires quite a bit of yaml that might have a fairly advanced syntax(ex construction graphs). These are allowed, especially mass yaml restructuring and organization. Though STILL RECOMMENDED TO TELL DEVS YOU ARE DOING THIS AND TO GET APPROVAL!! OTHER PEOPLE COULD BE DOING THE SAME THING OR ANOTHER CONFLICTING CHANGE IS COMING UP!!! | |||
Other than that it is still recommended to document your changes very well, especially for large yaml edits. Give a rundown on how things are supposed to be organized so we and others can follow suite and make corrections when needed. If you make a huge amount of yaml edits and fail to document or explain things then that is a sure fire way to get your PR closed. | |||
== Brief C# Code Guidelines == | |||
=== See if you can do it in YAML first!!! === | |||
When creating any PR, the first thing you should think is if it can just be done in yaml. You shouldn't create systems/components for a feature if it already exists and is applicable via yaml. The point of having yaml and different in-engine features that "connects" C# to yaml is to make this code easily reusable and just usable in general for other people without having to hard code up a new system every time. | |||
=== Do not reinvent or repeat existing code === | |||
This relates heavily with the above point. When creating new systems, you may need to make use of existing functionality. This is alright in cases, especially if that functionality is heavily coupled to whatever system it exists for and so it is impractical to reuse it. Though for this codebase at least, there's been a problem of contributors completely ignoring perfectly good systems and engine features to create redundant code. They are essentially copying or roughly replicating functions. | |||
SS14 is a game that already does a lot in one place. It doesn't help if there are multiple functions that just exist to do the same thing. The codebase just becomes overly complicated, bloated, and just harder to work with. These sort of additions also cause bugs and instability. Redundant code often won't do the same checks or preform the same measures as the code it copies. This especially gets bad if others use this redundant code, trusting it like it is any other API from the engine. It also makes changing anything magnitudes of times harder since replacing the redundant code with the "correct version" is not simple and often won't be enough. Behavior that players relied on from the old code may not be perfectly replicated. The replaced code may have additional requirements that were neglected before, so an extensive refactor or rewrite might be needed. You actually need to often understand what the redundant code was trying to do in the first place so you know how to remove/replace it. | |||
You may hear about this sort of situation and heard it described as "code debt", and yea it is. Except this version is so easily avoidable in a lot of cases. It is also a sign on AI usage keep in mind. AI commonly isn't able to make use of existing software very well and has to reinvent functions. If you leave your code like that as the AI wrote it, then you're just incredibly lazy and making the code worse in the long term. | |||
=== Design with modularity in mind aka ECS-ify === | |||
If you do not know what ECS is, then go read the basic wizden guides. ECS is here for a reason. When creating new functionality, you want to make it as modular and context free as possible. The ideal to aim for is that your feature needs to be as "plug and play" as possible where someone can easily use your feature by just adding the component. | |||
To that effect, if you're in the middle of making a system and it does a lot of things at once, consider breaking it up into smaller parts. Maybe you're making a new gamemode that spawns waves of mobs. You're going to have to make some mob spawning functionality. That can easily be rewritten during development(especially early on) to be its own system. This is just good software practice in general, as doing this can reduce complexity and speed up development of other systems. Though this is a big part of ECS, as it easily allows you to reuse that system, making it very modular. You may also just find yourself writing functionality that should honestly be a basic utility that can find good use in other areas of code. | |||
Software though is an iterative process. It might not be helpful to start creating with the immediate goal of modularity or ECS, as you may want to focus on creating/figuring out the system first. You can then focus n ECS-fying a system later on. It might even become obvious to you what to ECS-fy. | |||
Though also keep in mind, ECS-fying is not relevant to do in all cases. This is evident for various features that need multiple inter working systems/comps made just for them. An example of that could be atmospherics. Though still, you would be surprised what one decently coded system and some yaml could do. | |||
=== Make use of Datafields. Try not to hardcode values === | |||
We want systems to be as flexible and usable as possible. The main way you modify how your functions work should be with some datafields via yaml. This is also good for testing, as yaml can be very quickly hot reloaded. | |||
=== Do not overuse comments to explain obvious things === | |||
=== Save comments for advanced/hard to understand code === | |||
I've been guilty of this. You may see alot of left over comments like //#Misfits add: or just a lot of comments in general. These are not as helpful as you think. A lot of times the code is pretty self-evident and adding a comment can make things worse. Comments can be misinterpreted or just be outdated as the code is changed. Instead comments should be reserved for summarizing code, explaining WHY things were coded in a specific way, and to explain complex sections of code. | |||
So for example, simple functions may just need a basic summary and other details that users should be aware of(like maybe there's a specific case where the function will crash everything that is easily missed in otherwise simple code). Maybe it is so simple it doesn't really need a summary. Code that really would need comments though are things like atmospherics math and functions. If you do something different from the "norm" or just anything unusual code wise that should be explained too in comments. That is especially true if you needed to write something in a specific way to fix/avoid a bug. DO NOT Just comment "// added this to prevent crash". Expain what exactly causes the crash and how it is fixed/avoided. It'll also help to explain more context, like why the code is needed here in X area but not in Y area. If you have no idea, then be honest about that and explain as much of the context as you can. Keep in mind you can also save such explanations for the PR. | |||
Latest revision as of 21:31, 11 November 2026
This is not a full guide on how to write yaml or C# for ss14!!! These are just guidelines meant to catch the most common issues seen with PRs.
Use the helpful guides that wizden already provides if you are an absolute beginner wanting to code:
https://docs.spacestation14.com/en/general-development/setup/howdoicode.html
YAML PR Guide
Simple Yaml PRs
These are one of the most common type of PRs we get. I am describing PRs that modify a small amount of existing yaml or add a small amount of yaml. So think renaming some id, changing the number values of some weapons/armor, adding new components to something ect... Basically simple changes that can be described in a few sentences.
THESE ARE NOT ALLOWED IN MOST CASES!!! Why? Because yaml is really easy to do(ideally) and so if something obvious and easy needed to be changed but isn't, then that is on purpose and we do not need a random trying to merge simple changes for us.
So how do you get something changed in yaml? A lot of things do require "small" yaml changes and we cant be aware of everything.
Well you need to first go through the discord and actually suggest the change or point out to a dev what needs to be changed and why. So basically try discussing your PR before PRing it to make sure if we actually need it or want it.
BUGFIXES/OBVIOUS MISTAKES, are an exception to this. Still need to go through normal procedures and explain the bug, the fix, what the fix does(if you know the underlying C#), and ect...
'ADVANCED' Yaml PRs
These include large yaml changes that add new content, restructure/organizes existing yaml, or adds some other feature that requires quite a bit of yaml that might have a fairly advanced syntax(ex construction graphs). These are allowed, especially mass yaml restructuring and organization. Though STILL RECOMMENDED TO TELL DEVS YOU ARE DOING THIS AND TO GET APPROVAL!! OTHER PEOPLE COULD BE DOING THE SAME THING OR ANOTHER CONFLICTING CHANGE IS COMING UP!!!
Other than that it is still recommended to document your changes very well, especially for large yaml edits. Give a rundown on how things are supposed to be organized so we and others can follow suite and make corrections when needed. If you make a huge amount of yaml edits and fail to document or explain things then that is a sure fire way to get your PR closed.
Brief C# Code Guidelines
See if you can do it in YAML first!!!
When creating any PR, the first thing you should think is if it can just be done in yaml. You shouldn't create systems/components for a feature if it already exists and is applicable via yaml. The point of having yaml and different in-engine features that "connects" C# to yaml is to make this code easily reusable and just usable in general for other people without having to hard code up a new system every time.
Do not reinvent or repeat existing code
This relates heavily with the above point. When creating new systems, you may need to make use of existing functionality. This is alright in cases, especially if that functionality is heavily coupled to whatever system it exists for and so it is impractical to reuse it. Though for this codebase at least, there's been a problem of contributors completely ignoring perfectly good systems and engine features to create redundant code. They are essentially copying or roughly replicating functions.
SS14 is a game that already does a lot in one place. It doesn't help if there are multiple functions that just exist to do the same thing. The codebase just becomes overly complicated, bloated, and just harder to work with. These sort of additions also cause bugs and instability. Redundant code often won't do the same checks or preform the same measures as the code it copies. This especially gets bad if others use this redundant code, trusting it like it is any other API from the engine. It also makes changing anything magnitudes of times harder since replacing the redundant code with the "correct version" is not simple and often won't be enough. Behavior that players relied on from the old code may not be perfectly replicated. The replaced code may have additional requirements that were neglected before, so an extensive refactor or rewrite might be needed. You actually need to often understand what the redundant code was trying to do in the first place so you know how to remove/replace it.
You may hear about this sort of situation and heard it described as "code debt", and yea it is. Except this version is so easily avoidable in a lot of cases. It is also a sign on AI usage keep in mind. AI commonly isn't able to make use of existing software very well and has to reinvent functions. If you leave your code like that as the AI wrote it, then you're just incredibly lazy and making the code worse in the long term.
Design with modularity in mind aka ECS-ify
If you do not know what ECS is, then go read the basic wizden guides. ECS is here for a reason. When creating new functionality, you want to make it as modular and context free as possible. The ideal to aim for is that your feature needs to be as "plug and play" as possible where someone can easily use your feature by just adding the component.
To that effect, if you're in the middle of making a system and it does a lot of things at once, consider breaking it up into smaller parts. Maybe you're making a new gamemode that spawns waves of mobs. You're going to have to make some mob spawning functionality. That can easily be rewritten during development(especially early on) to be its own system. This is just good software practice in general, as doing this can reduce complexity and speed up development of other systems. Though this is a big part of ECS, as it easily allows you to reuse that system, making it very modular. You may also just find yourself writing functionality that should honestly be a basic utility that can find good use in other areas of code.
Software though is an iterative process. It might not be helpful to start creating with the immediate goal of modularity or ECS, as you may want to focus on creating/figuring out the system first. You can then focus n ECS-fying a system later on. It might even become obvious to you what to ECS-fy.
Though also keep in mind, ECS-fying is not relevant to do in all cases. This is evident for various features that need multiple inter working systems/comps made just for them. An example of that could be atmospherics. Though still, you would be surprised what one decently coded system and some yaml could do.
Make use of Datafields. Try not to hardcode values
We want systems to be as flexible and usable as possible. The main way you modify how your functions work should be with some datafields via yaml. This is also good for testing, as yaml can be very quickly hot reloaded.
Do not overuse comments to explain obvious things
Save comments for advanced/hard to understand code
I've been guilty of this. You may see alot of left over comments like //#Misfits add: or just a lot of comments in general. These are not as helpful as you think. A lot of times the code is pretty self-evident and adding a comment can make things worse. Comments can be misinterpreted or just be outdated as the code is changed. Instead comments should be reserved for summarizing code, explaining WHY things were coded in a specific way, and to explain complex sections of code.
So for example, simple functions may just need a basic summary and other details that users should be aware of(like maybe there's a specific case where the function will crash everything that is easily missed in otherwise simple code). Maybe it is so simple it doesn't really need a summary. Code that really would need comments though are things like atmospherics math and functions. If you do something different from the "norm" or just anything unusual code wise that should be explained too in comments. That is especially true if you needed to write something in a specific way to fix/avoid a bug. DO NOT Just comment "// added this to prevent crash". Expain what exactly causes the crash and how it is fixed/avoided. It'll also help to explain more context, like why the code is needed here in X area but not in Y area. If you have no idea, then be honest about that and explain as much of the context as you can. Keep in mind you can also save such explanations for the PR.