Home

Coding with AI For Dummies Cheat Sheet

|
|  Updated:  
2024-03-22 13:56:32
Generative AI For Dummies
Explore Book
Buy On Amazon
Generative AI coding tools can improve your productivity as a coder, remind you about syntax, and even help you with testing, debugging, refactoring, and documentation, but it's up to you to know how to use them correctly. Get ten prompt engineering tips that can make the difference between AI spitting out garbage spaghetti code and crafting elegant code that works.

AI coding tools present unique challenges and hazards for software development teams, so check out some simple rules to make sure that generative AI doesn't tank your project. Then see what happened when ChatGPT was asked to list the top things human coders do that AI can never replace.

Prompting tips for coding with AI

The process of figuring out the right prompts to give generative AI tools to cause them to generate high-quality content is called prompt engineering. In reality, however, designing prompts is more of a creative art than a formal engineering discipline. Using trial and error, people have discovered techniques and phrases that reliably increase the quality of generated code. Here are my top ten tips for effective prompting of generative AI chatbots.

  • Use active voice. Instead of writing, “It is desired to…” write, “The code should…” or “I want…”.
  • Avoid using negations. Instead of writing, “Don’t use loops,” write, “Use recursion instead of loops.”
  • Specify a role. Start your prompt by describing the role the AI should emulate. For example, “You are an experienced and highly skilled programmer.”
  • Emphasize code quality. Use phrases such as “clean code,” “readable,” and “well-documented” in your prompts.
  • Mention specific libraries, frameworks, and APIs. If you expect the generated code to use certain tools, mention those (along with versions if appropriate) in your prompt.
  • Start with an explanation of the overall problem. This gives the AI tools a foundational understanding of the outcome you want.
  • Describe the input and output data formats. Even when it may be obvious to you that a function named sumNumbers() should accept numbers as its arguments and return a number, providing that information in your prompt will limit the number of choices the AI has to make and will improve the quality of its response.
  • Break down complexity. Focus on solving one small chunk of a problem at a time.
  • Be clear and concise. Avoid ambiguity and vague words such as “good” or “efficient.”
  • Work iteratively. Don’t settle for the first generated result. Iterate and refine your prompt and provide feedback to the AI tool to guide it towards a desired result. Sometimes, even changing one word in your prompt can dramatically affect the output.

Potential dangers of AI-generated code

While AI chatbots and pair programmers such as ChatGPT and Copilot can make you a more productive programmer, there are dangers associated with AI-generated code. Here are the worst possible outcomes of using a generative AI tool to write code:

  • Using AI as a substitute for becoming a better programmer. No matter what tool you use, it’s still your responsibility as a programmer to be constantly improving and learning.
  • Deploying AI-generated code without proper code reviews. While generated code may seem to work correctly and be free from obvious errors, it’s still essential to have humans review any generated code before it reaches end users.
  • Use of deprecated functions, libraries, or APIs. The code that AI assistants were trained on likely contains references to outdated and possibly insecure dependencies. Manually verify that everything in the generated code is safe and correct to use.
  • Confusing or long-winded documentation. Generative AI is famous for writing documentation that’s overly verbose and potentially confusing for even the simplest of functions. Having an experienced human writer edit or rewrite generated documentation will save time and frustration in the long run.
  • Related functions that have no relationship. Every human coder has their own style and particular way of doing things. This makes the code written by a person, or even a team of people, coherent and consistent. A generative AI tool doesn’t have a style, and the choices it makes can be radically different from function to function. Provide proper context to minimize inconsistencies.

Things AI Coding Assistants Can't Do

The current crop of AI-powered assistants are tools that can help you be more productive, but at this point they work well only if you have your hands firmly on the steering wheel.  

As AI pair programmers become more sophisticated and are trained on ever larger datasets, chances are good that generative AI tools will master coding in the same way that AI mastered chess and every other board game, card game, and video game. Notably, however, AI chatbots still can’t compete with even the most inexperienced human basketball player. 

Want to find out what will be left for human coders to do once AI leaves us in the dust? ChatGPT was asked to provide a list of things that human coders can do that AI will never be able to do. The following list was generated by ChatGPT and has been edited for style and humorous effect. 

ChatGPT 4: 

Here are some humorous differences between AI coding assistants and human coders: 

  • Misplacing semicolons and then searching for hours: AI coding assistants are precise with syntax, but they’ll never know the “joy” of misplacing a semicolon and spending hours debugging, only to find it hiding in plain sight. 
  • Drinking copious amounts of coffee: While AI can churn out code 24/7, it’ll never experience the quintessential coder ritual of consuming copious amounts of coffee to fuel late-night coding sessions. 
  • Naming variables after pets or favorite snacks: AI might suggest efficient variable names, but it won’t creatively name a variable after a beloved pet or a favorite snack like “fluffyKittenCounter” or “chocolateChipArray”. 
  • Celebrating small victories with a victory dance: Only human coders get to do a little victory dance after finally fixing a bug or getting a complex function to work. 
  • Accidentally creating an infinite loop and crashing their own computer: AI is too logical for this. It won’t know the thrill of accidentally creating an infinite loop, watching everything freeze, and then having to perform the ritual computer reboot. 
  • Getting distracted by social media or cat videos: AI coding assistants stay focused on the task, never falling down the rabbit hole of social media or watching just “one more” cat video before getting back to work. 
  • Using rubber ducks for debugging: AI doesn’t use a rubber duck (or any inanimate object) to explain their code aloud for debugging purposes — a unique human debugging technique. 
  • Experiencing the mystery of code that works but you don’t know why: AI understands why its code works, but human coders sometimes encounter the mystical phenomenon of code that works perfectly without any logical explanation. 
  • Having strong opinions on code editors: AI won’t engage in passionate debates about why Vim is better than Emacs or why spaces are superior to tabs; these are sacred human coder debates. 
  • Writing code comments as if they’re diary entries: AI sticks to factual comments, but human coders sometimes turn code comments into personal diaries, complete with jokes, existential questions, and musings about lunch. 

To learn more about how AI can make your life as a coder easier, get the book.

About This Article

This article is from the book: 

About the book author:

Chris Minnick is an accomplished author, teacher, and programmer. Minnick authored or co-authored over 20 books, including titles in the For Dummies series. He has developed video courses for top online training platforms and he teaches programming and machine learning to professional developers at some of the largest global companies.