How To Improve Code In Unity

If you are serious about game development, you should seriously consider improving your programming skills. Yet, what does it mean to improve or write better code? How do you write better code? In this series of articles, we will give you a complete overview on how to improve your code in Unity.


How To Improve Code in Unity

To write better code in Unity there is a lot to consider. Our goal is to give you a complete overview on all of the ways on how to improve your code in Unity. A lot of improvements have to do with basic software design principles. Yet, Unity is unique and thus requires a special approach when it comes to writing better code. So what can we do to improve our code in Unity?

How To Improve Your Code in Unity using SerializeField
Click to Learn About SerializeField

Use SerializeField in Classes

Are you familiar with the SerializeField Attribute? If not, you are at risk of exposing members of your classes. Without SerializeField, you are at risk of delegating implementations and improved code duplication. Continue Reading…

Write Small Scripts

Do you write huge scripts that do a lot of things? You should seriously consider stop doing that right now. By creating small scripts, it’s easier for us to adhere to the Single Object Responsibility principle. Every script should really only do one thing. Continue Reading…

How To Improve Your Code in Unity Using Small Scripts
Click to Learn About Small Scripts
How To Improve Your Code in Unity by Reducing Hardcoding
Click to Learn About Hardcoding

Reducing Hardcoding

Are you guilty of hardcoding values? This turns your code in a rigid structure while it should be flexible for fast iterations. Because of hardcoding, you are stuck changing your code while you should develop your game. Continue Reading…

Use Enumerations

Have you ever heard of Enumartions or Enums? Stop trying to use integers for indexers. By using enumerations you make your code constants more flexible and more readable. Next to that, they work extremely well with the Unity Editor. Continue Reading…

How to improve code in Unity using Enums
Click to Learn About Enums
How to improve code in Unity using abstraction
Click to Learn About Abstraction

Use Abstraction

One of the best ways to improve your code in Unity is by using abstraction. Through abstraction you will learn that for classes it’s not always important to know how something is done. Instead, classes should only care about doing it. Continue Reading

Decouple Through Mediators

Did you learn to separate all of you logic into classes, yet are there too much dependencies between them? Consider using Mediators that will reduce the amount of dependencies between classes. Continue Reading

How to improve code in Unity using mediators
Click to Learn About Mediators
How to improve code in Unity using comments
Click to Learn About Comments

Use Comments Wisely

Do you actively use comments to describe how your code works? That’s great! With comments, it’s important not to comment too much. Instead, you should focus on writing self documenting code. After all, the best comment is no comment! Continue Reading.


Summary

What you learned in this article:

  • Unity is unique and thus requires a special approach when it comes to writing better code
  • Without SerializeField, you are at risk of delegating implementations and improved code duplication.
  • By creating small scripts, it’s easier for us to adhere to the Single Object Responsibility principle.
  • Because of hardcoding, you are stuck changing your code while you should develop your game.
  • By using enumerations you make your code more flexible and more readable.
  • Through abstraction you will learn that for classes it’s not always important to know how something is done.
  • Consider using Mediators that will reduce the amount of dependencies between classes
  • With comments, it’s important not to comment too much. Instead, you should focus on writing self documenting code

Conclusion

To conclude, we hope you now have a better understand of what better code is and how you can improve your code in Unity. In this series, we have given you an overview of the many different angles associated with writing better code. Next, we have outlined best practices and pitfalls that new developers often run into.