FS > M.A.Y.B.E.

Marc's Assays, Yearnings, and Badger Encounters 6-sided die showing the number 6

< back

A quick way to get more creative coding control with ChatGPT

Tuesday May 30, 2023

Want more control over the implementation of your code idea by ChatGPT? Want your time spent with ChatGPT to be a bit more interesting, or possibly inspiring?

If so, try using a more interactive, prospective design prompt which sources a variety of ideas, like this example (my emphasis in bold):

Write a bash script with three functions: Allow a user to select how many dice to roll, roll the dice, and announce the results graphically. For each function, allow me to pick from three different ways of creating the function.

The resulting code may then look something like this:

#!/bin/bash

# Function 1: Allowing the user to select how many dice to roll

# Option 1: Read input from user
select_dice_count_1() {
    read -p "Enter the number of dice to roll: " dice_count
    echo $dice_count
}

# Option 2: Using command-line arguments
select_dice_count_2() {
    dice_count=$1
    echo $dice_count
}

# Option 3: Using a default value
select_dice_count_3() {
    dice_count=1
    echo $dice_count
}

# Function 2: Rolling the dice

# Option 1: Using the $RANDOM variable
roll_dice_1() {
    echo $((RANDOM % 6 + 1))
}

# Option 2: Using the shuf command
roll_dice_2() {
    shuf -i 1-6 -n 1
}

# Option 3: Using an array to represent the dice faces
roll_dice_3() {
    dice_faces=("1" "2" "3" "4" "5" "6")
    index=$((RANDOM % 6))
    echo "${dice_faces[$index]}"
}

# Function 3: Announcing the results graphically

# Option 1: Using a for loop and printf
announce_results_1() {
    for ((i = 1; i <= $1; i++)); do
        printf "[Dice $i] - $2\n"
    done
}

# Option 2: Using echo and a separator line
announce_results_2() {
    for ((i = 1; i <= $1; i++)); do
        echo "Dice $i: $2"
    done
    echo "-----------------"
}

# Option 3: Using ASCII art for graphical representation
announce_results_3() {
    for ((i = 1; i <= $1; i++)); do
        echo "╔═══╗"
        echo "║ $2 ║"
        echo "╚═══╝"
    done
}

# Main script

# Selecting dice count
dice_count=$(select_dice_count_1)  # Change the function name to select_dice_count_2 or select_dice_count_3 for different options

# Rolling the dice
results=""
for ((i = 1; i <= dice_count; i++)); do
    result=$(roll_dice_1)  # Change the function name to roll_dice_2 or roll_dice_3 for different options
    results+=" $result"
done

# Announcing the results
announce_results_1 $dice_count "$results"  # Change the function name to announce_results_2 or announce_results_3 for different options

Then, simply alter the function names as needed (these places in the code were helpfully indicated by ChatGPT in comments).

Did you notice that these are now actually software-design questions you’re faced with, not simply “how to code that” questions?

This highlights the possibility of using ChatGPT for designing while coding, not just for simple coding.

Don’t stop there

With the creative juices starting to flow, you can, and should, also continue to ask for more creative, rare, funny, or obscure ideas. For example, this follow-up prompt:

Give 3 more creative options for the display of the result.

…produced the code to display 1) ASCII dice with pips instead of numbers, 2) ANSI-colored dice graphics, and 3) emoji dice.

And you can keep going further—I asked ChatGPT for three even more creative options, and it then offered a way to play a sound effect of dice rolling, and two ways of animating the resulting dice rolls.

I was surprised to see the sound effect option, and it got me thinking—does this need to be a boring script, or could some more creativity be really fun and interesting?

Adding flexibility to your design process is worth it

This simple hack gives you more direct, authorship-style control over the initial style, feel, and functionality of your script or program.

This is helpful because you can end up developing a superior outcome more quickly, by reviewing a variety of different design choices as soon as possible.

Also, down the road it will become harder (mostly in terms of your own working memory & patience, etc.) to change things, so a design process that is more interactive in this way will generally work in your favor.

Note that you’ll want to change the approach depending on the style of scripting or programming you’re using (if something else other than functional). Be sure to take advantage of the additional prompt length that ChatGPT can work with, to specify details about the type of code you want to see.

(Or possibly, to prompt ChatGPT to offer ideas as to different types of code you might want to see!)

Use it for non-code things as well

As a bonus, this tip also works when you are asking ChatGPT for non-code-related tips and other queries.

For example, if asking for tips on a given topic, you can add something like:

Present the tips in three different styles, only presenting the first tip of each style at first. Allow me to choose which style I like best before continuing with the rest of the tips.

The resulting output can prime your intuition to think more clearly about the way you may end up using the information. This means engaging your conceptualizing skills in co-design with ChatGPT, for a better result.

YOU will feel more creative as a result. You should end up feeling at least a bit less like a copy-paste author or coder, which can be a reassuring benefit for the newcomer.

Conclusion & some final thoughts on breadth

Not only can it be fun to see where things go with this kind of a prompt, but in my opinion this little hack can give you a better idea of the broad capabilities of ChatGPT, as opposed to its singular, deep capabilities.

Most people will err on the side of deep assistance when asking for coding help, aiming for one really good, single result. But getting your code some more breadth exposure throughout the process can be vital to getting an even better outcome.

Filed in: Technology /41/ | Intuition /62/ | Productivity /119/ | Ni /42/ | Ne /17/

Own your procrastination with Whole Productivity, a new system → Get my free INTJ COVID-19 Guide → Explore your gifts with my INTJ Workbook → Other Publications → ...and the fake word of the hour: "Lurpating." Which I believe is a term used when speaking about overcooked food.

 ·