Alan K
Alan K

Reputation: 2017

Whether to put method code in a VB.Net data storage class, or put it in a separate class?

TLDR summary: (a) Should I include (lengthy) method code in classes which may spawn multiple objects at runtime, (b) does doing so cause memory usage bloat, (c) if so should I "outsource" the code to a class that is loaded only once and have the class methods call that, or alternatively (d) does the code get loaded only once with the object definition anyway and I'm worrying about nothing?

........

I don't know whether there's a good answer to this but if there is I haven't found it yet by searching in the usual places.

In my VB.Net (2010 if it matters) WinForms project I have about a dozen or so class objects in an object model. Some of these are pretty simple and do little more than act as data storage repositories. The ones further up the object model, however, have an increasing number of methods. There can be a significant number of higher level objects in use though the exact number will be runtime dependent so I can't be more precise than that.

As I was writing the method code for one of the top level ones I noticed that it was starting to get quite lengthy.

Memory optimisation is something of a lost art given how much memory the average PC has these days but I don't want to make my application a resource hog. So my questions for anyone who knows .Net way better than I do (of which there will be many) are:

Any thoughts appreciated.

Upvotes: 2

Views: 234

Answers (2)

Steve
Steve

Reputation: 5545

The short answer, it doesn't matter. Your data is stored in memory but your code is loaded only once.

EDIT: I guess I need a longer answer.

If you have 10 instances of a class, the variables that are part of that instance all take up thier own memory space. So if you have 10 properties, variables, etc, that means you have 100(ish) items in your memory. As for your code, it was loaded just once with your assembly. If you create 10 instances of your class, your code is not in memory 10 times.

Upvotes: 2

StingyJack
StingyJack

Reputation: 19479

Go with whichever is going to be the easier to maintain codebase (shorter methods, etc). That is the more important cost with anything that has increasing complexity.

Memory optimization is only a problem if its a problem. 12 classes is really nothing, when you have hundreds of instances of hundreds of classes, then it may become a problem.

Upvotes: 3

Related Questions