Code fragment-reduces the number of nested layers and ends the returned results in advance.

Source: Internet
Author: User

This article is a good reference from the art of writing readable code!

This article mainly introduces two ways to solve the problem ,,

  • Reduces the number of nested layers. The nested layers undoubtedly increase the complexity of the Code.
  • Return as early as possible, and process the judgment that can be processed as early as possible. Later corrections will make the code clearer.

For example, before reconstruction

public synchronized Drawable getAvatar(Context context, Drawable defaultValue) {    if (mAvatar == null) {        if (mAvatarData != null) {            Bitmap b = BitmapFactory.decodeByteArray(mAvatarData, 0, mAvatarData.length);            mAvatar = new BitmapDrawable(context.getResources(), b);        }    }    return mAvatar != null ? mAvatar : defaultValue;}

The code above is difficult to see clearly at a glance, and the use of nesting increases the complexity of code reading.

After reconstruction:

public synchronized Drawable getAvatar2(Context context, Drawable defaultValue) {if (mAvatar != null) {return mAvatar;}if (mAvatarData == null) {return defaultValue;}Bitmap b = BitmapFactory.decodeByteArray(mAvatarData, 0, mAvatarData.length);mAvatar = new BitmapDrawable(context.getResources(), b);if (mAvatar == null) {return defaultValue;}return mAvatar;}

In addition, for the return in if else, there is no sense to perform else processing. If the conditions are met, You can directly use if. As follows:

private Contact getContactInfo(Contact c) {    if (c.mIsMe) {        return getContactInfoForSelf();    } else if (Mms.isEmailAddress(c.mNumber) || isAlphaNumber(c.mNumber)) {        return getContactInfoForEmailAddress(c.mNumber);    } else {        return getContactInfoForPhoneNumber(c.mNumber);    }}

After reconstruction:

private Contact getContactInfo(Contact c) {if (c.mIsMe) {return getContactInfoForSelf();}if (Mms.isEmailAddress(c.mNumber) || isAlphaNumber(c.mNumber)) {return getContactInfoForEmailAddress(c.mNumber);}return getContactInfoForPhoneNumber(c.mNumber);}

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.