Reduce Reliance on Tuples #18

Closed
opened 2025-05-22 02:52:03 -07:00 by VD15 · 2 comments
Collaborator

Various parts of the bot rely on tuples and arrays to pass data between functions:

command, full_user, arguments, note_obj = parsed_command


return [command,full_user, arguments, note_obj]


return None, None, None, None


return chosen['id'], chosen['name'], chosen['file_id'], chosen['rarity']

(This list is not exhaustive)

Occurences of returned tuples and arrays should be replaced with dictionaries. Dictionaries allow us to reference members by name instead of index, allowing for clearer and more maintainable code:

def generate_response(parsed_command):
    command, full_user, arguments, note_obj = parsed_command
    
    if command == "roll":
        user_id = get_or_create_user(full_user)
        character_id, character_name, file_id, rarity = get_character()

        if not character_id:
            return(f"@{full_user} Uwaaa... something went wrong! No characters found. 😿")

        add_pull(user_id, character_id)
        stars = '⭐️' * rarity
        return([f"@{full_user} 🎲 Congrats! You rolled {stars} **{character_name}**\nShe's all yours now~ 💖✨",[file_id]])

Becomes:

def generate_response(parsed_command):
	command = parsed_command["command"]
	full_user = parsed_command["full_user"]
    
    if command == "roll":
        user_id = get_or_create_user(full_user)
        character = get_character()
        character_id = character["id"]

        if not character_id:
            return {
                "message": "@{full_user} Uwaaa... something went wrong! No characters found. 😿"
            }

        add_pull(user_id, character_id)
        stars = '⭐️' * rarity
        return {
            "message": f"@{full_user} 🎲 Congrats! You rolled {stars} **{character["name"]}**\nShe's all yours now~ 💖✨",
            "attachment": character["file_id"]
        }

Notably, accessing data becomes more explicit and allows for adding, removing, or reordering members of the tuple without having to update all references to it. It's now much more clear what is being returned. Dictionaries also support the get() accessor, which allows for default arguments, making

Various parts of the bot rely on tuples and arrays to pass data between functions: https://git.waifuism.life/waifu/kemoverse/src/commit/ff20e2682148b2fa0cfd80c4fb029332185ca1bb/bot/response.py#L34 https://git.waifuism.life/waifu/kemoverse/src/commit/ff20e2682148b2fa0cfd80c4fb029332185ca1bb/bot/parsing.py#L41 https://git.waifuism.life/waifu/kemoverse/src/commit/ff20e2682148b2fa0cfd80c4fb029332185ca1bb/bot/response.py#L14 https://git.waifuism.life/waifu/kemoverse/src/commit/ff20e2682148b2fa0cfd80c4fb029332185ca1bb/bot/response.py#L19 (This list is not exhaustive) Occurences of returned tuples and arrays should be replaced with dictionaries. Dictionaries allow us to reference members by name instead of index, allowing for clearer and more maintainable code: ```py def generate_response(parsed_command): command, full_user, arguments, note_obj = parsed_command if command == "roll": user_id = get_or_create_user(full_user) character_id, character_name, file_id, rarity = get_character() if not character_id: return(f"@{full_user} Uwaaa... something went wrong! No characters found. 😿") add_pull(user_id, character_id) stars = '⭐️' * rarity return([f"@{full_user} 🎲 Congrats! You rolled {stars} **{character_name}**\nShe's all yours now~ 💖✨",[file_id]]) ``` Becomes: ```py def generate_response(parsed_command): command = parsed_command["command"] full_user = parsed_command["full_user"] if command == "roll": user_id = get_or_create_user(full_user) character = get_character() character_id = character["id"] if not character_id: return { "message": "@{full_user} Uwaaa... something went wrong! No characters found. 😿" } add_pull(user_id, character_id) stars = '⭐️' * rarity return { "message": f"@{full_user} 🎲 Congrats! You rolled {stars} **{character["name"]}**\nShe's all yours now~ 💖✨", "attachment": character["file_id"] } ```` Notably, accessing data becomes more explicit and allows for adding, removing, or reordering members of the tuple without having to update all references to it. It's now much more clear what is being returned. Dictionaries also support the `get()` accessor, which allows for default arguments, making
VD15 added the
Refactoring
label 2025-05-22 02:52:03 -07:00
VD15 added the
Feedback Wanted
label 2025-05-22 04:31:23 -07:00
Author
Collaborator

@nai Thoughts on this approach?

It's just my opinion, but tuples tend to rub me the wrong way when I see them.

@nai Thoughts on this approach? It's just my opinion, but tuples tend to rub me the wrong way when I see them.
VD15 added this to the v1.1 project 2025-05-24 14:36:32 -07:00
VD15 added this to the v1.1 milestone 2025-05-26 08:28:17 -07:00
VD15 self-assigned this 2025-05-31 14:42:02 -07:00
Owner

I like the implementation, I actually didn't understood it fully until I was rewriting the pr #42, but especifies a lot of things and makes functions more explicit. Let's go with it absolutely 💯

I like the implementation, I actually didn't understood it fully until I was rewriting the pr #42, but especifies a lot of things and makes functions more explicit. Let's go with it absolutely 💯
waifu closed this issue 2025-06-02 07:34:49 -07:00
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: waifu/kemoverse#18
No description provided.