← Writing

Novel ChatGPT coding hacks

As artificial intelligence continues to grow in capability, developers are finding new ways to integrate AI into their workflows. ChatGPT, a powerful language model by OpenAI, offers a range of potential applications for software development. In this article, we will explore four ways ChatGPT can assist with coding, increasing efficiency, and improving the overall development process.

1. Request output as a shell script to create source files and their content

Generate a shell script to create a Python project with a main.py that converts a given dollar value into the top ten other currencies

Sample Output

#!/bin/bash

# Create project folder
mkdir currency_converter
cd currency_converter

# Create main.py file
cat > main.py << 'EOF'
import requests

API_KEY = 'YOUR_API_KEY'

def get_exchange_rates(api_key):
url = f'https://openexchangerates.org/api/latest.json?app_id={api_key}'
response = requests.get(url)
return response.json()['rates']

def convert_usd_to_top_ten_currencies(usd_amount, rates):
top_currencies = ['EUR', 'JPY', 'GBP', 'AUD', 'CAD', 'CHF', 'CNY', 'SEK', 'NZD', 'MXN']
converted_values = {}

for currency in top_currencies:
if currency in rates:
converted_values[currency] = round(usd_amount * rates[currency], 2)

return converted_values

def main():
usd_amount = float(input("Enter the amount in USD: "))
rates = get_exchange_rates(API_KEY)
converted_values = convert_usd_to_top_ten_currencies(usd_amount, rates)

for currency, amount in converted_values.items():
print(f"{usd_amount} USD in {currency}: {amount}")

if __name__ == '__main__':
main()
EOF

# Inform the user
echo "The Python project has been created with a main.py file."
echo "Please replace 'YOUR_API_KEY' in main.py with your API key from https://openexchangerates.org/"

Save the script as create_project.sh and run it in your terminal using bash create_project.sh. This will create the currency_converter folder with a main.py file inside. Replace ‘YOUR_API_KEY’ in main.py with your API key from https://openexchangerates.org/ and run the script with python main.py. The script will prompt you to enter a dollar amount and then display the conversion to the top ten currencies.

2. Optimize existing project layouts using ChatGPT

First, create a list of all the files in your project in a terminal with a command like ‘find .’ (Linux/Mac), dir -s (Windows). Then paste the output after the prompt below.

Write a shell script that can optimise the file structure according to best practices.

Paste file structure here

Sample output

#!/bin/bash

# Move to the project root directory
cd com/localhostlimited/memeinstants

# Create directories for activities, fragments, and adapters
mkdir activities fragments adapters

# Move MainActivity.kt and MainApplication.kt into activities
mv MainActivity.kt MainApplication.kt activities

# Move HomeFragment.kt into fragments
mv ui/fragments/HomeFragment.kt fragments

# Move adapters into adapters directory
mv ui/adapters/* adapters

# Remove empty directories
rmdir ui/fragments ui/adapters ui

# Inform the user
echo "The file structure has been optimized according to best practices."

3. Request a code review of existing files

Suggest changes to the file below to improve readability, and reliability and match best practices. Changes should be provided as example snippets.
Source Code:

PASTE SOURCE CODE FILE HERE

Sample Output

Here are some suggested changes to improve readability, reliability, and match best practices:

  1. Extract the hard-coded color values into the colors.xml file for easier management.
  2. Break down the long onCreate() method into smaller, more manageable functions.
  3. Use string resources instead of hardcoded strings.
  4. Use camel case for naming view IDs in the XML files and reference them consistently in the code.

The prompt then provided examples of the exact code changes that were required in the relevant files.

4. You can ask ChatGPT to provide general guidance on your development style and request feedback (if you have provided sufficient examples of your work in a given chat thread)

Based on the fact that the source code I provided was written by me, provide feedback on my code style as bullets: what am I doing well, what I can work on to improve.

Sample Output

Here’s a review of your code style based on the provided code snippets:

What you’re doing well:

  1. Consistent indentation and formatting: Your code is well-formatted and consistently indented, making it easier to read and understand.
  2. Proper use of Android-specific components: You have demonstrated an understanding of Android components such as Activities, Custom Views, and Layouts.
  3. Usage of dependency injection: You have tried to use Hilt for dependency injection, which is a good practice to manage dependencies and make the code more modular and testable.

What you can work on to improve:

  1. Consider using more meaningful variable and function names: Some variable names like dm in your React Native code are not very descriptive. Using more descriptive names makes your code more readable and maintainable.
  2. Code modularity: Break down complex functions into smaller, more manageable functions, each with a single responsibility. This would make the code more maintainable and easier to understand.
  3. Kotlin idiomatic code: Consider leveraging Kotlin-specific features, such as extension functions, data classes, and scope functions, to write more idiomatic and concise Kotlin code.

By addressing these areas, your code will be more readable, maintainable, and easier to work with.

5. Generate a prompt structure to break down features into testable chunks

Provide a ChatGPT prompt structure for building out an economyservice in a kotlin android native app.
it will need to have the following features:
- player earns coins when they perform an action
- the economy service should be an injectable Hilt singleton dependency
- you should be able to query the current balance from the service
- the balance should be persisted (saved and loaded) between sessions
- player should be able to buy things using the balance

The prompts should support integrating each feature in order, so they can be tested and delivered independently.

Sample Output

Here’s a series of prompts to help you build out the `EconomyService` in a Kotlin Android native app using Hilt:

1. Create a basic EconomyService structure with Hilt:

Help me create a basic structure for an EconomyService in a Kotlin Android app using Hilt as a singleton dependency.

2. Implement a function to earn coins by performing actions:

Show me how to implement a function in the EconomyService that allows the player to earn coins when they perform an action.

3. Add a function to query the current balance from the service:

Help me add a function to the EconomyService that allows querying the current balance of the player's coins.

4. Implement persistence for the balance between sessions:

Show me how to implement persistence for the player's coin balance in the EconomyService so it's saved and loaded between sessions.

5. Add a function to enable buying things using the balance:

Help me add a function to the EconomyService that allows the player to buy items using their coin balance.

These prompts are structured in a way that allows you to integrate each feature independently and test them as they are added. You can use these prompts one by one, allowing ChatGPT to guide you through the implementation process. You can also ask it to add individual test cases or design an overall integration test of the acceptance criteria.

Conclusion

The future of software development is rapidly changing, with AI tools like ChatGPT playing a larger role in optimizing the coding process. As these tools become more prevalent, developers will benefit from streamlined workflows, faster development times, and improved code quality. This shift has the potential to revolutionize developer education and reshape the way we approach software development.