C# Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jumbojs
    New Member
    • May 2009
    • 20

    C# Classes

    I would like some advice on how I should, or if I should break up this class. Right now the class is about 4000 lines of code but it seems hard for me to break up the class. The only downfall I see right now is that it can be a little difficult to read.

    Anyway, application takes product information from one database and imports it, after doing some conversion, into another database.

    I have some classes to interact with the different databases but most of the logic is handled in one class.

    Basically the class does this, checks to see if the product already exists, builds sku, get's the product description, attaches a special to the product and adds the product to a category.

    So, to me this seems that since it all deals with a product, it all should be in the same class.

    I guess I have a few questions. Does this seem like it would all belong in the same class and What are some of the best tips to know what belongs in what class? Does 4000 lines seem to long? Should I use partial classes if it sounds like all this information does belong in one class?
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    I read once somewhere that its a good practice to keep each method within a class to a length of 24 lines of code or less. Of course you cannot always have tiny methods like that, but the principle is good, check if you could optimize and shorten any of your methods.

    Secondly check that your code comments are not bloated, too many comments really don't help, on the other side of that coin I do hope, for the sanity of whoever is to maintain that code, that it is indeed commented :)

    Check if you can make any of your methods generic, especially frequently used ones. You can then move all those re-usable methods to a common library outside of your class. The code which handles the CRUD operations of the DB is generally the easiest to move out of a class.

    Partial classes are a great thing (IMHO). I generally split large classes into partial classes by grouping related methods together. For your class you might group all the methods used to add a product to a category for example.

    These are just some general ideas, hope it helps.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Classes should be designed in such a way that it makes sense to have the members/methods of that class in that class.

      If you break up a class purely because it has a lot of code in it, and create a new class then this class will be tightly bound to the original class. Changes made in either class are going to directly effect the other class and could potentially break applications if modifications are made to either class in the future.

      My advice would be to look at the class design. Do the methods and properties belong to that class...if you move them out of the class, what effect will that have on the coupling between your classes?

      -Frinny

      Comment

      Working...