Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding guides: How do you split up your large source files?

The project I'm working on has just hit 4200 lines in the main C# file, which is causing IntelliSense to take a few seconds (sometimes up to 6 or so) to respond, during which Visual Studio locks up. I'm wondering how everyone else splits their files and whether there's a consensus.

I tried to look for some guides and found Google's C++ guide, but I couldn't see anything about semantics such as function sizes and file sizes; maybe it's there - I haven't looked at it for a while.

So how do you split your files? Do you group your methods by the functions they serve? By types (event handlers, private/public)? And at what size limit do you split functions?

To clarify, the application in question handles data - so its interface is a big-ass grid, and everything revolves around the grid. It has a few dialogs forms for management, but it's all about the data. The reason why it's so big is that there is a lot of error checking, event handling, and also the grid set up as master-detail with three more grids for each row (but these load on master row expanded). I hope this helps to clarify what I'm on about.

like image 324
Nick Devereaux Avatar asked Mar 03 '09 22:03

Nick Devereaux


People also ask

Should I split my code into multiple files?

Splitting your code into multiple files is a great way to produce smaller, more focused files. Navigating theses smaller files will be easier and so will understanding the content of each of these files.

What is the purpose of splitting big chunk of source code to different classes?

For many years this is how things were done until computers got faster and tools got better. That being said if you break up your code into different logical units (Classes, modules etc) is that you can have a lot of short easy to understand pieces of code. that can be maintained later.

Why do we sometimes break your code out into separate functions rather than one large program?

The advantages of splitting a program up in multiple functions are: Most programs have some basic functionality that is needed in multiple places. Having that functionality in a separate function means it is more obvious when that same functionality is used and you also only have to fix problems with it only once.


1 Answers

I think your problem is summed up with the term you use: "Main C# file".

Unless you mean main (as in the method main()) there is no place for that concept.

If you have a catch-all utility class or other common methods you should break them into similar functional parts.

Typically my files are just one-to-one mappings of classes.

Sometimes classes that are very related are in the same file.

If your file is too large it is an indication your class is too big and too general.

I try to keep my methods to half a screen or less. (When it is code I write from scratch it is usually 12 lines or fewer, but lately I have been working in existing code from other developers and having to refactor 100 line functions...)

Sometimes it is a screen, but that is getting very large.

EDIT:

To address your size limit question about functions - for me it is less about size (though that is a good indicator of a problem) and more about doing only one thing and keeping each one SIMPLE.

like image 183
Tim Avatar answered Oct 25 '22 03:10

Tim