代码质量好不好怎么评定-为啥说我的代码很好理解

飞一样的编程
飞一样的编程
擅长邻域:Java,MySQL,Linux,nginx,springboot,mongodb,微信小程序,vue

分类: Java 标签: 代码规范 代码健壮性

2022-01-23 17:39:20 1548浏览

好的代码规范和封装的重要性,为啥你的代码被说是垃圾

前言:

为啥你写的代码只是实现了功能而已?扩展起来很费劲?在原来的基础上增加一个功能修改一个功能都很慢呢?你写的代码的为啥被说是垃圾代码?

1.几个重要的概念

1.扩展性
2.底层封装得怎样
3.编写是否规范
4.代码复用性,可读性怎样
5.健壮性
6.胶水代码

2.案例对比

刚入职或没参加工作的朋友写的代码——不规范的代码

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTDMapper3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caiwei.mapper.AccountMapper">

    <resultMap type="com.caiwei.entity.Account" id="AccountMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="acname" column="acname" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
        <result property="acphone" column="acphone" jdbcType="VARCHAR"/>
        <result property="password" column="passWord" jdbcType="VARCHAR"/>
        <result property="code" column="code" jdbcType="VARCHAR"/>
        <result property="goodid" column="goodId" jdbcType="INTEGER"/>
        <result property="userid" column="userId" jdbcType="INTEGER"/>
    </resultMap>
    <insert id="add">
        INSERT  INTO `account`(`id`,`acname`,`roleId`,`acphone`,`passWord`,`code`,`goodId`,`userId`,`picture`,`inviterId`,`comId`,`token`)
        VALUES (NULL,null,6,#{acphone},123456,#{code},NULL,NULL,NULL,#{inviterId},NULL,#{token})
    </insert>
    <insert id="addCs">
        INSERT  INTO `commission`(`id`,`csprice`,`month`,`accountId`) VALUES (NULL,0,0,#{accountId})
    </insert>
    <update id="updMoney" parameterType="com.caiwei.entity.Commission">
        UPDATE `commission`  SET `month`=#{month}
        WHERE `accountId`= #{accountid}
    </update>
    <select id="getUser" resultType="com.caiwei.entity.Account">
        SELECT * FROM `account`
        WHERE `acphone`=#{acphone} AND `passWord`=#{passWord}
    </select>
    <select id="acphone" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM `account` WHERE `acphone`=#{acphone}
    </select>
    <select id="passWord" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM `account` WHERE `passWord`=#{passWord}
    </select>

    <select id="getPhoneUser" resultType="com.caiwei.entity.Account">
         SELECT a.id,a.`acname`,a.`roleId`,a.`acphone`,a.`passWord`,a.`code`,a.`goodId`,a.`userId`,a.`picture`,a.`inviterId`,a.`comId`,
        (SELECT `month` FROM `commission` WHERE `accountId`=1) AS `month`
        FROM `account` AS a WHERE `acphone`=#{acphone}
    </select>
    <select id="code" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM `account` WHERE `code`=#{code}
    </select>
    <select id="month" resultType="java.lang.Integer" parameterType="integer">
        SELECT c.`month` FROM `commission` c  WHERE c.accountId=#{id}
    </select>
    <update id="updateToken" parameterType="com.caiwei.entity.Account">
        UPDATE `account`  SET `token`=#{token}
        WHERE `id`=#{id}
    </update>

    <select id="selectByToken" resultType="com.caiwei.entity.Account" parameterType="string">
        SELECT * FROM `account` WHERE `token`=#{token}
    </select>

</mapper>


有经验的老手写的代码(以本人写的代码为例)——规范的代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="s.jf3q.com.dao.CmNewsMapper">

   <sql id="field"> id,uid,see_num,title,context,cts,cfrom,cdesc,img</sql>
  
    
   <insert id="add" parameterType="s.jf3q.com.model.CmNews" useGeneratedKeys="true" keyProperty="id">
      insert into cm_news (uid,see_num,title,context,cts,cfrom,cdesc,img)
      values(#{uid},#{see_num},#{title},#{context},#{cts},#{cfrom},#{cdesc},#{img})
   </insert>
   
   
   
   <select id="queryList" resultType="s.jf3q.com.model.CmNews" parameterType="s.jf3q.com.model.CmNews">
      select
      <include refid="field"></include>
      from cm_news where 1=1
      <if test="title != null and title != ''">
        and  title  like concat('%',#{title},'%') 
       </if>
        
         
   </select>
    
   
   <select id="queryById" resultType="s.jf3q.com.model.CmNews"
      parameterType="java.lang.Integer">
      select
      <include refid="field"></include>
      from cm_news where id=#{0}
   </select>
   
 
   <update id="update" parameterType="s.jf3q.com.model.CmNews">
      update cm_news
      <trim prefix="set" suffixOverrides=",">
         <if test="uid !=null ">uid=#{uid},</if>
         <if test="see_num !=null ">see_num=#{see_num},</if>
         <if test="title !=null ">title=#{title},</if>
         <if test="context !=null ">context=#{context},</if>
         <if test="cfrom !=null ">cfrom=#{cfrom},</if>
         <if test="cdesc !=null ">cdesc=#{cdesc},</if>
         <if test="img !=null ">img=#{img},</if>
      </trim>
      where   id=#{id}       
   </update>
    
    <delete id="delete" parameterType="java.lang.Integer">
             delete from cm_news where id=#{id}
   </delete>

</mapper>

3.总结

用最少最整洁的代码写出最完善的功能,同样一个功能,你写的需要100行代码才能实现,别人只需要10行实现了同样的效果,那就说明人家的代码比你的好。

4.视频讲解

针对前3点的解说视频,洋葱式的代码(一层一层的)。建议认真看看。

5.好的代码甚至不写注释都能看懂

请看下面的这个代码结构讲解的视频。


好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695