SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru Raskolnikov's Avatar
    Join Date
    Jul 2003
    Location
    USA
    Posts
    606
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# and code practices

    So i have been trying to learn how to make my code more user friendly (in the possiblility i may not have this project in my hands much longer).

    I wanted to get some feedback on a structure on this site that i learned from one of the first sites i ever built with .net.

    Basically I have a .cs file called utilities.cs. It contains all of my regularly used classes. so anytime within the site that i need to call one i just include it within the using area and call the class as needed.

    it works well however i am not sure it conforms to the codeing standards (which i know none of by the way). It contains roughly 30 different functions.

    The problem is when i have it open it is tough to filter through. I have commented almost all of it so that the next user will have an idea what is going on, but it makes for alot of stuff to look at. I have just added #regions which makes it much easier to scroll through, but have read that if you need to use regions, then you probably have over complicated structure.

    Is having one control with that many functions within it bad coding practices?

    Ras

  2. #2
    always learning . . .
    Join Date
    Nov 2003
    Location
    UK
    Posts
    821
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally I don't think the issue is whether you have too many lines in your class but more the structure of your code.

    Maybe I am wrong but I take away from your explanation that you struggle with the more object orientated way of thinking.

    If one class (object) has many lines of code then the class has many lines of code, this can't be helped all the time. Look into partial classes to help seperate the class into more than 1 .cs file.

    Also in terms of structure, break your application down into a logical structure.
    Have objects that represent real world items.
    E.G a time sheet application would have a timesheet object which would have properties like startDate and endDate, and maybe have methods like Reset and SendForApproval

    Structuring your code into layers seperating the presentation logic from the business logic from the data logic is a good idea too.

    These are just initial thoughts from my reading.

  3. #3
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes! Here are some links that will get you started down the road of refactoring: http://en.wikipedia.org/wiki/Single_...lity_principle and http://en.wikipedia.org/wiki/Separation_of_concerns

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio, USA
    Posts
    1,494
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds counterintuitive to suggest a Java book for C#, but it's close enough. I strongly suggest Heads Up Design Patterns. It'll help change the way you think about writing code.

  5. #5
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I second that recommendation. All the books I've been reading lately are Java, if your fairly comfortable with C# then the Java examples should be easy to follow.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •