Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:37:50+00:00 2026-06-01T04:37:50+00:00

I am trying to improve my coding style and I understand that my coding

  • 0

I am trying to improve my coding style and I understand that my coding style is terrible.
Could you suggest the WAYS of improving my function?

    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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:37:52+00:00Added an answer on June 1, 2026 at 4:37 am

    First of all, stop the empty catch blocks for parsing. If you really want to throw away errors, use TryParse instead. Next, remove unnecessary parentheses. Also cut down on the superfluous comments, like “perform insert operation” (the code makes it obvious). So:

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to improve my Javascript coding style and have been reading that it's
I'm trying to improve my coding style. Consider the following scenario: Suppose that I
I am trying to improve my SQL query so that i could get only
I am coding for various programming olympiads and am trying to improve time efficiency.
I'm trying to do something that is fairly simple, but my code looks terrible
I trying to improve a textbox that filters a gridview, by implementing an enhanced
I am trying improve the fault tolerance in a JSP-page that is updated on
I'm trying to improve my coding ninja h4x skills, and I'm currently looking at
I'm trying to improve a plotting library that I wrote with GtkD (the D
I am trying to improve a script that contains a third-party Delphi tree of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.