More of a general question on Code Organization..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: More of a general question on Code Organization..

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Thumbs up More of a general question on Code Organization..

    Hey folks, thanks for taking the time to read my post.

    I'm trying to develop a clean way to organize my latest program (first time developing with C#). I'm trying to avoid global/public variables while at the same time trying to avoid massive chains of function calls, particularly on performance-sensitive blocks (i.e. rendering).

    So first off, I have a MainForm class, which represents the primary window of my application (and is utilized by the Application.Run() function within Main(). The first question I have is about this MainForm class - should I make this static? I have no intention of allowing multiple instances of the window open. The alternative to this would be to create a public instance of the MainForm class, stored in the Program class, so that other classes in my program can access the mainForm. The second solution feels a bit ugly, but is generally what I've done in the past. Any comments?

    Next, I use an openGL control within my MainForm (thanks to the TAO library). I would like some other classes (particularly, the Graphics Class) to have access to this control, i.e. to determine its size etc. Currently, I pass it the the graphics class's constructor, and allow the graphics class to store a reference to the control locally. This again feels ugly, and basically entangles the two classes.

    It feels like I'm only really using classes to put code into separate files. They all end up horribly intertwined anyway! How do you handle this situation? Something I've considered is making various controls on my UI class read-only (not through the keyword but by creating a public property which only defines a get accessor), so that other classes can access but not modify them... but then this brings up the problem of having to create a program-wide public (global) instance of the UI! Kinda brings us back to square one.


    As you can probably guess, I'm suffering this 'problem' throughout all of my work. Yeah, I've gotten better over time, but still don't feel like it's down solid. I'm not looking for a particular answer - instead, I'm hoping to hear some methodologies or general guidelines that you guys have adopted over time.

    Again, thanks for reading the post, despite its slight-rambling. I look forward to learning some new tricks!

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: More of a general question on Code Organization..

    Quote Originally Posted by USNavyFish View Post
    Hey folks, thanks for taking the time to read my post.

    I'm trying to develop a clean way to organize my latest program (first time developing with C#). I'm trying to avoid global/public variables while at the same time trying to avoid massive chains of function calls, particularly on performance-sensitive blocks (i.e. rendering).
    You don't have to worry about that, since C# doesn't allow global values.

    Quote Originally Posted by USNavyFish View Post
    So first off, I have a MainForm class, which represents the primary window of my application (and is utilized by the Application.Run() function within Main(). The first question I have is about this MainForm class - should I make this static? I have no intention of allowing multiple instances of the window open. The alternative to this would be to create a public instance of the MainForm class, stored in the Program class, so that other classes in my program can access the mainForm. The second solution feels a bit ugly, but is generally what I've done in the past. Any comments?
    No need to make it static. Are you going to create two instances of it? Probably not, and no one else is either, so don't sweat it.

    Quote Originally Posted by USNavyFish View Post
    Next, I use an openGL control within my MainForm (thanks to the TAO library). I would like some other classes (particularly, the Graphics Class) to have access to this control, i.e. to determine its size etc. Currently, I pass it the the graphics class's constructor, and allow the graphics class to store a reference to the control locally. This again feels ugly, and basically entangles the two classes.
    If you are worried about this, then just pass the size/position data.

    Quote Originally Posted by USNavyFish View Post
    It feels like I'm only really using classes to put code into separate files. They all end up horribly intertwined anyway! How do you handle this situation? Something I've considered is making various controls on my UI class read-only (not through the keyword but by creating a public property which only defines a get accessor), so that other classes can access but not modify them... but then this brings up the problem of having to create a program-wide public (global) instance of the UI! Kinda brings us back to square one.
    There isn't any concept of global in C#. Classes shouldn't be designed based on file layout. You might want to read up on how to design a classes for a given problem. It doesn't really relate to files (btw, with partial classes, a class can be defined over several different cs files).

  3. #3
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: More of a general question on Code Organization..

    Even I am working on a similar kind of setup for my project. Sometimes you feel you have to leave with it as your application requirement is so complex and get used to dealing with classes getting entangled with in one another !!

    mine is basically a design tool ( UML tool) developed in C# for drawing various UML diagrams...

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: More of a general question on Code Organization..

    Sometimes you feel you have to leave with it as your application requirement is so complex and get used to dealing with classes getting entangled with in one another !!
    That's where Test Driven Development comes in.

    In addition to giving you good unit test coverage, I've found it also helps you organise and design your application.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: More of a general question on Code Organization..

    can you provide useful links for that test driven development...

  6. #6
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: More of a general question on Code Organization..

    Try googling... that's all I would do to give you the links

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  7. #7
    Join Date
    Oct 2009
    Posts
    3

    Red face Re: More of a general question on Code Organization..

    I wiki'd Test-Driven Development.. http://en.wikipedia.org/wiki/Test-driven_development

    Thanks for mentioning that practice.. It seems like a really good idea. I like the notion of feature-oriented programming, getting the program to first produce a desired result and only then going back to refactor.

    @Arjay - thanks for the feedback. I am aware that 'Globals' aren't a part of C# per-se, but I am using that term to describe a public variable within the main "Program" class. Not technically the same, but I think the end functionality is similar to a global. Of course correct me if I'm wrong!


    W.R.T. Partial classes - I have seen the Visual designer spread my MainForm over several files, using partial. I think that's very convenient, so I don't have to see all the initialization code for the controls and their properties. Would you elaborate on what you mean by "Classes shouldn't be designed based on file layout" ? I'm under the impression that it's best to make no more than one class per file (and one class can be spread over multiple files using partial). Is this what you mean?


    Quote Originally Posted by Arjay
    If you are worried about this, then just pass the size/position data.
    Yes, I think that is good advice. It also seems to agree well with the TDD concept. I think I will apply that kind of solution to alot of other classes - basically, have them send only the required data to another function (i.e. just width/height instead of the whole object's reference). Of course, there will still be cases when one class requires data members from another....

    And that's my next question.. Say I have a MainForm class and Input class. When the input class calls its "ProcessPolledInput" function, it needs to check if MainForm.ActiveControl == MainForm.openGLControl.. and only process the input (i.e. WASD for movement) if the openGLControl is active.

    Of course, the issue here is how to give the Input class access to the MainForm class, WITHOUT writing "MainForm.openGLControl" into my ProcessPolledInput function - because doing so essentially ties the two classes together.

    The only solution I've got so far is to create private instances of each class (i.e. MainForm, Graphics, Input) within the program class, then locate my update loop in the program class, and have that loop pass these private members back and forth between each other, i.e.:

    Code:
    public Class Program
    {
         private MainForm _MainForm;
         private Input _Input;
    
         ...
    
         private void UpdateLoop()
         {
               ...
               If ( _MainForm.ActiveControl == _MainForm.openGLControl)
               {
                     _Input.ProcessPolledInput(_MainForm.openGLControl);
               }
               ...
         }
    }
    I suppose it's OK to expose openGLControl in this way, through the use of a Property, or the readonly keyword (I might shy away from properties due to the extra CIL-overhead, especially since this particular function call is made very often).


    Any more tips or techniques on good ways to expose data members in a consistent, logical way while avoiding a mess of entanglement? Again, thanks for reading..

  8. #8
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,485

    Re: More of a general question on Code Organization..

    USNavy, since you are just stating development with C#, I'd skip the whole Winforms route and move directly to WPF (Windows Presentation Foundation).

    One of the goals of design is to decouple the UI from the business logic and there are several design patterns that help with this (MVC, MVP, MV-VM, etc.).

    In my opinion, because WPF has more advanced data binding than WinForms, the application of design pattern is more straightforward.

    WPF also offers greater control customization capabilities, dynamic skinning and other features over WinForms.

  9. #9
    Join Date
    Oct 2009
    Posts
    3

    Re: More of a general question on Code Organization..

    WPF and XAML certainly look interesting. I'm concerned about the performance of 3D applications though. That and having to learn a version of D3D (not even the full thing, I take it). I'm fairly well into OpenGL at this point, and jumping ship isn't very appealing.

    Then again, the flexibility of those interfaces, and the fact that all the UI is basically in a XAML file and not mucking up my code is pretty neat.

    Thanks for the recommendation. Not sure what I'll end up doing in the long run, but in the short-run I'm sticking with my TAO-enabled WinForms, OpenGL... and perhaps a few small projects to learn WPF, on the side ;-)

  10. #10
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    422

    Re: More of a general question on Code Organization..

    Quote Originally Posted by darwen View Post
    Try googling... that's all I would do to give you the links

    Darwen.

    thanks for that

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center