Delivering Highly Maintainable and Readable Code Using ChatGPT

QASource
QASource | August 3, 2023

Delivering Highly Maintainable and Readable Code Using ChatGPT

ChatGPT, the acclaimed AI language model developed by OpenAI, excels at generating human-like text across domains. However, ChatGPT needs more clarity and readability regarding code generation. This blog explores real coding examples that illustrate these issues and discusses strategies to overcome them. By addressing these concerns, developers can unleash the full potential of intelligent code generation and enhance the quality of their software projects.

Brief About ChatGPT

ChatGPT is built on the GPT-3.5 and GPT-4.0 architectures, generative pre-trained transformer models that can generate code. They have been trained on massive text and code datasets, allowing them to create coherent and contextually relevant responses. The quality and maintainability of their code may vary, so reviewing it carefully before using it in a production environment is essential.

 

Coding Examples of Maintainability and Readability Issues

    • Inadequate Comments in Generated Code: ChatGPT may generate code without providing sufficient comments, making it difficult for developers to understand the purpose or functionality of the code.

      Example:

      // Recruitment based system
      FUNCTION main():
      SET applicant1 = createApplicant("John Doe", "john@example.com", "resume1.pdf", ["Java", "Database", "Communication"])
      SET applicant2 = createApplicant("Jane Smith", "jane@example.com", "resume2.pdf", ["Python", "Machine Learning", "Problem Solving"])
      SET applicant3 = createApplicant("Michael Johnson", "michael@example.com", "resume3.pdf", ["C++", "Algorithms", "Teamwork"])
      shortlistApplicant(applicant1)
      shortlistApplicant(applicant2)
      shortlistApplicant(applicant3)
      scheduleInterview(applicant1)
      scheduleInterview(applicant2)
      hireApplicant(applicant2)
      PRINT "Applicant Status:"
      PRINT "------------------"
      PRINT "Applicant 1:", applicant1.isShortlisted, applicant1.isInterviewed, applicant1.isHired
      PRINT "Applicant 2:", applicant2.isShortlisted, applicant2.isInterviewed, applicant2.isHired
      PRINT "Applicant 3:", applicant3.isShortlisted, applicant3.isInterviewed, applicant3.isHired
      END FUNCTION

      In this example, the lack of comments makes it challenging to comprehend the intention behind the code and its expected behavior.

    • Hardcoding Variable Values: Generated code often relies on hardcoded values, limiting its adaptability and flexibility to different scenarios.

      Example:

      // Online Shopping based system
      FUNCTION placeOrder(cartItems):
      // Hard-coded product prices for demonstration purposes
      SET productPrices = {
      "item1": 25.99,
      "item2": 12.49,
      "item3": 8.75
      }
      // Hard-coded quantities of items in the cart
      SET cartItems = [ {"item": "item1", "quantity": 2}, {"item": "item2", "quantity": 1},
      {"item": "item3", "quantity": 4} ]
      subtotal = calculateSubtotal(cartItems)
      taxRate = 0.15 // Assuming a 15% tax rate
      taxAmount = calculateTax(subtotal, taxRate)
      totalWeight = calculateTotalWeight(cartItems)
      shippingRatePerKg = 2.5 // Assuming a shipping rate of $2.5 per kg
      shippingCost = calculateShippingCost(totalWeight, shippingRatePerKg)
      totalAmount = subtotal + taxAmount + shippingCost
      discountPercent = 10 // 10% discount for promotion (hard-coded)
      discountAmount = applyDiscount(subtotal, discountPercent)
      totalAmount -= discountAmount
      // Display the order details with hard-coded values
      PRINT "Order Summary:"
      FOR EACH item IN cartItems:
      PRINT " -", item.quantity, "x", item.item, "at $", productPrices[item.item], "each"
      PRINT "Subtotal: $", subtotal
      PRINT "Tax: $", taxAmount
      PRINT "Shipping: $", shippingCost
      PRINT "Discount: $", discountAmount
      PRINT "Total: $", totalAmount
      END FUNCTION

      In this case, a hardcoded value (3) restricts the function's applicability to only divisibility by 3 rather than allowing it to handle any divisor.

    • Inappropriate Variable Names: ChatGPT-generated code may use generic or unclear variable names, hindering code comprehension and maintainability.

      Example:

      // Letting based system
      FUNCTION main():
      property1Id = addProperty("123 Main Street", "Apartment", 2, 1500.00)
      property2Id = addProperty("456 Park Avenue", "House", 3, 2000.00)
      listAvailableProperties()
      tenant1 = NEW Tenant
      tenant1.id = GENERATE_UNIQUE_ID()
      tenant1.name = "John Smith"
      tenant1.email = "john@example.com"
      tenant1.occupation = "Software Engineer"
      handleTenantApplication(property1Id, tenant1)
      listAvailableProperties()
      END FUNCTION

      Here, the variable names "length" and "width" are too generic and fail to convey the specific purpose or context of the code.

    • Complex and Convoluted Control Flow: The code generated by ChatGPT can sometimes exhibit unnecessarily intricate control flow, leading to confusion and potential logic errors.

      Example:

      // Function to generate a medical report for a patient
      FUNCTION generateMedicalReport(patientId):
      patient = FIND_PATIENT_BY_ID(patientId) // Assuming a function to find a patient by ID
      IF patient == NULL:
      PRINT "Patient not found."
      RETURN
      ENDIF
      PRINT "Medical Report for patient", patient.name
      PRINT "Age:", patient.age
      PRINT "Gender:", patient.gender
      PRINT "Address:", patient.address
      PRINT "Medical History:", patient.medicalHistory
      PRINT "Allergies:", patient.allergies
      IF patient.appointments.IS_EMPTY():
      PRINT "No appointments found."
      ELSE:
      PRINT "Appointments:"
      FOR EACH appointment IN patient.appointments:
      PRINT " - Date:", appointment.date, "Time:", appointment.time, "Doctor:", appointment.doctorName, "Reason:", appointment.reason
      ENDFOR
      ENDIF
      END FUNCTION

      The convoluted control flow in this example makes it challenging to grasp the logic behind finding the maximum number in the list.

    • Limited Error Handling and Exception Management: The generated code may lack comprehensive error handling and exception management, resulting in unexpected behavior or crashes.

      Example:

      // Function to enroll a student in a course
      FUNCTION enrollStudentInCourse(studentId, courseCode):
      student = FIND_STUDENT_BY_ID(studentId) // Assuming a function to find a student by ID
      IF student == NULL:
      PRINT "Student not found."
      RETURN
      ENDIF
      course = FIND_COURSE_BY_CODE(courseCode) // Assuming a function to find a course by code
      IF course == NULL:
      PRINT "Course not found."
      RETURN
      ENDIF
      IF student.coursesEnrolled.CONTAINS(courseCode):
      PRINT "Student is already enrolled in this course."
      RETURN
      ENDIF
      student.coursesEnrolled.ADD(courseCode)
      PRINT "Student", student.name, "enrolled in course", course.name
      END FUNCTION

      If the divisor (num2) is zero, it will result in a ZeroDivisionError, without proper handling or exception management.

    • Difficulty in Collaborating With Other Developers: ChatGPT-generated code may not adhere to established conventions, making it challenging for other developers to understand, modify, or collaborate effectively.

      // Function to update employee details
      FUNCTION updateEmployeeDetails(employeeId, name, age, department, salary):
      employee = findEmployeeById(employeeId)
      IF employee == NULL:
      PRINT "Employee not found."
      RETURN
      ENDIF
      IF name IS NOT EMPTY:
      employee.name = name
      ENDIF
      IF age > 0:
      employee.age = age
      ENDIF
      IF department IS NOT EMPTY:
      employee.department = department
      ENDIF
      IF salary > 0:
      employee.salary = salary
      ENDIF
      PRINT "Employee details updated successfully."
      END FUNCTION

      In this example, the function lacks proper error handling, and using the "+" operator may not be the most efficient way to merge lists.

    • Absence of Whitespace and Formatting: The lack of proper indentation, spacing, and formatting in the generated code significantly impacts readability and maintainability.

      // Function to borrow a book
      FUNCTION borrowBook(borrowerId, bookId):
      borrower = findBorrowerById(borrowerId)
      IF borrower == NULL:
      PRINT "Borrower not found."
      RETURN
      ENDIF
      book = findBookById(bookId)
      IF book == NULL:
      PRINT "Book not found."
      RETURN
      ENDIF
      IF book.isAvailable == FALSE:
      PRINT "Book is not available for borrowing."
      RETURN
      ENDIF
      borrower.borrowedBooks.ADD(book)
      book.isAvailable = FALSE
      PRINT "Book", book.title, "borrowed by", borrower.name
      END FUNCTION

      Proper indentation and spacing make reading and understanding the code structure easier.

 

