Naming Variables Just Got Harder

It's a joke that has been done to death, but it is true. Naming things is one of the hardest aspects of Computer Science. It affects the readability, maintainability, and every facet of the lossy interface between the concept in your mind, and the cold reality of the code. This has been always the way since variables could be named. However, I believe that variable naming has become much more important in the past few months with the increasing presence of "AI" assisted tools.

I was initially stubborn when it came to personally using "AI" powered tools. I still am, but I have now accepted that these tools are here to stay. The cat is out of the bag, Pandora's Box has been opened, etc etc. For better or worse, we should all be operating under the assumption that these tools are currently in use, and will become more relied upon in future. This is why naming in software development has become even more important.

These tools rely, at least partially, on the semantic meaning of your variable and function names. That means if your names are inconsistent or poorly conceived, you're going to get worse output. Some examples in regular code block: (Because I also reject Jupyter Notebooks)

import openai
import os
import sys
import time

from pydantic import BaseModel, Field


openai.api_key = os.getenv("OPENAI_API_KEY")


class GermanTranslationResponse(BaseModel):
    message_in_german: str = Field(
        description="The user message translated into German"
    )

class FrenchTranslationResponse(BaseModel):
    message_in_french: str = Field(
        description="The user message translated into French"
    )

class FrenchTranslationResponse2(BaseModel):
    message_in_german: str = Field(
        description="The user message translated into French"
    )

class FrenchTranslationResponse3(BaseModel):
    message_in_french: str = Field(
        description="The user message translated into German"
    )


def get_response(message: str, language: str, model) -> str:
    context = [{
        "role": "user",
        "content": message
    }]

    chat_completion = openai.ChatCompletion.create(
        model="gpt-3.5-turbo-0613",
        messages=context,
        functions=[
            {
                "name": f"get_{language}_translation",
                "description": f"get the message translated to {language}",
                "parameters": model.schema(),
            },
        ],
        function_call={"name": f"get_{language}_translation"},
        temperature=0.0,
    )

    return chat_completion.choices[0].message.function_call.arguments

if __name__ == "__main__":
    print(f"Input message: {sys.argv[1]}")

    # Translates to German
    print("German")
    print(get_response(sys.argv[1], "German", GermanTranslationResponse))
    time.sleep(1)

    # Translates to French
    print("French")
    print(get_response(sys.argv[1], "French", FrenchTranslationResponse))
    time.sleep(1)

    # Translates to English
    print("French and German")
    print(get_response(sys.argv[1], "French", GermanTranslationResponse))
    time.sleep(1)

    # Translates to English
    print("German and French")
    print(get_response(sys.argv[1], "German", FrenchTranslationResponse))
    time.sleep(1)

    # Translates to German
    print("German and French2")
    print(get_response(sys.argv[1], "German", FrenchTranslationResponse2))
    time.sleep(1)

    # Translates to French
    print("German and French3")
    print(get_response(sys.argv[1], "French", FrenchTranslationResponse3))

$ python3 ex.py "what's up"
Input message: what's up
German
{
  "message_in_german": "Was gibt's Neues?"
}
French
{
  "message_in_french": "Quoi de neuf"
}
French and German
{
  "message_in_german": "what's up"
}
German and French
{
  "message_in_french": "what's up"
}
German and French2
{
  "message_in_german": "Was gibt's Neues?"
}
German and French3
{
  "message_in_french": "Quoi de neuf"
}
Show Comments