Refactoring collapsiblesplitter (1)

Source: Internet
Author: User
Tags repetition

As the ultimate version of the splitter control, collapsiblesplitter in the rssbandit project provides the feature I dreamed of: It can split two adjacent controls like the splitter control and allow them to be adjusted during runtime, it also provides the function of minimizing the specified control when you click, and draws a very intuitive fine pattern on a small separator bar. Oh? You do not have this control. You can find it here.

Is there something unsatisfactory about this control? Well, yes. It can only have 8 pixels in width (when placed vertically). It cannot be set to the default 4 pixel width of splliter, nor can it be set to 16 pixels in width unexpectedly.

Open the code file of collapsiblesplitter-how can I smell a bad smell? Oh, Comrade Martin Fowler said: this is the bad smell of code. We should remove it from deodorization.

Let's eliminate the odor.

Let's take a look at the bad smell of this stuff:

  1. Too many switch and if statements are used to throw the advantages of object orientation to the Java country. Let's take a look at these codes.

    This is the code in the togglesplitter () method. There is a lot of such code in this control:
    Code 1
    If (controltohide. Visible)
    {
    If (useanimations)
    {

    If (parentform! = NULL)
    {
    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {

    }
    Else
    {

    }
    }

    }
    Else
    {

    If (expandparentform & parentform! = NULL)
    {
    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {

    }
    Else
    {

    }
    }
    }
    }
    Else
    {
    // Control to hide is collapsed
    If (useanimations)
    {

    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {
    If (parentform! = NULL)
    {

    }

    }
    Else
    {
    If (parentform! = NULL)
    {

    }

    }

    }
    Else
    {
    // No animations, so just toggle the visible state

    If (expandparentform & parentform! = NULL)
    {
    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {

    }
    Else
    {

    }
    }
    }
    }
    The following is the code of the animationtimertick () method (in fact, some if... else... nesting are also removed by me). There are three or four Code Methods: Code 2.
    Switch (currentstate)
    {
    Case splitterstate. collapsing:
    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {

    }
    Else
    {

    }
    Break;

    Case splitterstate. Expanding:
    If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right)
    {

    }
    Else
    {

    }
    Break;
    }
  2. The number of lines of code in a single method is too large, and the code repetition rate is high, as the old lady is nagging and ambiguous. Let's take a look at the code lines and repetition rates of these long methods:
    • The togglesplitter () method. There are 89 rows. The repeated statements include:

      • If (parentform! = NULL), 3 places
      • If (expandparentform & parentform! = NULL), 2 places
      • If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right), 4 places
      • If (useanimations), 2 places
    • Animationtimertick (Object sender, system. eventargs e) method, 114 rows, where the repeated statements include
      • If (this. Dock = dockstyle. Left | this. Dock = dockstyle. Right), 2 places
      • If (expandparentform & parentform. windowstate! = Formwindowstate. maximized & parentform! = NULL), at 8
    • Onpaint (painteventargs e) method, 254 rows, where repeated statements include
      • If (hot), 4 places
      • If (this. enabled), 2 places
      • Switch Statement (to judge a parameter), where

The odor is so obvious that it is more necessary for deodorization. Let's start a great deodorization job. What's more, we just said, "it will always have eight pixels wide." This feature is also not good: for people with good eyesight, this split is so big, and it seems so small for people with a little myopia. In this case, we should naturally remove the 8-pixel limit.

Now we have set our targets for deodorization, as shown below:

  1. Remove the nasty, ugly, layer-by-layer, long, smelly switch statements and if statements, which should be put together even if they cannot be completely removed, instead of scattered everywhere;
  2. Remove the repetitive statements that are the same, identical everywhere, and have to be moved;
  3. Shorten these function codes over one screen and split them into multiple methods;
  4. Finally, we hope it will be big when it is big, and it will be small when it is small, rather than always the "8 pixel" width (or high ).

Okay, let's start working.-Let's come back in the next article. during work hours, the article will be written too long by the boss K...

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.