Code fragment-processing of Boolean values in the IF statement separately.

Source: Internet
Author: User

This article is a good reference from the "clean code!

Input a Boolean parameter to the method to determine the logic and execute it separately. Disadvantages:

  • Method signature becomes complicated immediately.
  • This method does more than one thing.

Instance:

    /**     * @param isChecked     */    protected void setAllDayViewsVisibility(boolean isChecked) {        if (isChecked) {            if (mEndTime.hour == 0 && mEndTime.minute == 0) {                mEndTime.monthDay--;                long endMillis = mEndTime.normalize(true);                // Do not allow an event to have an end time                // before the                // start time.                if (mEndTime.before(mStartTime)) {                    mEndTime.set(mStartTime);                    endMillis = mEndTime.normalize(true);                }                setDate(mEndDateButton, endMillis);                setTime(mEndTimeButton, endMillis);            }            mStartTimeButton.setVisibility(View.GONE);            mEndTimeButton.setVisibility(View.GONE);            mTimezoneRow.setVisibility(View.GONE);        } else {            if (mEndTime.hour == 0 && mEndTime.minute == 0) {                mEndTime.monthDay++;                long endMillis = mEndTime.normalize(true);                setDate(mEndDateButton, endMillis);                setTime(mEndTimeButton, endMillis);            }            mStartTimeButton.setVisibility(View.VISIBLE);            mEndTimeButton.setVisibility(View.VISIBLE);            mTimezoneRow.setVisibility(View.VISIBLE);        }        updateHomeTime();    }

After reconstruction, it becomes two methods.

protected void setAllDayViewsGone() {if (mEndTime.hour == 0 && mEndTime.minute == 0) {mEndTime.monthDay--;long endMillis = mEndTime.normalize(true);// Do not allow an event to have an end time// before the// start time.if (mEndTime.before(mStartTime)) {mEndTime.set(mStartTime);endMillis = mEndTime.normalize(true);}setDate(mEndDateButton, endMillis);setTime(mEndTimeButton, endMillis);}mStartTimeButton.setVisibility(View.GONE);mEndTimeButton.setVisibility(View.GONE);mTimezoneRow.setVisibility(View.GONE);updateHomeTime();}protected void setAllDayViewsVisible() {if (mEndTime.hour == 0 && mEndTime.minute == 0) {mEndTime.monthDay++;long endMillis = mEndTime.normalize(true);setDate(mEndDateButton, endMillis);setTime(mEndTimeButton, endMillis);}mStartTimeButton.setVisibility(View.VISIBLE);mEndTimeButton.setVisibility(View.VISIBLE);mTimezoneRow.setVisibility(View.VISIBLE);updateHomeTime();}

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.