-3

I am pretty much new to programming, but recently I began to learn C# intensively for Visual C# and for Unity. I have noticed that I use many scripts that have absolutely the same content in different applications.
An example of this script would be the script I use in most of my games in Unity, ObjectPooler. It has got absolutely identical contents in every game, so I can just copy it from one project to another and it will work as intended.
How do you think, is it better to write a script from scratch every time I need it, or maybe just have a library of most used scripts to simply copy and paste them on demand?

1 Answer 1

3

If you find yourself copy/pasting code, nine times out of ten you're missing an abstraction. That's not a guarantee, but it is generally correct. Especially as a beginner, you're going to miss abstractions that a more experienced developer would implement immediately (and that's okay - we've all had to learn this, mostly the hard way).

In this particular case, it's less of a pressing issue as you're copying across projects, but it's the same principle at play.

I'm no Unity3D expert, but I would assume that you can reference C# libraries (whether via project reference or DLL). So it makes sense here for you to create a separate library with your class, so that it can be used as a dependency in all your different projects.

Is that necessary? No. But if you find a bug in your class tomorrow, and you fix it, are you going to have to revisit all your projects and copy/paste the same fix over and over? Because that is an excellent reason to use libraries instead, as all of your projects can simply reference the same project, so you have to implement your fix only once.

Note: when referencing a DLL directly, I'm unsure whether you're going to have to manually update the reference in your projects or not. Like I said, I'm not a Unity3D expert.


The answer was written with class libraries in mind, but can apply to any content. The core question is always: when you change the shared content, do you want that change to propagate to all consumers of this shared content? If so, look for a solution that has these consumers reference the same shared content instead of duplicating it endlessly.

1
  • This also sounds like a terrific opportunity to learn how to create a NuGet package. Commented Apr 23, 2020 at 21:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.