C# – Should I keep all the class definitions in the same class library

clibrariesprogramming practices

I am starting to work with class libraries in my C# class. We were told to write code that can be used more than one time. Should I keep all my class definitions in one class library for the length of the term, or separate them by project?

Best Answer

Separate them by purpose. A class library is supposed to be cohesive. You shouldn't throw arbitrary things together.

For example, I just wrote an updater library. It has classes that deal with updating an application. But it doesn't have classes for some neat string operations because it doesn't make sense to add them. Any time I want to reuse some code, I want to reuse just one thing (for example this updater). If you throw everything together, it will be a mess and you will also force yourself to reference the whole thing when you really want to use just one. Give yourself the freedom to reuse the parts you want.

Writing truly reusable code is hard, but I definitely encourage you to try and do this. You will learn important things about writing actually reusable code.

I also suggest reading about YAGNI and KISS principles. Not to blindly follow, but to be aware of them.

Related Topic