读书人

小弟我需要培养的编程风格

发布时间: 2012-12-20 09:53:21 作者: rapoo

我需要培养的编程风格

本文分析了jdk源代码String.class 的代码规范.

?

?? 接触java有两年多了,真正的开发也只有不到3个月. 看平台代码才知道,自己的差距还是很远的. 各式各样的风格都有,这是与好的程序员的差距.从今天开始,我要树立自己的代码风格,模板为jdk源代码. 一些基本的规范就不列举了.

?

1.空格

(1) 关于=号, 对变量赋值时,按照jdk源码 = 两端加两个空格,如

int size = original.count;

?(2)对于关系运算符(> < == ..) 两端也加空格

if (offset < 0) {

??(3) 方法后 加空格然后{

  public String() {

?(4)对于多个参数间,参数间逗号后加空格

System.arraycopy(value, 0, v, 0, size);

?(5) if语句中 括号() 外面两侧都加一个空格

if (offset < 0) {

?2 空行

(1)pakage上下各一空行

(2)所有import中没有空行

(3)成员变量间有一空行

(4)连个方法间有一空行

?

3.注释

(1)类注释:写出类的功能, 其它格式见下,如author, 日期格式: MM/DD/YYYY

/** * The <code>String</code> class represents character strings. All * * @author  Lee Boynton * @author  Arthur van Hoff * @version 1.189, 10/21/05 * @see     java.lang.Object#toString() * @see     java.lang.StringBuffer * @see     java.lang.StringBuilder * @see     java.nio.charset.Charset * @since   JDK1.0 */

?(2)成员变量的注释:单行使用/** comment .*/ (?注意标点)

  /** The value is used for character storage. */    private final char value[];

?(3)方法注释: 首行写出方法作用,

?参数行通过@param 参数变量 参数注释.

异常 @exception 异常名 注释

返回? @return 注释

 /**     * Allocates a new <code>String</code> that contains characters      * @param      value    array that is the source of characters.     * @param      offset   the initial offset.     * @param      count    the length.     * @exception  IndexOutOfBoundsException  if the      *        the bounds of the <code>value</code> array.     */    public String(char value[], int offset, int count) {  

?(5)单行注释

//和下面if 竖直对齐

        // Argument is a String        if (cs.equals(this))

?

/** * FrontUserInfoServiceImpl.java V1.0 2010-4-1 上午09:06:00 * * Copyright 2010 Shenzhen **** Information Technology Co.,Ltd. All rights reserved. * * Modification history(By Time Reason): * * Description: */package com.****.service.user.impl;import java.util.ArrayList;import java.util.Date;import java.util.List;import com.****.dao.user.AccumulatePointsDAO;import com.****.dao.user.FrequentFlyerInfoDAO;import com.****.dao.user.PortalUserInfoDAO;import com.****.dao.user.VoucherDAO;import com.****.model.front.user.FrequentFlyerModel;import com.****.model.front.user.FrontUserInfoModel;import com.****.service.user.FrontUserInfoService;import com.****.usermodel.front.user.PasswordDTO;import com.****.usermodel.front.user.PointsSummaryDTO;import com.****.usermodel.front.user.VoucherSummaryDTO;import com.****.utils.GlobalKeys;import com.****.utils.factory.DAOFactory;import com.****.frame.Service.BaseServcie;import com.****.frame.exception.GeneralException;import com.****.util.charater.StringUtil;import com.****.util.collection.CollectionUtil;import com.****.util.encrypt.DesEncrypt;/** * <b>功能描述:</b>用户信息Service操作实现类 * * @author :****(Kevin.xie)<br> * * <b>修改历史:</b>(修改人,修改时间,修改原因/内容)<br> */public class FrontUserInfoServiceImpl extends BaseServcie implements FrontUserInfoService { /** * 常旅客-人员DAO */ private FrequentFlyerInfoDAO frequenterDAO; /** * 会员积分DAO */ private AccumulatePointsDAO accumulatePointsDAO; /** * 优惠券DAO */ private VoucherDAO voucherDAO; /** * @author :****(Kevin.xie) * * @param userName 用户名 * @return 用户信息对象 * * <br> * (non-Javadoc) * @see com.****.service.user.FrontUserInfoService#getUserInfoByUserName(java.lang.String) */ @Override public FrequentFlyerModel getUserInfoByUserName(String userName) { getFrequentFlyerInfoDAO(); FrequentFlyerModel userInfoModel = null; // 1、获取指定用户的用户信息 List<FrequentFlyerModel> list = frequenterDAO.getFrequentersListByUserName(userName, true); // 2、如果有记录,必然是只有条记录 if (!CollectionUtil.listIsNullOrEmpty(list)) { userInfoModel = list.get(0); } return userInfoModel; } /** * @author :****(Kevin.xie) * * @param frequenterModel 要修改的用户信息模型 * @param userName 用户名 * @return 包含用户基本信息、积分信息和优惠券等信息的集合 * * <br> * (non-Javadoc) * @see com.****.service.user.FrontUserInfoService#modifyUserInfo * (com.****.model.front.user.FrequentFlyerModel, * java.lang.String) */ @Override public List<Object> modifyUserInfo(FrequentFlyerModel frequenterModel, String userName) { // 1、修改用户信息 getFrequentFlyerInfoDAO(); frequenterDAO.modifyFrequenter(frequenterModel, userName); // 2、获取用户的详细信息 return getDetailsUserInfo(userName); } /** * * @author :****(Kevin.xie) * * @param userName 用名 * @return 包含用户的用户信息,积分信息和优惠券信息的集合 * * <br> * (non-Javadoc) * @see com.****.service.user.FrontUserInfoService#getDetailsUserInfo(java.lang.String) */ public List<Object> getDetailsUserInfo(String userName) { List<Object> retList = null; if (StringUtil.isNEIgnoreWhiteSpace(userName)) { return retList; } // 1、个人信息 FrequentFlyerModel frequentFlyer = null; retList = new ArrayList<Object>(); getFrequentFlyerInfoDAO(); List<FrequentFlyerModel> frequenterList = frequenterDAO.getFrequentersListByUserName( userName, true); if (!CollectionUtil.listIsNullOrEmpty(frequenterList)) { frequentFlyer = frequenterList.get(0); } // 2、可用积分、最近一年分和总历史积分信息对象 getAccumulatePointsDAO(); PointsSummaryDTO pointsSummary = accumulatePointsDAO.getPointsSummary(userName); // 3、优惠券列表信息 getVoucherDAO(); List<VoucherSummaryDTO> voucherList = voucherDAO.getVoucherSummary(userName); // 4、组装数据返回 retList.add(frequentFlyer); retList.add(pointsSummary); retList.add(voucherList); return retList; } /** * @author :****(Kevin.xie) * * @param passwordDTO 包含账户密码信息对象 * @return 用户的详细信息,包括用户的用户信息,积分信息和优惠券信息 * * <br> * (non-Javadoc) * @see com.****.service.user.FrontUserInfoService#changePassword * (com.****.usermodel.front.user.PasswordDTO) */ @Override public List<Object> changePassword(PasswordDTO passwordDTO) { if (passwordDTO == null) { throw new GeneralException("1000001", "更新密码的数据丢失"); } // 1、判断用户是否存在或密码是否正确 String oldPasswordEncrypt = DesEncrypt.imEncrypt(passwordDTO.getOldPassword()); PortalUserInfoDAO userInfoDAO = DAOFactory.getPortalUserInfoDAO(); FrontUserInfoModel userInfoModel = userInfoDAO.portalLogon(passwordDTO.getUserName(), oldPasswordEncrypt); if (userInfoModel == null) { throw new GeneralException("1000001", "密码或用户名不匹配"); } // 2、设置修改数据 String encryptPwd = DesEncrypt.imEncrypt(passwordDTO.getNewPassword()); userInfoModel.setPassword(encryptPwd); userInfoModel.setModificator(GlobalKeys.TAL_MODIFICATOR_FIELD); userInfoModel.setMoDate(new Date()); userInfoModel.setMoUnit(GlobalKeys.TAL_MO_UNIT_FIELD); // 3、更新数据库 userInfoDAO.modifyUserInfo(userInfoModel, passwordDTO.getUserName()); // 4、获取用户的详细信息 return getDetailsUserInfo(passwordDTO.getUserName()); } /** * * <b>功能描述:</b>获取FrequentFlyerInfoDAO的实例 * * @author :****(Kevin.xie) <br> * <b>创建日期 :</b>2010-3-22 下午09:14:38 * * * <br> * <b>修改历史:</b>(修改人,修改时间,修改原因/内容)<br> */ private void getFrequentFlyerInfoDAO() { frequenterDAO = DAOFactory.getFrequentFlyerInfoDAO(); } /** * * <b>功能描述:</b>获取AccumulatePointsDAO的实例 * * @author :****(Kevin.xie) <br> * <b>创建日期 :</b>2010-3-31 上午10:45:47 * * * <br> * <b>修改历史:</b>(修改人,修改时间,修改原因/内容)<br> */ private void getAccumulatePointsDAO() { accumulatePointsDAO = DAOFactory.getAccumulatePointsDAO(); } /** * * <b>功能描述:</b>获取VoucherDAO的实例 * * @author :****(Kevin.xie) <br> * <b>创建日期 :</b>2010-3-31 下午02:48:14 * * * <br> * <b>修改历史:</b>(修改人,修改时间,修改原因/内容)<br> */ private void getVoucherDAO() { voucherDAO = DAOFactory.getVoucherDAO(); }} 50 楼 抢街饭 2011-02-18 编程风格并不同于其他的style,目的并不是形成自己独树一帜的风格,而是为了让你的程序易读,有利于团队合作以及别人帮你改错。

51 楼 mfkvfn 2011-02-18 果然是新手帖子。

你说的那些都只要Ctrl+Shift+F格式化一下就可以,也不一定一成不变,不同的公司或团队都有自己的代码规范。别人设置好了导出成xml给你你导入一下,然后写代码后Ctrl+shift+F就行了(当然也可以设置成保存时自动格式化代码)。

读书人网 >编程

热点推荐