B轮融资|代码注释的艺术,优秀代码真的不需要注释吗?( 四 )


母语的力量
其实上述例子 , 我们更易阅读 , 还有一个重要的原因 , 那就是母语的力量 。 我们天然所经历的环境与我们每天所接触到的事物 , 让我们对中文与英文有完全不一样的感受 。 我们代码的编写本质上是一个将我们沟通中的“中文问题” , 翻译成“英文代码”来实现的过程 。 而阅读代码的人在做得 , 是一件将“英文代码”翻译成“中文表述”的事情 。 而这之中经过的环节越多 , 意思变味越严重 。
TaskDispatch taskDispatch = TaskDispatchBuilder.newBuilder().withExceptionIgnore().build();taskDispatch // 外贸信息 .join(new FillForeignTradeInfoTask(targetCustomer sourceInfo)) // 国民经济行业、电商平台、注册资本 .join(new FillCustOutterInfoTask(targetCustomer sourceInfo)) // 客户信息 .join(new FillCustomerOriginAndCategoryTask(targetCustomer sourceInfo)) // 客户扩展信息 .join(new FillCustExtInfoTask(targetCustomer sourceInfo)) // 收藏屏蔽信息 .join(new FillCollectStatusInfoTask(targetCustomer sourceInfo loginDTO())) // 详情页跳转需要的标签信息 .join(new FillTagInstanceTask(targetCustomer sourceInfo loginDTO())) // 客户信息完整度分数 .join(new FillCustomerScoreTask(targetCustomer sourceInfo)) // 潜客分层完整度 .join(new FillCustomerSegmentationTask(targetCustomer sourceInfo)) // 填充操作信息 .join(new FillOperationStatusTask(targetCustomer sourceInfo loginDTO)) // 认证状态 .join(new FillAvStatusTask(targetCustomer loginDTO)) // 客户地址和组织 .join(new FillCompanyAddressTask(targetCustomer loginDTO)) // 违规信息 .join(new FillPunishInfoTask(targetCustomer sourceInfo)) // 填充客户黑名单信息 .join(new FillCustomerBlackStatusTask(targetCustomer sourceInfo)) // 填充客户意愿度 .join(new FillCustIntentionLevelTask(targetCustomer sourceInfo)); // 执行 .execute(); 这是一段补齐客户全数据信息的代码 , 虽然每一个英文我们都看得懂 , 但我们永远只会第一眼去看注释 , 就因为它是中文 。 并且也因为有这些注释 , 这里非常复杂的业务逻辑 , 我们同样可以非常清晰的了解到它做了哪些 , 分哪几步 , 如果要优化应该如何处理 。 这里也建议大家写中文注释 , 注释是一种说明 , 越直观越好 。
注释的真正归属 复杂的业务逻辑
// Fail if we're already creating this bean instance:// We're assumably within a circular reference.if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName);// Check if bean definition exists in this factory.BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null!containsBeanDefinition(beanName)) { // Not found -check parent. String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. return parentBeanFactory.getBean(nameToLookup args);else { // No args -delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup requiredType);这是Spring中的一段获取bean的代码 , spring作为容器管理 , 获取bean的逻辑也非常复杂 。 对于复杂的业务场景 , 配上必要的注释说明 , 可以更好的理解相应的业务场景与实现逻辑 。截取自: org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean
晦涩的算法公式
/** * Returns the value obtained by reversing the order of the bits in the * two's complement binary representation of the specified {@code long * value. */public static long reverse(long i) { // HD Figure 7-1 i = (i0x5555555555555555L)1 | (i1)0x5555555555555555L; i = (i0x3333333333333333L)2 | (i2)0x3333333333333333L; i = (i0x0f0f0f0f0f0f0f0fL)4 | (i4)0x0f0f0f0f0f0f0f0fL; i = (i0x00ff00ff00ff00ffL)8 | (i8)0x00ff00ff00ff00ffL; i = (i48) | ((i0xffff0000L)16) | ((i16)0xffff0000L) | (i48); return i; 这是JDK中Long类中的一个方法 , 为reverse方法添加了足够多的注释 。 对于几乎没有改动且使用频繁的底层代码 , 性能的优先级会高于可读性 。 在保证高效的同时 , 注释帮助我们弥补了可读性的短板 。截取自java.lang.Long#reverse