Is it possible to program the method body inside an event?

I recently saw some college teacher code where he had something like this:

public void Button1_Click(blabla)
{
   //His code was here.
}

Isn't it considered good programming practice to call a method to do the dirty work, so if the method changes, you need to change the method itself, not the event? (Less likely to break something)

public void Button1_Click(blabla)
{
   DoSomething();
}

public void DoSomething()
{
   //The actual code here.
}
+3
source share
9 answers

You only need to extract DoSomethingif

a) its functionality is needed somewhere else, or perhaps

b) This is a fairly long piece of code.

If the event is the only place it used, then there is no real need to retrieve it.

+8
source

, , DoSomething ( ).

- , . -:

button.Click += delegate { DoSomething(); }

, , .

, , Visual Studio. , Visual Studio , , - ( -). , .

, , .

+4

, .

DoSomething() ,... .

Button1_Click - . , . , , , .

+1

? (, Visible/Enabled) , . , , - / , , , , , . , UI , .

+1

, .

, 1 2 , , , .

0

, "", - YAGNI ( ).

: :

  • , .

. , , , , , , . - YAGNI.

0

, - . "DoSomething()" , , , .

0

, . , , .

Grz, Kris.

0

(, MVC, MVVM) , ( ) .

( ) , .

0
source

Source: https://habr.com/ru/post/1750825/


All Articles