You are on page 1of 10

Magic Pushbutton

By Jeff Welch
Description
 Magic Pushbutton is an anti-pattern consisting of a system
partitioned into two parts : user interface and business logic, that
are coupled through a " single " point, clicking the " magic
pushbutton " or submitting a form of data

 A technique that creates undesirable constraints which make it


difficult to code business logic in an easy-to-maintain way

 One of the most off-putting aspects of a magic pushbutton is its


tendency for the user interaction to proceed by entering a large
volume of data, then having it rejected for some unexpected
reason
Typical Scenarios

 A very large list of insurance claim codes might be filtered to a


much smaller list, if the user has already selected
Home/Car/Pet insurance, or if they have already entered their
own identification and so the system can determine the set of
risks for which they're covered, omitting the obscure policies
that are now known to be irrelevant for this transaction
Sample Usage Code
procedure TForm1.Button1Click(Sender: TObject);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKey_Current_User;
    if reg.OpenKey('\Software\MyCompany', true) then
    begin
      reg.WriteString('Filename', Edit1.Text);
    end;
Example of Magic Pushbutton
private void btnSendTestEmail_Click(System.Object sender,
System.EventArgs e)
{
    try
    {
        string mail = "To: fubar@customer.com{0}{0}This is a test email!";
        // Show a MessageBox representing the email.
        MessageBox.Show(string.Format(mail, Environment.NewLine),
          "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
        // Log to the UI. Not best practice, but it will do for the example.
        this.txtLog.Text += "-- Email was sent." + Environment.NewLine;
catch (Exception ex)
    {
        // Show the user that something went wrong.
        MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.
OK,
                        MessageBoxIcon.Error);
        // Then log the Message. Imagine someone forgetting
        // the -- or NewLine. Your log would be a mess!
        this.txtLog.Text += "-- "
+ ex.Message + Environment.NewLine;
    }
}
UML

 In the UML using the "magic


pushbutton" takes you back to the
beginning or resets
Advantages of the Magic Pushbutton

 None, this is anti-pattern, so you do not want to incorporate

 Does not benefit anyone, from user to programmers


Disadvantages of the Magic Pushbutton

 Hides the complexity inside each module, and devalues interface quality
relative to cost

 Tendency for the user interaction to proceed by entering a large volume of


data, then having it rejected for some unexpected reason

 The code behind the Pushbuttons grows unmanageably

 Changing the user interface or adding an alternate interface is difficult

 Testing the code is difficult


References

 Rossel, S. (2011, April 30). What not to do: Anti-patterns and


the solutions. CodeProject. Retrieved May 25, 2022, from
https://www.codeproject.com/Articles/187230/What-not-to-d
o-Anti-Patterns-and-the-Solutions
 

 Wikimedia Foundation. (2022, May 1). Magic Pushbutton.


Wikipedia. Retrieved May 25, 2022, from 
https://en.wikipedia.org/wiki/Magic_pushbutton

You might also like