Solved

Organizing Code

Posted on 2003-12-03
7
367 Views
Last Modified: 2010-04-16
I'm designing a class, and once again I'm faced with the old problem: When fully implemented, the length of the class (lines of code) will be very large. Perhaps 1500 lines of code.

So the question becomes: How can I somehow divide/partition/organize the code so that it is partitioned into smaller chunks?

I've had this picture in my mind that if you can organize information into a tree/hierarchy, with each level of the three having hopefully <= 10 nodes, that information seems readable. It's when you nodes of the tree with, perhaps 40 children, that things are very hairy.

Where this comes into code is that I wish you could design code into hierarchies. What is the appropriate way to do this? What do you do when you have a class that has, say 10 data members, but has a function within it that is 1000 lines long and a few other functions totalling 500 lines? You can break the 1000 line long function into parts so that each part is on average say 75 lines long, but then you have this long list of component functions which are unorganized. And your .CS file is still huge.

I guess I'm just a bit frustrated with organizing my code, and I wish I could:

- Not have any .CS files that are big (>500 lines)
- Organize code into hierarchies

I'm rambling.

Thoughts?
0
Comment
Question by:daniel_bigham
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 150 total points
ID: 9872916
Use regions. I always divide my classes to regions like: publuc functions, public members, event handlers, etc. It's up yo you to decide the level and content of this hierarchy. It may depend also on class type: form, control, generic class etc.
Use helper classes when possible.
Use Class View as much as possible - when you access your code from Class View, you don't care about it's length.

0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9874754
Can you give me an example of what a helper class is and how it's used?
0
 
LVL 7

Expert Comment

by:jj819430
ID: 9878535
What I have found to be useful is to break up the classes.

Say you are doing something simple like a linked list class.

Well you would have the Root Class = Linked List.

You would have Root's children = Add to Linked list, Delete from Linked List, Sort Linked List, Search Linked list .... Etc.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Assisted Solution

by:shahpaulin
shahpaulin earned 50 total points
ID: 9883635
I am elaborating on regions.
I use them and they create a lot of space on the screen and help to organize my codes.

you just write
#region "MyClass"

======ur relevant code=====

#endregion "MyClass"

The above statements will cause the code between *regions* to drop down into a tree and create a node by name "MyClass", which you can expand and close to see the code in between.

One other feature in .net while writing big codes and working with many files in the same project is TODO keyword.

While writing a code, if you come across a loop/funcion which you want to deal with later, just
write
           'TODO write up the connection
And you can go into Menu->View->Show Tasks, it will give you a listing of all *TODO*'s in your project and when u click it will directly take you to that place.  its a very cool feature which i find very helpful.

Hope this helps.

-Paulin
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 150 total points
ID: 9883730
Helper class is just class created to solve some subtasks. For example, if you have some string-processing functions in your class:

string s;
Operation1(s);
Operation2(s);

...

you may decide to vreate another class for these operations:

string s;
StringHandler h;
h.Operations(s);
h.Operation2(s);


0
 
LVL 1

Author Comment

by:daniel_bigham
ID: 9884259
But a helper class won't have access to the implementation of the class its helping?
0
 
LVL 1

Assisted Solution

by:bakobako
bakobako earned 50 total points
ID: 9885270
Other than using regions, which I find really helpful, the rest depends on how you decide to architect the class. The use of helper classes must be tempered by when they make since to use them.
 You can also look to see if anything in the methods breaks out logically as separate classes, or structures. If you look at it you may see different aspects of what you are doing break out into a separate class. But all of this depends on what you are doing. The regions are the only things I can think of which would help make things more bearable if the architecture just doesn’t break down any further.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question