养成良好编码风格的重要性

在编写代码时,养成良好的代码风格,提高代码质量,可以避免很多漏洞,为代码的维护和扩展提高效率。下面以实际工作中遇到的不良编码风格引出的问题为例,说明良好代码风格的作用:

  1. if语句、for语句不管有没有多个语句,一定要使用花括号括起来。下面的这个例子就是因为if语句的body部分存在多个语句,但是没有采用花括号包含在一起,引出的问题:当rn->vinfo0位NULL时,route就会使用上一次的值给entry变量赋值,从而导致数据异常,严重的话会导致进程挂起。
没有使用花括号导致的bug
modify before:
for (rn = ls_table_top (top->rt_network); rn; rn = ls_route_next (rn))
{
    if (rn->vinfo0)
        route = RN_INFO (rn, RNI_DEFAULT);
    if(route && route->selected)
    {
        entry.prefixlen = rn->p->prefixlen;
        entry.prefix = *((PSP_ULONG *)(rn->p->prefix));
        entry.type = route->selected->type;
        entry.flags = route->selected->flags;
        entry.area_id = route->selected->area_id.s_addr;
        entry.cost = OSPF_PATH_COST(route->selected);
        if (CHECK_FLAG(route->selected->flags, OSPF_PATH_TYPE2))
        {
            entry.type2_cost = route->selected->type2_cost;
        }
        else
        {
            entry.type2_cost = 0;
        }

        /* Get the actual nexthop vector.  */
        vec = ospf_path_nexthop_vector (route->selected);
        for (i = 0; i < vector_max (vec); i++)
        {
            if ((nh = vector_slot (vec, i)))
            {
                count++;
                entry.nexthop.flags = nh->flags;
                entry.nexthop.if_id = nh->if_id.s_addr;
                entry.nexthop.nbr_id = nh->nbr_id.s_addr;
                entry.nexthop.id = count;
                entry.nexthop.oi_if_name[0] = '\0';
                strncpy(entry.nexthop.oi_if_name, nh->oi->u.ifp->name,  
                       PSP_INTERFACE_NAMSIZ);
                if(record && fn)
                {
                    (*fn)(tid,record,&entry);
                }			  
            }
        }
        vector_free (vec);
    }
}
modify after:
for (rn = ls_table_top (top->rt_network); rn; rn = ls_route_next (rn))
{
    if (rn->vinfo0)
    {
        route = RN_INFO (rn, RNI_DEFAULT);
        if(route && route->selected)
        {
            entry.prefixlen = rn->p->prefixlen;
            entry.prefix = *((PSP_ULONG *)(rn->p->prefix));
            entry.type = route->selected->type;
            entry.flags = route->selected->flags;
            entry.area_id = route->selected->area_id.s_addr;
            entry.cost = OSPF_PATH_COST(route->selected);
            if (CHECK_FLAG(route->selected->flags, OSPF_PATH_TYPE2))
            {
                entry.type2_cost = route->selected->type2_cost;
            }
            else
            {
                entry.type2_cost = 0;
            }

            /* Get the actual nexthop vector.  */
            vec = ospf_path_nexthop_vector (route->selected);
            for (i = 0; i < vector_max (vec); i++)
            {
                if ((nh = vector_slot (vec, i)))
                {
                    count++;
                    entry.nexthop.flags = nh->flags;
                    entry.nexthop.if_id = nh->if_id.s_addr;
                    entry.nexthop.nbr_id = nh->nbr_id.s_addr;
                    entry.nexthop.id = count;
                    entry.nexthop.oi_if_name[0] = '\0';
                    strncpy(entry.nexthop.oi_if_name, nh->oi->u.ifp->name, PSP_INTERFACE_NAMSIZ);
                    if(record && fn)
                    {
                        (*fn)(tid,record,&entry);
                    }			  
                }
            }
            vector_free (vec);
        }
    }    
}

猜你喜欢

转载自blog.csdn.net/weixin_43722423/article/details/85542308