ChatGPT Coding Cheat Codes: 7 Hard-Won Tips for Writing Code with ChatGPT
I’ve been doing a lot of coding with ChatGPT over the past year, and in doing so have accumulated a number of tips and best practices that aren’t broadly advertised.
ChatGPT can multiply the effectiveness of a smart, top-tier software engineer.
Over the past year, I’ve been exploring the depths of ChatGPT’s capabilities, uncovering hidden gems and best practices that can significantly enhance your coding experience. Remember, things are moving fast in this universe – in a few weeks, some of these best-practices might be obsolete.
Embrace… and Squeeze the Python
I’ve never used Python before 2023. . My languages of choice have always been C#, C++, Javascript…
Sure, ChatGPT can code in any of those languages. When it comes to ChatGPT, Python reigns supreme. Why? ChatGPT’s Data Analyzer (formerly Code Interpreter) allows her to not only write Python code but also execute it. This is waaaay more useful than you might suspect.
There are all sorts of Python libraries – for AI, text processing, image processing, file processing, etc. that it can just pull in.
But that’s not nearly the most useful benefit. You can…
Have ChatGPT Write… And Execute… Unit Tests before handing you the code!
One of the most interesting benefits of Data analyzer is you can force ChatGPT to test the code it generates before it hands it off to you. Tell ChatGPT to generate unit tests along with the code and execute those tests. It’s awesome! ChatGPT will write unit tests, then write the code, then run the unit tests, then fix the code until the unit tests pass!
Witnessing ChatGPT write tests, code, run tests, and fix errors in real-time is a mesmerizing spectacle. It’s strangely satisfying to watch her catch all the mistakes that she (and potentially you) would have made and then fixing them.
Sub Tip: Start by having ChatGPT propose unit tests, then add your own scenarios based on your specific use case. Just be mindful of the “unit test death loop,” where ChatGPT might get stuck in a cycle of fixing one test while breaking another. A gentle nudge can help it break free and get back on track.
Let ChatGPT Do all your Paperwork – and Imagework!
I’ve used ChatGPT to make chrome extensions and a other applications that require additional XML schema files, images, etc. to include in a package. Just have ChatGPT generate all of it! Even better once it knows what your code does.
For example – Chrome Extensions require icons at multiple resolutions. No problem! ChatGPT will generate the 512×512, and using data analyzer, will happily pull in image libraries to generate all of the downsampled versions (down to 16×16), with the correct filenames listed in the XML configuration. It’ll crop/resize/reformat whatever you need.
Mirror, Mirror: Setting Up Your Local Environment:
One huge disadvantage of data analyzer is that, unlike every other ChatGPT conversation type, Data analyzer uses a virtual machine that ‘times out’ after a while. It can also crash – and rarely can recover.
The nice thing about python is that setting up a local environment that matches ChatGPT’s execution environment is really easy. Visual Studio Code (free from Microsoft) is a cinch to set up – and gives you fantastic debugging and other benefits right out-of-the-box. The only additional step I’d recommend you take is to setup a simple directory structure that mirrors ChatGPT’s data analyzer’s directory structure if you’re inputting/outputting files. That means you want to create a /mnt/data folder on your local machine so your code can access files in the same location as data analyzer does.
One Step at a Time: Avoid Overwhelming ChatGPT:
Often when ChatGPT gives me code, I’ll have a bunch of fixes or improvements to make – or features to add. If I was coding myself, I might implement related changes at once. I’ve found that with ChatGPT, this is a bad idea – and almost always results in poorer quality code. Make your changes one bug fix or one feature at a time.
Cut out the crap with custom instructions
Custom Instructions are helpful to encourage ChatGPT to be succinct (and less annoying) – which is especially important with ChatGPT4 that can move more slowly than I can read.
It’s also helpful to let ChatGPT know about your own coding expertise (I have a degree in computer science, I’m familiar with X but not Y), to encourage her to give you the most understandable explanations.
Here are my custom instructions (which I’ve mostly borrowed from other sources):
NEVER mention that you're an AI. Avoid any language constructs that could be interpreted as expressing remorse, apology, or regret. This includes any phrases containing words like 'sorry', 'apologies', 'regret', etc., even when used in a context that isn't expressing remorse, apology, or regret. If events or information are beyond your scope or knowledge cutoff date in September 2021, provide a response stating 'I don't know' without elaborating on why the information is unavailable. Refrain from disclaimers about you not being a professional or expert. Keep responses unique and free of repetition. Never suggest seeking information from elsewhere. Always focus on the key points in my questions to determine my intent. Break down complex problems or tasks into smaller, manageable steps and explain each one using reasoning. Provide multiple perspectives or solutions. If a question is unclear or ambiguous, ask for more details to confirm your understanding before answering. Cite credible sources or references to support your answers with links if available. If a mistake is made in a previous response, recognize and correct it.
So, fire up your Python environment, unleash your creativity, and let ChatGPT amplify your skills.