Poma
Poma

Reputation: 8474

Should I place every class in separate file?

Should I place every class in separate file? Even those short helper classes that are used only in one place? Like this one:

public class IntToVisibilityConverter : GenericValueConverter<int, Visibility>
{
    protected override Visibility Convert(int value)
    {
        return value == 0 ? Visibility.Collapsed : Visibility.Visible;
    }
}

Upvotes: 3

Views: 5480

Answers (6)

&#216;yvind Br&#229;then
&#216;yvind Br&#229;then

Reputation: 60694

That depends greatly of personal preference, but I like to do it.

In this case, I would have a folder inside my application called ValueConverters, and put all converters, including short ones, inside their own files.

I find it makes it easier to get an overview of what your project consist of from the Solution Explorer.

Upvotes: 5

xanatos
xanatos

Reputation: 111870

I'll rephrase the question for you: should I use StyleCop? (it includes this rule). The answer is yes. I use it and my code is much more readable (but I have to admit I disable all the rules that require the method documentation to be complete :-) )

I do think that when you program in a team, having a fixed and uniform code format is very important. And even when you program "solo". A cluttered code is more difficult to read and errors can hide better in the clutter :-)

Upvotes: 3

Justin Pihony
Justin Pihony

Reputation: 67075

Typically, IMO yes. Think about any new developers who must find where code lives. Yes, you can use go to definition, but that is not the be all, end all. However, I will say that sometimes if you have an interface that is small and only used for the class that it is within, then you can probably get away with it. However, even that can expand and later be required to be pulled out (and maybe those contracts should be in another namespace anyways).

So, ultimately, I would say the majority of the time, yes, but there are some caveats. As with anything, it is never black and white

Upvotes: 0

M.Schenkel
M.Schenkel

Reputation: 477

It is usually the best practise to put every class in a seperate file. Taking into account your short helper classes; you could create a helper class which contain all your helper methods, to prevent having way too many classes. If your helper class gets too big, you can seperate your helper functions per category

Upvotes: 1

mbx-mbx
mbx-mbx

Reputation: 1775

I do this and it is usually best practice to do so, but it is sometimes a matter of opinion.

Upvotes: 6

Oded
Oded

Reputation: 499022

It is good practice to do so.

You can easily find the class if you name the file after the class.

Resharper has a built in error for classes not matching the file name they are in...

Upvotes: 0

Related Questions