Overcoming the Challenges

To overcome the challenges in maintaining the clarity and readability of ChatGPT-generated code, developers can adopt the following strategies:

  • Code Review and Testing: Conduct thorough code reviews to identify bugs, logic errors, and coding mistakes early in the development process. Implement a robust testing process to ensure the correctness and functionality of the generated code.

  • Static Code Analyzers: Utilize static code analysis tools, such as Pylint or SonarQube, to automatically analyze the generated code and identify potential issues, bugs, or violations of coding standards.

  • Incorporate Coding Standards and Best Practices: Establish and enforce coding standards and best practices within the development team. This includes following consistent naming conventions, modularization, and code organization principles.

  • Encourage Documentation and Comments: Promote the inclusion of meaningful comments and documentation in the generated code. Encourage developers to document the code's purpose, expected inputs, outputs, and significant considerations or constraints.

  • Leveraging Code Generation Templates or Libraries: Utilize code generation templates or libraries that follow established best practices and domain-specific patterns. These resources provide a structured approach to generating maintainable and readable code.

  • Continuously Gathering User Feedback and Iterating on the Model’s Outputs: Actively seek feedback from developers who utilize the model's results. Incorporate their input and repeat the generated code to align it better with their needs and expectations.

  • Augmenting GPT With Code-Specific Training Data and Fine-Tuning Techniques: Enhance ChatGPT's training process by incorporating code-specific training data and fine-tuning techniques. This helps the model learn from well-structured, maintainable, and readable code examples.

 

Conclusion

ChatGPT offers a powerful capability for generating code; however, it requires attention to maintainability and readability. By addressing the challenges of inadequate comments, hardcoded values, inappropriate variable names, complex control flow, limited error handling, collaboration difficulties, and formatting issues, developers can unlock the full potential of intelligent code generation. Developers can produce high-quality code aligned with their requirements by implementing code review, static code analyzers, adherence to coding standards, proper documentation, and continuous improvement through user feedback and model training.

To gain deeper insights and unlock the full potential of intelligent code generation, we invite you to download our comprehensive ChatGPT report. By harnessing the power of AI and adhering to best practices, you can elevate the quality of your code and propel your software projects to new heights.

Disclaimer

This publication is for informational purposes only, and nothing contained in it should be considered legal advice. We expressly disclaim any warranty or responsibility for damages arising out of this information and encourage you to consult with legal counsel regarding your specific needs. We do not undertake any duty to update previously posted materials.