About interface event handling

When I work with user interface, if into a event handling method there is some code that not manages interface elements but implements logic, I prefer not write that code directly into the method but I create other method with the logic and then recall this method into the event handling method.
This implementation is a little bit complex and verbose than a direct implementation but is more open and reusable for the future development.
Consider this scenario and suppose to use c++ as programming language: we have a method that manage the clik on a certain button, called MyButton, on my user interface. The method for click management is MyButtonOnClick(TObject *Sender):

void MyClass::MyButtonOnClick(TObject *Sender)
{ 
   if(!sender)
      return;
   
   int param1 = getParam1();
   int param2 = getParam2();
   //this is the logic write directly into the event handling method
   int ret = param1 + param2;
   
   ResultLabel->Text = StringToInt(ret);  

}

In MyButtonOnClick there is a piece of code that implement addition of two params (from other interface elements or from other methods). This addition can be wrapped into other method so I can reuse that.

My implementation begin something like this:

int MyClass::AddParam(int param1,int param2)
{
   int ret = param1 + param2;
   return ret;   
}

void MyClass::MyButtonOnClick(TObject *Sender)
{ 
   if(!sender)
      return;
   
   int param1 = getParam1();
   int param2 = getParam2();

   int ret = AddParam(int param1,int param2);
   
   ResultLabel->Text = IntToString(ret);  

}

In this way i can reuse the AddParam Method into other methods.

The example is very simple but explain clearly the initial these.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s