Use ChatGPT to check software bugs?


In my previous blog Why you need CodeGuru, I mentioned that how I used CodeGuru to detect a high severity bug in a piece of Python code. Just out of curiosity, I am wondering what ChatGPT is going to tell me about the same code.

This the Python Code:

def get_child_ous(root_or_ou_id: str) -> list:
    """Get child ous for parent ou recursively"""
    child_ous = []
    orgs_client = boto3.client("organizations")
    try:
        response = orgs_client.list_organizational_units_for_parent(
            ParentId=root_or_ou_id
        )
        child_ous.extend(response["OrganizationalUnits"])
        while "NextToken" in response:
            response = orgs_client.list_organizational_units_for_parent(
                ParentId=root_or_ou_id, NextToken=response["NextToken"]
            )
            child_ous.extend(response["OrganizationalUnits"])
        for child_ou in child_ous:
            print(f"child_ou name is {child_ou['Name']}")
            child_ous.extend(get_child_ous(child_ou["Id"]))
    except ClientError as error:
        print(error)
    return child_ous

Here is what ChatGPI thinks:

The code looks generally correct, but there are a few things you may want to consider for improvement:

1. Recursion: The function recursively calls itself to get the child OUs, which can lead to a stack overflow if the hierarchy is too deep. Consider using an iterative approach to avoid this issue.

2. Error handling: The ClientError exception is caught, but it is not clear what action is taken when this occurs. Consider logging the error message or returning an error status to the caller to provide more information about what went wrong.

3. Print statement: The print statement inside the loop is not necessary as it only serves to print information to the console. Consider removing this line or replacing it with a more appropriate action such as logging the information.

4. Return type: The function returns a list, but it is not clear what the expected format of this list should be. Consider adding a docstring to explain the expected format of the returned list.

5. Naming: The function name "get_child_ous" can be improved to be more descriptive, such as "get_child_organizational_units".

Well, there are some good suggestions on improvement, but it missed the critical bug “Error prone sequence modification“. Even with that, you can see it is already smart as it understands the context (I did not provide the codes at the first place).

I have no doubt that it will give a better answer next time given it keeps evolving with more trainings and users’ feedbacks.

Leave a comment