?
Solved

Organizing Code

Posted on 2003-12-03
7
Medium Priority
?
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 450 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
Independent Software Vendors: 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 150 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 450 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 150 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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!
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

764 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