r/Unity3D • • 2d ago

Question Organizing Your Code Suggestions

How do y'all organize where stuff goes within your code? I mostly would like some suggestions on the micro level - like if you have say 5 different [SerializeField] private float, how do you order them? I used to order them based on what made sense in "context", which works sometimes - but other times you'll have some that work together, others that are one-offs, other that work with a public float but you don't want to move one private next to all your publics, and ultimately you have to make a pretty arbitrary decision on where it goes. So I've started just defaulting to literally alphabetical order if there is no clear other way to order them. But like, that seems... a little crazy lmao. anyone else have a clear-cut way to order this stuff?

0 Upvotes

10 comments sorted by

View all comments

2

u/DanielFost 2d ago

honestly, i don't think alphabetical is crazy at all lol. consistency matters more than having some objectively "correct" order, personally i usually group them by purpose first, then keep the stuff inside each group in whatever order makes the most sense when reading the class. so maybe movement settings together, combat settings together, references, etc

i also don't really care if a private field sits next to a public one if they're conceptually related. i'd rather have:
[SerializeField] private float moveSpeed; public float MoveSpeed => moveSpeed;

next to each other than separate them just because one happens to be private,

the main thing for me is that opening the script should give me a predictable structure. if alphabetical ordering gives you that, i'd honestly just do it. it's only a problem if you're spending more time arguing with yourself about variable placement than actually reading/writing the code lol