1. 程式人生 > >養成良好編碼風格的重要性

養成良好編碼風格的重要性

在編寫程式碼時,養成良好的程式碼風格,提高程式碼質量,可以避免很多漏洞,為程式碼的維護和擴充套件提高效率。下面以實際工作中遇到的不良編碼風格引出的問題為例,說明良好程式碼風格的作用:

  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);
        }
    }    
}