我的大而丑陋的功能 - 如何改进

时间:2012-03-29 18:26:57

标签: c# optimization coding-style code-cleanup

我正在努力改进我的编码风格,我理解我的编码风格很糟糕。 你能建议改善我的功能的方法吗?

    public void polulateSpecs(int itemID, List<neItem> coll)
    {
        Program p = new Program();

        for (int i = 0; i < (coll.Count - 1); i++) //going over all Objects in the list
        {
            String CatName = coll[i].specCat.Trim(); //define the name of the category
            String queryCategoryCheck = "SELECT ID FROM ATTRIBUTE_CATEGORY WHERE ATTRIBUTE_NAME = '" + CatName + "'"; //check if Cat Exist query
            String queryCategoryInsert = "INSERT INTO ATTRIBUTE_CATEGORY(ATTRIBUTE_NAME) VALUES ('" + CatName + "')"; //insert new category query

            int cdId; //Category ID holder
            try
            {
                String CID = p.querySQLStringReturn(queryCategoryCheck); //get ID
                cdId = int.Parse(CID); //parse ID to number
            }
            catch
            {
                cdId = 0; // if can't parse to number, set to 0
            }

            if (cdId == 0) //if value is 0, then no bran exist, therefore create one
            {
                p.insertSQL(queryCategoryInsert, "Insert New Category " + CatName); //perform insert operation
            }

            try //now Category should be in the database- get it's ID
            {
                String CID = p.querySQLStringReturn(queryCategoryCheck);
                cdId = int.Parse(CID); //Category ID
            }
            catch { }

            for (int c = 2; c < (coll[i].attributesList.Count); c += 2) //go over 
            {
                String attname = coll[i].attributesList[c].Trim(); // name of the attribute
                String attSpec = coll[i].attributesList[c - 1].Trim(); //description of the attribute

                String queryAttributeCheck = "SELECT ID FROM ATTRIBUTE_LIST WHERE ATT_NAME = '" + attname + "' AND ATT_SPEC = '" + attSpec + "'"; //check if Attribute Exist query
                String queryAttributeInsert = "INSERT INTO ATTRIBUTE_LIST(PARENT_CAT, ATT_NAME, ATT_SPEC) VALUES (" + cdId + ", '" + attname + "', '" + attSpec + "')"; //insert new category query


                int aId; //Attribute ID holder
                try
                {
                    String AID = p.querySQLStringReturn(queryAttributeCheck); //get ID
                    aId = int.Parse(AID); //parse ID to number
                }
                catch
                {
                    aId = 0; // if can't parse to number(does not exist), set it to 0
                }

                if (aId == 0) //if value is 0, then no bran exist, therefore create one
                {
                    p.insertSQL(queryAttributeInsert, "Insert New Attribute Set " + attname); //perform insert operation
                }

                try //now Category should be in the database- get it's ID
                {
                    String AID = p.querySQLStringReturn(queryAttributeCheck);
                    aId = int.Parse(AID); //Attribute ID
                }
                catch { }
            }

            //Add final record to database (Item ID and Corresponding Attribute category)
            String queryProductToAttributeCheck = "SELECT ID FROM PRODUCT_TO_ATT_CATEGORY WHERE PROD_ID = '" + itemID + "' AND ATT_CAT = '" + cdId + "'"; //check if Attribute Exist query
            String queryProductToAttributeInsert = "INSERT INTO PRODUCT_TO_ATT_CATEGORY(PROD_ID, ATT_CAT) VALUES (" + itemID + ", " + cdId + ")"; //insert new category query                

            int ptaID; //Attribute ID holder
            try
            {
                String PTAID = p.querySQLStringReturn(queryProductToAttributeCheck); //get ID
                ptaID = int.Parse(PTAID); //parse ID to number
            }
            catch
            {
                // if can't parse to number(does not exist), insert it
                ptaID = 0;
            }
            p.insertSQL(queryProductToAttributeInsert, "Insert Product to Attribute Mapping " + itemID);
        }
    }

1 个答案:

答案 0 :(得分:3)

首先,停止空catch块进行解析。如果您真的想丢掉错误,请改用TryParse。接下来,删除不必要的括号。还减少了多余的注释,如“执行插入操作”(代码使其显而易见)。所以:

public void polulateSpecs(int itemID, List<neItem> coll)
{
    Program p = new Program();

    for (int i = 0; i < coll.Count - 1; i++) // going over all Objects in the list
    {
        string CatName = coll[i].specCat.Trim(); // define the name of the category
        string queryCategoryCheck = "SELECT ID FROM ATTRIBUTE_CATEGORY WHERE ATTRIBUTE_NAME = '" + CatName + "'"; //check if Cat Exist query
        string queryCategoryInsert = "INSERT INTO ATTRIBUTE_CATEGORY(ATTRIBUTE_NAME) VALUES ('" + CatName + "')"; //insert new category query

        int cdId = 0; //Category ID holder
        string CID = p.querySQLStringReturn(queryCategoryCheck); //get ID
        int.TryParse(CID, ref cdId); //parse ID to number

        if (cdId == 0) //if value is 0, then no brand exists, therefore create one:
        {
            p.insertSQL(queryCategoryInsert, "Insert New Category " + CatName);
        }

        // Now Category should be in the database - get its ID
        string CID = p.querySQLStringReturn(queryCategoryCheck);
        int.TryParse(CID, ref cdId); // Category ID

        for (int c = 2; c < coll[i].attributesList.Count; c += 2)
        {
            string attname = coll[i].attributesList[c].Trim(); // Name of the attribute
            string attSpec = coll[i].attributesList[c - 1].Trim(); // Description of the attribute

            string queryAttributeCheck = "SELECT ID FROM ATTRIBUTE_LIST WHERE ATT_NAME = '" + attname + "' AND ATT_SPEC = '" + attSpec + "'"; // Query to check if attribute exists
            string queryAttributeInsert = "INSERT INTO ATTRIBUTE_LIST(PARENT_CAT, ATT_NAME, ATT_SPEC) VALUES (" + cdId + ", '" + attname + "', '" + attSpec + "')"; // Query to insert new category


            int aId = 0; // Attribute ID holder
            string AID = p.querySQLStringReturn(queryAttributeCheck); // get ID
            int.TryParse(AID, ref aId); // parse ID to number

            if (aId == 0) // if value is 0, then no bran exist, therefore create one
            {
                p.insertSQL(queryAttributeInsert, "Insert New Attribute Set " + attname);
            }

            // now Category should be in the database - get its ID
            string AID = p.querySQLStringReturn(queryAttributeCheck);
            int.TryParse(AID, ref aId); // Attribute ID
        }

        // Add final record to database (Item ID and Corresponding Attribute category)
        string queryProductToAttributeCheck = "SELECT ID FROM PRODUCT_TO_ATT_CATEGORY WHERE PROD_ID = '" + itemID + "' AND ATT_CAT = '" + cdId + "'"; // check if Attribute Exist query
        string queryProductToAttributeInsert = "INSERT INTO PRODUCT_TO_ATT_CATEGORY(PROD_ID, ATT_CAT) VALUES (" + itemID + ", " + cdId + ")"; // insert new category query                

        int ptaID = 0; // Attribute ID holder
        string PTAID = p.querySQLStringReturn(queryProductToAttributeCheck); // Get ID
        int.TryParse(PTAID, ref ptaID);
        p.insertSQL(queryProductToAttributeInsert, "Insert Product to Attribute Mapping " + itemID);
    }
}