开发工程师如何写好一个方法的业务逻辑

这里记录一下和刚进入it行业的新生沟通的关于开发工程师如何写好一个方法的业务逻辑的几条 言简意赅 公司规范

1. 方法或方法对应的接口必须有完备的注释

方法是给别的方法或组件,甚至为外部接口调用,因此编写的方法最起码要标注每一个参数什么意思,该方法是干什么的。
【源码范例】

    /**
     * Creates a new <code>SOAPElement</code> object initialized with the
     * given <code>Name</code> object and adds the new element to this
     * <code>SOAPElement</code> object.
     * <P>
     * This method may be deprecated in a future release of SAAJ in favor of
     * addChildElement(javax.xml.namespace.QName)
     *
     * @param name a <code>Name</code> object with the XML name for the
     *        new element
     *
     * @return the new <code>SOAPElement</code> object that was created
     * @exception SOAPException if there is an error in creating the
     *                          <code>SOAPElement</code> object
     * @see SOAPElement#addChildElement(javax.xml.namespace.QName)
     */
    public SOAPElement addChildElement(Name name) throws SOAPException;

2. 方法名称命名规范: 动作类单词 + 具体业务名称, 单词不易过长

void loadMyBusinessInfo(String userName,String tenantCode)

4. 考虑好是否一定要返回值! 首选不用任何返回值的方法

5. 考虑好方法参数是否为空验证

对于一般controller 层调用Service层方法,参数对象一般已经做了非空处理无需重复验证,但方法注释一定写明白改参数必须不为空!

6. 业务逻辑先写出要做的事情的注释,然后写代码

一定要在脑子里面过一遍具体的业务流程,写出这些流程注释!

public void  loadMyBusinessInfo(String userName,String tenantCode){
	
	// 用户一般性验证(存在、唯一等)
	
	// 查询用户信息
	
	// 查询用户历史操作记录信息
	
	// 记录查询操作记录日志
	
	// 发送邮件提醒
}

6. 方法一定尽量做到职责单一 !

除主调用方法除外不建议一个方法承担过多的业务逻辑
【源码范例】可以将 refresh()主调用方法里面业务逻辑拆分成 职责单一的小业务逻辑方法。
每一个方法都有简要的注释说明。

	@Override
  public void refresh() throws BeansException, IllegalStateException {
  	synchronized (this.startupShutdownMonitor) {
  		// Prepare this context for refreshing.
  		prepareRefresh();

  		// Tell the subclass to refresh the internal bean factory.
  		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

  		// Prepare the bean factory for use in this context.
  		prepareBeanFactory(beanFactory);

  		try {
  			// Allows post-processing of the bean factory in context subclasses.
  			postProcessBeanFactory(beanFactory);

  			// Invoke factory processors registered as beans in the context.
  			invokeBeanFactoryPostProcessors(beanFactory);

  			// Register bean processors that intercept bean creation.
  			registerBeanPostProcessors(beanFactory);

  			// Initialize message source for this context.
  			initMessageSource();

  			// Initialize event multicaster for this context.
  			initApplicationEventMulticaster();

  			// Initialize other special beans in specific context subclasses.
  			onRefresh();

  			// Check for listener beans and register them.
  			registerListeners();

  			// Instantiate all remaining (non-lazy-init) singletons.
  			finishBeanFactoryInitialization(beanFactory);

  			// Last step: publish corresponding event.
  			finishRefresh();
  		}catch (BeansException ex) {
  			if (logger.isWarnEnabled()) {
  				logger.warn("Exception encountered during context initialization - " +
  						"cancelling refresh attempt: " + ex);
  			}

  			// Destroy already created singletons to avoid dangling resources.
  			destroyBeans();

  			// Reset 'active' flag.
  			cancelRefresh(ex);

  			// Propagate exception to caller.
  			throw ex;
  		}finally {
  			// Reset common introspection caches in Spring's core, since we
  			// might not ever need metadata for singleton beans anymore...
  			resetCommonCaches();
  		}
  	}
  }

7. 一定要避免过多的 if判断嵌套

void loadMyBusinessInfo(String userName,String tenantCode){
       if(判断1){
           if(判断2){
               
           }
           if(判断3){
               
           }else {
               
           }
       }else {
           if(判断4){
               
           }
       }
}

将一些判断类业务逻辑优先处理,避免过多if嵌套


 void loadMyBusinessInfo(String userName,String tenantCode) {
   if(){ 
   	  //  判断不通过 1
   	  return;
   	}
   	
   	if(){ 
   	  //  判断不通过 2
   	  return;
   	}
   	
   	if(){ 
   	  //  判断不通过 3
   	  return;
   	}
   	
   	// 判断逻辑之外的其他具体操作业务逻辑
   	if(){
   	  // 更新表2
   	}else {
   	  // 更新表1
   	}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值