代码是如何越写越烂的?
你是否经常听同事自嘲,“开始还想好好写,不知怎滴,后面越写越烂”?
代码越写越烂,果真是个没有端倪、无法干预的魔咒玄学吗?
让我们来快速浏览一下 重构前 项目里的代码是怎么写的。 protected void initView() { PagerAdapter pagerAdapter = new PagerAdapter(); viewPagerFix.setOffscreenPageLimit(4); viewPagerFix.setAdapter(pagerAdapter); mFragmentBinding.tabLayout.setTabData(pagerAdapter.titles); mFragmentBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() { @Override public void onTabSelect(int position) { viewPagerFix.setCurrentItem(position); } @Override public void onTabReselect(int position) { } }); viewPagerFix.addOnPageChangeListener(new ViewPagerFix.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { KeyboardUtils.hideSoftInput(getActivity()); } @Override public void onPageSelected(int position) { mFragmentBinding.tabLayout.setCurrentTab(position); if (mViewModel.getXXXDetailTouchManager().isZZBG()) { zzbgPageSelected(position); } else if (mViewModel.getXXXDetailTouchManager().isYBJZ()) { ... } else { ... } } @Override public void onPageScrollStateChanged(int state) { } }); viewPagerFix.setCurrentItem(0); mFragmentBinding.headContainer.getTitleView().setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mViewModel.getXXXDetailTouchManager().isZZBG()) { return; } mViewModel.changeWyhcrwMajorState(); EventBus.getDefault().post(new RefreshItemEventBus( mViewModel.getXXXDetailTouchManager().getCurrentWyhcrw())); } }); } private void zzbgPageSelected(int position) { if (mScreenNum == 3) { switch (position) { case 0: case 1: mViewModel.removeAllArrows(); if (mAttachmentFragment != null) { mAttachmentFragment.hideClickHighLight(ALBUM_ALL); } break; case 2: mViewModel.showAllArrows(); break; default: break; } } else { ... } ; } /** * viewPager适配器 */ private class PagerAdapter extends FragmentPagerAdapter { String[] titles; PagerAdapter() { super(getChildFragmentManager()); if (mViewModel.getXXXDetailTouchManager().isZZBG()) { if (mScreenNum == 3) { titles = getResources().getStringArray(R.array.XXX_detail_tabs_for_no_tbjt); } else { titles = getResources().getStringArray(R.array.XXX_detail_tabs_for_zzbg); } } else if (mViewModel.getXXXDetailTouchManager().isYBJZ()) { titles = getResources().getStringArray(R.array.XXX_detail_tabs_for_ybjz); } else { titles = getResources().getStringArray(R.array.XXX_detail_tabs); } } @Override public Fragment getItem(int position) { if (mViewModel.getXXXDetailTouchManager().isZZBG()) { return zzbgGetItem(position); } else if (mViewModel.getXXXDetailTouchManager().isYBJZ()) { switch (position) { case 0: if (mXXXTuBanPicFragment == null) { mXXXTuBanPicFragment = XXXTuBanPicFragment.newInstance( mViewModel.getUniqueCode(), mViewModel.getXXXTouchManger() ); } return mXXXTuBanPicFragment; case 1: if (mRecordFragment == null) { mRecordFragment = XXXRecordFragment.newInstance(mViewModel.getXXXDetailTouchManager()); } return mRecordFragment; default: if (mAttachmentFragment == null) { mAttachmentFragment = XXXAttachmentFragment.newInstance( mViewModel.getAttachments(), mViewModel.getOriginalAttachments(), mViewModel.getUniqueCode(), mViewModel.getXXXTouchManger(), XXXDetailFragment.this ); } return mAttachmentFragment; } } else { ... } } private Fragment zzbgGetItem(int position) { if (mScreenNum == 3) { switch (position) { case 0: if (mAttributeFragment == null) { mAttributeFragment = XXXAttributeFragment.newInstance( mViewModel.getUniqueCode(), mViewModel.getXXXTouchManger() ); } return mAttributeFragment; case 1: if (mRecordFragment == null) { mRecordFragment = XXXRecordFragment.newInstance( mViewModel.getXXXDetailTouchManager()); } return mRecordFragment; default: if (mAttachmentFragment == null) { mAttachmentFragment = XXXAttachmentFragment.newInstance( mViewModel.getAttachments(), mViewModel.getOriginalAttachments(), mViewModel.getUniqueCode(), mViewModel.getXXXTouchManger(), XXXDetailFragment.this ); } return mAttachmentFragment; } } else { .... } } @Override public Object instantiateItem(ViewGroup container, int position) { Object object = super.instantiateItem(container, position); if (mViewModel.getXXXDetailTouchManager().isZZBG()) { if (mScreenNum == 3) { switch (position) { case 0: mAttributeFragment = (XXXAttributeFragment) object; break; case 1: mRecordFragment = (XXXRecordFragment) object; break; default: mAttachmentFragment = (XXXAttachmentFragment) object; break; } } else { switch (position) { case 0: mRecordFragment = (XXXRecordFragment) object; break; default: mAttachmentFragment = (XXXAttachmentFragment) object; break; } } return object; } else if (mViewModel.getXXXDetailTouchManager().isYBJZ()) { ... } else { switch (position) { case 0: mXXXTuBanPicFragment = (XXXTuBanPicFragment) object; break; case 1: mAttributeFragment = (XXXAttributeFragment) object; break; case 2: mRecordFragment = (XXXRecordFragment) object; break; default: mAttachmentFragment = (XXXAttachmentFragment) object; break; } return object; } } @Override public int getCount() { if (mViewModel != null) { if (mViewModel.getXXXDetailTouchManager().isZZBG()) { if (mScreenNum == 3) { return 3; } return 2; } if (mViewModel.getXXXDetailTouchManager().isYBJZ()) { return 3; } else { return 4; } } return 0; } }
(为保护隐私,模块类名已替换为“XXX”)
可以看到,该主页目前服务于 3 个地区,每个地区对子页面的展示都有定制需求。
if else switch if else switch,只在乎功能实现的码农就是这么写的。