Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've a c# file that will contain, most likely 10,000+ lines of code. I'd like to break this file into tabbed segments, so I can organize each method into a certain tab and label the tab something like : initial setup, helper functions,execution logic,list structures,global variables, etc.

If I were using PHP I would have separate files and use include(); to include the separate files. What can be done, following the same style, with c#?

share|improve this question
    
Does the file contain multiple objects? –  Ben Brocka Feb 16 '12 at 22:31
1  
I'm just glad you're not doing a giant #Include<BigBallOfmudPart1> project with me on your team. –  Warren P Feb 16 '12 at 23:34
6  
Uh, why are you not breaking this thing up into smaller segments. I find it hard to believe you can't pull out segments of 10,000 LOC to isolate into their own files/classes. I'd hate to have to maintain whatever it is you are working on... –  Rig Feb 16 '12 at 23:57

5 Answers 5

up vote 10 down vote accepted

First, since you are talking about C# and Notepad++, have you considered Visual C# 2010 Express? It's free, and is much more powerful for C#, especially when it comes to visualizing source code in other forms (like class diagrams) or refactoring it.

Second, there are two possible cases for the C# file to have 10,000+ LOC:

  • Either there is something wrong with it, i.e. the code lacks architecture and, for example, was written in a procedural way, with 10,000+ LOC in a single method with plenty of goto. In this case, you need to create the architecture, than refactor the source code to match your architecture.

  • Or the file contains just dozens of classes, combined in a single file for an unknown reason. If this is the case, you can separate them by putting one class per file in the same project. If you keep the namespace the same, it will magically work (if there are no classes inside other classes).

  • Also, you could separate class members into several files using partial classes.

Third, if you don't want to refactor the code and can't split it into several files, a temporary solution would be to add #region blocks to be able to collapse code you don't work on at the moment.

share|improve this answer
1  
is #region supported by notepad++? –  ratchet freak Feb 16 '12 at 23:55
    
@ratchet freak: #region works in Programmer's Notepad (pnotepad.org). Don't know about Notepad++ since I don't have it installed, but it's a powerful application which is particularly extensible, so either it supports it natively or there is a plugin for it. –  MainMa Feb 17 '12 at 0:06
2  
If your app is dependent upon #region to maintain then you've got more problems than picking an editor. –  Wyatt Barnett Feb 17 '12 at 1:08
    
@Wyatt Barnett: have you read the whole last sentence of my answer, especially the "if you don't want to refactor the code and can't split it into several files" and "temporary" in bold? –  MainMa Feb 17 '12 at 1:36
1  
Hudson; If you're impressed by NOtepad++s capabilities, you haven't yet seen what Visual Studio can do. Read some tutorials and watch some video (youtube) demos of using Visual Studio to navigate. Full professional Visual Studio + something like COdeRush or Resharper is truly impressive. Check them out. –  Warren P Feb 21 '12 at 21:06

You are looking at this the wrong way.

Your code needs to be refactored from being a big ball of mud to something with a proper design.

You don't just need to think about what "tabs" have what code. You need to think about what classes contain what methods, which perform some "object oriented" task.

Sure, C# partial units could be a help to you. But you're approaching this the wrong way around.

Learn to code, and improve your code and learn to refactor and rewrite your code. Surely you have a huge 10K line unit and it needs to be broken up into separate units. That's called "making your code better", or "refactoring".

share|improve this answer
    
Warren thanks for your comments, this is why I'm bringing the question to the community, to see how experienced c# organize their code. When I write in PHP, I use a file based organization system and it works very well. With C#, I hear there are better or similar ways, and I'm on my way to find out what they are. –  Hudson Feb 18 '12 at 22:45
    
I think you need a good book. How about something like this: shop.oreilly.com/product/0636920000679.do?CMP=ILC-hf1st –  Warren P Feb 21 '12 at 21:05
    
Whatever practices you learned with PHP might have to be unlearned when you learn a Real Programming Language. –  Warren P Apr 23 '12 at 21:45

C# allows for partial classes, which lets you split a class definition across multiple physical files, exactly as you could do it in PHP. Whether or not that is a good idea is a different question...

share|improve this answer

Well, in notepad++ you could roll up the tree if you put curly brackets or an #ifdef preprocessor around your various sections. That's that a tabbed list, but it will make moving about

This is non-functional extra code purely for the sake of readability though, which is a bad idea in general.

Otherwise, you could bite the bullet and split up your massive file.

share|improve this answer

A large class is generally a code smell which indicates that the class is most likely trying to take responsibility for doing too many things. If you ever hope to write good SOLID code, then you need to start thinking about your class as being a single purpose type as per the Single Responsibility Principle - aka the first initial of the "SOLID" acronym.

Here's another principle that you may find useful. DRY, or Don't Repeat Yourself. It has been my experience that when you have very bloated classes with more than a few hundred lines of code, it is most likely that you will have a lot of repetition within the code. This is usual the result of Cut-And-Paste coding that often occurs when developers are in a hurry to hack together something that works, with little thought to the design and future maintenance of the code.

Large classes are often indicators of a large accrual of Technical Debt that will likely be very costly to maintain, and difficult to change.

So with all of these issues I've bombarded with you, what can you do about it? First of all, looking at ways to reorganize your code is an admirable task, but using IDE tricks such as tabbing, regions, or something else to hide the mess will not help you ti fix your problem, and will most likely end up making your situation worse. I would recommend instead that you look to refactoring your code.

  • Break your long methods out into a collection of smaller ones.
  • Use these smaller methods to help you to remove duplication.
  • Identify what the class is intended to do, and move all of those methods that don't do what the class intended out into other classes.
  • Move your new classes into new source files, and this will give you all of the benefits you are hoping to gain from the IDE tricks I mentioned earlier, as well as making it easier for an entire team to work on the same code base while minimizing the number of 3-way merges you are likely to encounter if you leave everything in a single file.

Keeping lots of little classes in their own files would also achieve the ability to add functionality through "includes" (or using statements in C#) in exactly the same way as you would in other languages.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.