Solved

Organizing Code

Posted on 2003-12-03
7
361 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
Comment Utility
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
Comment Utility
Can you give me an example of what a helper class is and how it's used?
0
 
LVL 7

Expert Comment

by:jj819430
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Assisted Solution

by:shahpaulin
shahpaulin earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now