views:

896

answers:

6

Hello,

I have a class file that contains all the classes that are needed for a certain web application. Currently I'm at line 7269 and it contains numerous classes. I'm not particularly worried but I've started to notice that when working on this file Visual Studio responds slower. I'm thinking that this may be caused by the size of the file and Visual Studio's re-compile after every enter key press.

Has anyone got any recommendations for limits to lines per file of classes per file or perhaps guidelines as to how/why I should move classes into separate files?

I found this regarding Java but I'm specifically concerned with VB and Visual Studio 2005

http://stackoverflow.com/questions/107855/is-there-any-no-of-lines-code-limit-for-a-java-class

Thanks

EDIT There are about 50+ classes in the file, some tiny, some large!

+14  A: 

Egad.

It's highly recommended by just about everyone that classes are contained in a file per class.

Lines isn't very relevant directly, but it can be a symptom that you're creating an overly complex god class and need to break the logic up. Unit test driven development forces this when done correctly.

edit (noticed the "why?"):

from the fact you're asking it should be obvious :)

A few (of many reasons):

  • it's very hard to visualise the actual code layout in large files

  • most IDEs are default set-up to show multiple files at a time, but not multiple views of the same file

  • source control over a single file can be a problem: "can you please check in {godfile} so I can work on the project?"

  • fragmenting code into namespaces/packages/branches is a problem

annakata
Hi, the file contains about 50+ classes, some quite big so I could understand needing a file per class but some are tiny and a file each seems like overkill
David A Gibson
Your edit calls it a {godfile} which I like (the concept not the file) as I can understand your reasons why this is wrong.
David A Gibson
it's not overkill it's future proofing - I advise you to go look at some projects on sourceforge for comparison
annakata
What about Enumerator Classes? Would they be OK in the same file? The class structure is inherited and a bit of mess but I do understand what your saying - if I don't think it's worth a file of it probably shouldn't be class!
David A Gibson
some things (enums, structs, delegates, custom exceptions) can be more usefully kept either together, in the global/main class, or with the class which uses them - it's hard to specify for your case, but if you bear in mind readability and maintenance you'll be ok
annakata
Note that Java actually enforces the 1-top-level-class-per-file rule. It takes some getting used to, but really is far easier to navigate once you know it.
Joachim Sauer
+1 for best ever use of Egad, and because I agree with your response.
Binary Worrier
+5  A: 

One top-level type per file is conventional - and while nested types are okay, don't go overboard. There are exceptions - if I need to declare a number of delegates, I'll often put them all in one file (e.g. Delegates.cs for C#) - that just makes sense as each delegate type just consists of a method signature, basically. Some people do the same for enums, though I prefer not to.

This makes your code easier to navigate, quicker to compile, and shows changes in source control more easily too.

Beyond that - I start to worry if a single class is over 1000 lines of code without a really good reason. There can be good reasons, but you should at least look at whether the class has too many responsibilities. I've seen classes which cover enough functionality to deserve their own namespace before now (with a suitable number of smaller classes providing the required functionality)...

It's not clear from the question whether or not any of your individual classes is that huge, but separating the classes into "one per file" should at least help to start with.

Jon Skeet
A: 

1 class, 1 File, annakata is absolutely right.

In some cases really big classes you might consider using partial classes to split them in different files.

With only one really big file... that must be a nightmare to work in a team, how do you guys do it? I am guessing no source control and every change you need to make you must copy it to a deployment copy of the project.

That is a disaster just waiting to happen. Imagine if by any chance that one file got corrupted?

Sergio
Source control that must "check out" file to edit it -- that is nightmare to work :).
Eugene
A: 

Visual Studio 2008 has a few performance issues if a ASP.NET page is a little bit complicated or large. Fortunately, there is an official hotfix for the problem. Installing the hotfix really helped me a lot as Visual Studio 2008 got much faster, so I recommend giving it a try:

https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=10826

DrJokepu
A: 

One class per file. I try not to go over 200 lines per file (excluding comments), but it does happen.

Refactor, refactor, refactor!

hmcclungiii
A: 

Like almost everybody here, I suggest you 1 class per file. I always try to keep it clear and to separate concept and it's naturally tend to be under 600 lines of codes.

I was doing unittest by namespace and my files were going over 3k-4k of code. Now, I have split them in multiple file. Much more easy for maintenance and it's more fast if you have add-in like ReSharper (ReSharper over 2k lines slow down VS).

Daok