What's the best and most appropriate way to write code in Winforms?

What's the best way to write code?

(1) How to write code directly in an event button_click()

.

or

(2) Make a function of this code that I am writing in an event button_click()

, and write this function in one class and then this function that I have to call in the event button_click()

. What is it called three-tier approach

for writing code?

As with the event button_click()

, I am writing code to save the records to csv file from datatable

. So I have to write this code in the event button_click()

, or I have to make one new function and one new class and write this code in this which is the new class and calls this function in the event button_click()

.

This is just one example, but I am talking about all the code written in my application , which is appropriate and best way to write the code

and what are the advantages? Please note that I am writing code in Winforms from C #.

+2


a source to share


5 answers


You need to navigate to a separate function in another class. You must do this because you will make the code reusable and create a decent separation between the UI and application logic. For example, you can, for example, change the user interface without affecting the rest of the application.

Also have a look at the MVC pattern , you will see what the best idea is.



The only situation where I think the first option should be used is when it does some action that will affect the UI, and yet I'll create this in a separate function inside the Form class.

If it affects the UI, it should be in the same class because it is related and for example if this is the code to update the grid, I would put that in a separate method inside the same Form class because that could be used to various places within it. So changing the UI doesn't affect the application, you just make your code reusable and maintainable.

+2


a source


It all depends on the situation.

If you are going to make updates to the form, it is better to have the update code in the form. However, if there is a lot of processing, then it is certainly better to design for a separate class to handle the job.



It all depends on the situation.

0


a source


Typically, you don't need any logic in your event handler, as GUIs tend to provide redundant mechanisms (context menu, menu bar, toolbar, acceleration key) to run the same command, and event signatures are inconsistent for all of these. The question then becomes whether your shared function should go in the Form class or in the data model.

I often start with the logic in a Form and then refactor it into model classes as needed. Many small applications will never be large enough to require multiple classes for ease of maintenance. If you avoid code duplication (i.e. Copy + Paste), then refactoring will be easier later if you find it.

0


a source


It's always a good idea to design classes to work with. Because it makes your code reusable and also implements a 3-tier architecture. The advantage is that it is easy to understand. The important thing is that this is only useful if you design your classes appropriately. Develop methods in the class that can be reused for you.
Another advantage is that it hides the complexity of your code.

0


a source


There are two general approaches to adding structure to code: top-down and bottom-up. The top-down structure is concerned with design work, which can include a formal or informal process and pure design artifacts such as UML diagrams or functional specifications. The ultimate goal in a top-down process is to create classes and interfaces in your code that provide the proper structure for your code to be maintained. This can happen before you write the code or as part of an iteration, but the idea is that you first create the structure and then create the code.

The structure below comes from refactoring. For example, start with all your logic with a button click. When you add a menu item that does the same thing as a button click, move the code for the button click function into a private member function on the form. When you need the same functionality in another part of your application, encapsulate the code and state variables used by your private member function in a class.

I would recommend using both approaches. The right mix of your situation depends on the development team (size, location, ability to communicate) and the size of your application. Generally speaking, large applications require more from top to bottom, and very small applications require more from top to bottom. But there should always be high-level planning, even for a small application.

0


a source







All Articles