8 Replies - 1125 Views - Last Post: 26 September 2014 - 02:35 PM Rate Topic: -----

#1 Hikaros   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-September 14

How are form's classes supposed to be?

Posted 24 September 2014 - 09:33 AM

I don't even know how to ask this properly but i have some doubts. Up until recently i thought using Regions was a good idea to separate methods from events, fields, etc. But as i asked about them i was told they are bad and that i should never rely on regions and that partial classes solved the issue for which regions were useful for. I explained that i use regions because CodeMaid makes it simpler to navigate the code, and i was told that if the class is too complex to navigate through then it is too big. All those things made me wonder a lot of things.

A Form's class looks like:

  public partial class form1 : Form
    {
        public form1()
    		{
    			InitializeComponent();
    		}

        // Events, Methods, Etc.
    }


and there, visual studio adds the automatically generated code (control's events).

- If that class goes probably over 900 lines of code, is that wrong?

- Is it wrong if i place the methods that take care of information's
processing there?

- What is, exactly, considered a big class?

- When am i supposed to move methods to a different class? (besides the
obvious methods that will/can be re-used for a helper or extension
class)

- Why and how did partial classes solve the issues for which regions
were used for?

Is This A Good Question/Topic? 0
  • +

Replies To: How are form's classes supposed to be?

#2 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: How are form's classes supposed to be?

Posted 24 September 2014 - 11:52 AM

Regions and partial classes are unrelated features.

#region

Quote

#region lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. In longer code files, it is convenient to be able to collapse or hide one or more regions so that you can focus on the part of the file that you are currently working on.

Regions are a simple navigational tool within Visual Studio, and occur within the same file.

partial classes

Quote

It is possible to split the definition of a class or a struct, an interface or a method over two or more source files.

I believe that partial classes were developed originally to assist Microsoft developers, partly because some of their classes were quite big.

Classes can be very large, with lots of methods, properties and events. Look at some of the C# classes that we work with for examples: look at the String Class methods.

More often though, and especially for beginners, large classes arise because they are doing more than they should and you could take it as an indication that it requires other helper-classes, or more structure (a class hierarchy) and sub-classes.

Quote

- If that class goes probably over 900 lines of code, is that wrong?

This is a slightly different, but related, issue. If your form-class has over 900 lines then I strongly suspect that you are doing too much within this class, and you are not separating your GUI from your program logic:

separation of concerns :wikipedia

In fact, with over 900 lines, I feel certain that you aren't separating your GUI and program logic.

This post has been edited by andrewsw: 24 September 2014 - 12:14 PM

Was This Post Helpful? 3
  • +
  • -

#3 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: How are form's classes supposed to be?

Posted 24 September 2014 - 12:17 PM

Layered Application Guidelines :MSDN
Introducing MVP (Model-View-Presenter) Pattern (WinForms)
Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How are form's classes supposed to be?

Posted 24 September 2014 - 01:16 PM

Just adding my agreement. Regions aren't "bad". In fact, if you decompile your code's output (the DLL or EXE) you wouldn't see any hint of your #region statements. Because #region and other #statements are Preprocessor Directives.

Preprocessor directives have different uses, but generally they tell the compiler something, but aren't included in output. They're not part of your code, they're "meta information". In #region's case, the preprocessor completely ignores them. They're there for your benefit only.

That being said, it's entirely possible to abuse them. They are no substitute for proper code organization. They can make a code file cleaner and easier to navigate, but if you rely to heavily on them it's likely you're not using SOLID principles.

As to your "over 900" question: there is no set number. I've had people tell me I need line breaks if a line goes over 80 characters. That's ridiculous in the modern world of 16:9 monitors, but it's baked into some people's heads that because we fixed-size terminals in the past that we need to stick with it. However: that's not to say that all rules in that vein are useless. Once your methods start getting bigger than "one screen" (a dumb metric because screens have varying DPI and resolutions, but it works) you should look at refactoring. When you need a ton of comments for you or anyone else to know what's going on with your code, you need refactoring (my goal is to be able to read my code from left to right and know what it's doing. Not inside out, not all over the place, make it read out as close to English as possible and use descriptive names to make comments redundant except for why).

And especially if you find yourself with a massive class, you need to look at refactoring. Consider whether you can abstract functionality into a DLL layer. That's one of the best ways to learn code separation: make all the business logic defined in a separate assembly, make your UI layer a project with nothing but user interaction with the business layer. That kind of strictly-enforced code separation is tough, but makes you learn. And the added benefit is, when you need to update the UI, you can do it completely independently from the underlying logic.
Was This Post Helpful? 1
  • +
  • -

#5 Hikaros   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-September 14

Re: How are form's classes supposed to be?

Posted 24 September 2014 - 02:02 PM

Alright, i somewhat understand most of what you guys are telling me hahaha since there is a lot of info i must read i will try to work with it and come back with you guys!


View Postandrewsw, on 24 September 2014 - 11:52 AM, said:

In fact, with over 900 lines, I feel certain that you aren't separating your GUI and program logic.


You are correct hahaha, i will try to work with what everyone has told me to modify it and come back to check if i did it correctly.
Was This Post Helpful? 0
  • +
  • -

#6 Hikaros   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-September 14

Re: How are form's classes supposed to be?

Posted 26 September 2014 - 10:32 AM

Not sure if i can double post to give notice of an update x_x if i can't i'm sorry, i will merge the post.

I have read everything, read carefully the information that you guys gave me and specially this:

View Postandrewsw, on 24 September 2014 - 12:17 PM, said:



I understood (i think) and i tried to change the the class i mentioned before (is the form's class) but i have some doubts as of (in fact i have no idea) how to modify the TreeView (since the form modify's a TreeView, it is for creating menu profiles) from the presenter class.

Can you please explain to me how do one modify a control like a TreeView(checking, unchecking, expanding and collapsing nodes) and DataGridView (binding data, adding rows, deleting rows, etc) from the presenter?

Also, in the example that was provided in the MVP tutorial the model was a Task class with fields, did i understand correctly if i think that in a normal case that class doesn't exist because it is the database itself?

Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: How are form's classes supposed to be?

Posted 26 September 2014 - 11:29 AM

View PostHikaros, on 26 September 2014 - 05:32 PM, said:

Also, in the example that was provided in the MVP tutorial the model was a Task class with fields, did i understand correctly if i think that in a normal case that class doesn't exist because it is the database itself?


tutorial said:

The model is usually very simple and in our case will be a single class that has properties that we want to store. We will store our data in a (non-persistent) List<> of this type. In a fuller example there will be a database (or other persistent storage) and the model would reflect those tables and fields that the application can work with.

From wikipedia:

wikipedia said:

The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.

So the model does exist and is not the database itself but an object relational mapping. That is, classes with properties and methods that act against the database, rather than sql statements and commands that are called directly.

Think of it this way: the model does not expose the entire database to the application, but only those tables and fields that the application needs, wrapped in class properties and methods.

This post has been edited by andrewsw: 26 September 2014 - 12:23 PM

Was This Post Helpful? 1
  • +
  • -

#8 andrewsw   User is offline

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: How are form's classes supposed to be?

Posted 26 September 2014 - 12:19 PM

Quote

Can you please explain to me how do one modify a control like a TreeView(checking, unchecking, expanding and collapsing nodes) and DataGridView (binding data, adding rows, deleting rows, etc) from the presenter?

Typically, expanding and collapsing of a TreeView doesn't involve any interaction with data, other than with the (static) data that has already been obtained to populate the TreeView.

The Presenter might usually be involved when data of the TreeView or DataGridView needs to be synchronized with the data source (via the model).

You have to bear in mind that MVP is one of many patterns, and combinations of patterns, and nothing is set in stone. Which pattern might be suitable to a particular application, and whether to stick rigidly to a chosen pattern, are not clear-cut decisions.. unfortunately. (In fact, sticking too rigidly to a pattern can be detrimental to the application.)

This is an important article:

GUI Architectures :Martin Fowler



From the earlier discussion though, the first step is to separate the application logic from the GUI, not necessarily to straight-away enforce a particular pattern. (My tutorial is an introduction.) If all your code is in the Form Class then this needs to be resolved.

This post has been edited by andrewsw: 26 September 2014 - 12:24 PM

Was This Post Helpful? 1
  • +
  • -

#9 Hikaros   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 24-September 14

Re: How are form's classes supposed to be?

Posted 26 September 2014 - 02:35 PM

Oooh i was confused for a while there then haha i thought the model was the database itself, but thanks, i understand now :D

Thanks for the GUI architecture link, i will study it. And don't worry haha since you did mention the:

Quote

MVP is one of many patterns, and combinations of patterns, and nothing is set in stone.


i really won't be sticking to a pattern to the letter in every project. I'm all in for variations :)

Quote

If all your code is in the Form Class then this needs to be resolved.


Working on it! :D

Thank you for your help. If i get stuck somewhere i shall update again but hopefully it's be fine. Thank you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1