C# 我又大又丑的功能-如何改进

C# 我又大又丑的功能-如何改进,c#,optimization,coding-style,code-cleanup,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

我正在努力改进我的编码风格,我知道我的编码风格很糟糕。 你能提出改善我机能的方法吗

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

首先,停止空的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);
    }
}

可能更适合将来参考,这类问题更适合。你为什么要吞下那么多例外?你使用试捕的方法肯定会有很大的改进。例如,您应该捕获更具体的异常类型。如果您还没有,我建议安装的试用版并尝试一下。它为您提供了如何在编写代码时改进代码的建议。不要以字符串形式生成和执行SQL查询,请使用参数化查询。