Solved

Organizing Code

Posted on 2003-12-03
7
364 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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