Skip to content

Instantly share code, notes, and snippets.

@MatthewCallis
Last active March 14, 2024 17:59
Show Gist options
  • Save MatthewCallis/17d669b5a5efa7f1c023aa7e0a1108eb to your computer and use it in GitHub Desktop.
Save MatthewCallis/17d669b5a5efa7f1c023aa7e0a1108eb to your computer and use it in GitHub Desktop.
Using AI to help write better code

AI & Coding

Let's just ask it.

๐Ÿง”๐Ÿปโ€โ™‚๏ธ You: How can software engineers make the best use of generative AI technologies?

๐Ÿค– ChatGPT: To make the best use of generative AI technologies, software engineers can:

  1. Automate repetitive tasks: Use AI to generate code snippets, documentation, or automate testing and debugging.
  2. Enhance creativity: Leverage AI for design, brainstorming new ideas, or solving complex problems.
  3. Improve user experiences: Implement AI in applications for personalized content generation or interactive chatbots.
  4. Stay updated: Continuously learn about new AI tools and frameworks to stay ahead in the tech space.
  5. Collaborate and share knowledge: Use AI to facilitate knowledge sharing within teams and improve collaboration on projects.

Thanks for coming to my TED talk. ๐Ÿ˜‚


Good Use Cases (High Confidence)

  • Adding tests to existing code ๐Ÿ”ฌ
    • You should already know what it does or will be doing ๐Ÿ“š
    • You should know how to test it, let it provide the bulk of the tests ๐Ÿฆพ
    • Rarely has it ever generated tests that just work for me on the first try ๐Ÿ’

  • Super Auto Complete ๐Ÿฆน๐Ÿฝโ€โ™€๏ธ
    • Completions can be as short as a filename, or as complex as a common array reduce operations โ™ป๏ธ
    • The longer the completions go on, the worse the AI output gets in terms of the original ask โŒ›๏ธ

  • Optimizing existing code or looking for flaws โค๏ธโ€๐Ÿฉน
    • Ask it to improve your code in some way, like make it DRYer, optimize for speed or reduce loops ๐Ÿšฎ

  • Typed code will be able to be verified more easily than untyped code ๐Ÿงพ
    • Best used with Copilot as it will have the context of the workspace ๐Ÿฉป

Maybe / Maybe Not Use Cases (Medium Confidence)

  • Getting unstuck by getting an explination of code, reverse rubber ducky-ing ๐Ÿฆ†
    • If you need to understand the code quickly, asking the AI to explain it can be helpful ๐Ÿง‘๐Ÿฟโ€๐Ÿซ

  • Finding the missing knowledge ๐Ÿ•ต๐Ÿปโ€โ™‚๏ธ
    • When you know a concept exists but not how to execute it, getting the missing keywords to search for can be a huge time saver ๐Ÿ”‘

  • Language Translation: "I know this in Python, but not TypeScript" โ˜Ž๏ธ
    • This tends to work best when the type of languages are similar ๐Ÿ‘ฏโ€โ™€๏ธ
      • Rust to JavaScript tends to be worse than Python to JavaScript ๐Ÿ

  • PII & Secrets shouldn't be used with anything in the cloud ๐Ÿคซ
    • You should already know what it does or will be doing ๐Ÿ“š
    • If you have something you're unsure of, use a local LLM with Ollama ๐Ÿฆ™

Bad Use Cases (Low Confidence)

  • Trusting the output without understanding the input ๐Ÿ“ฅ
    • ๐Ÿšจ Dangerous: Getting working code without understanding it and releasing it to others who trust you

  • Long code will burn through the context tokens and result in truncated or bad suggestions ๐Ÿฅต

  • Reliance on the AI to do the logic for you leaves you with a lack of understanding of both the problem and the solution ๐Ÿ˜ถโ€๐ŸŒซ๏ธ

  • Lacking domain knowledge will result in bad suggestions ๐Ÿฅด

  • Not understanding the concepts well enough to know when it is halucinating realistic looking problems ๐Ÿ„
    • ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿซ Example: Mike using the Faker API with realistic looking methods that he knows aren't real

Tips & Tricks

  • Understanding core programing concepts is a hard requirement, you will need to know what you are asking for and be able to validate it ๐Ÿง 

  • "Explain it like I'm 5" is a good way to get the AI to explain a concept to you quickly before you dive into the details ๐Ÿฃ

  • Ask for the "Most Important Concepts" of a topic to start digging into the new domain ๐Ÿ†
    • ๐Ÿง”๐Ÿปโ€โ™‚๏ธ You: "What are the most important concepts of working with a vector database?"

  • Tell the AI to return code only to save on context tokens ๐Ÿ’ธ

  • High complexity will lead to worse suggestions ๐Ÿ˜ตโ€๐Ÿ’ซ; break the problem down into smaller parts ๐Ÿงฌ

GitHub Copilot

  • Write a good comment for the function you want to write
  • Use @workspace to give it the context of the project

// Q: What is the most efficent way to find the largest number in an array?
// A: You can use the Math.max function to find the largest number in an array
const arr = [1, 2, 3, 4, 5];
const largest = Math.max(...arr);
console.log(largest);
  • Install VSCode Copilot extenions if you want to follow along



Example of talking through an idea

๐Ÿ’ฌ ASK: Can you please write for me a reusable FFMPEG script that will take take in an audio file and a video file and compress it into an Instagram optimized MP4 video file with its audio stripped, that is cropped to the length of the audio file, and optionally zooms in on the video to fit vertically and will loop the video if it isn't as long as the audio

๐Ÿชฒ BUG: This appears to loop even if the video is more than long enough, we should only loop the video if necessary

โœจ FEAT: Fantastic, can we add some debugging for each step like outputting the lengths of the audio and video, if it needs trimmed or if it needs looped

๐Ÿชฒ BUG: Testing the new script, it did not output a video with the audio of the provided file, it kept the audio of the provided video

โœจ FEAT: Lets also update the script to optimize for 1080 by 1920 in resolution for Instagram stories

โœจ FEAT: Amazing work! ...


                            tl;dr Allow it assist you, not guide you. ๐Ÿฆค
                                ใ€€ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€ใ€€ใ€€
                                ใ€€ใ€€ใ€€ใ€€๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€ใ€€
                                ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€
                                ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€
                                ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€
                                ใ€€ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ
                                ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ
                                ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€๐ŸŸฆ๐ŸŸฆ
                                ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€๐ŸŸฆ
                                ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€๐ŸŸฆ๐ŸŸฆ
                                ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ
                                ใ€€ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ
                                ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€
                                ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€
                                ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€
                                ใ€€ใ€€ใ€€ใ€€๐ŸŸฆใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€ใ€€
                                ใ€€ใ€€ใ€€ใ€€ใ€€ใ€€๐ŸŸฆ๐ŸŸฆ๐ŸŸฆใ€€ใ€€ใ€€ใ€€ใ€€
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment