1. Blog
  2. Leveraging AI for Coding

Blog

Leveraging AI for Coding

2024-05-06 18:00:00 +0900

Using AI to help you be a more efficient programmer is a bit like Kenny Rogers' classic song "The Gambler": "You've got to know when to hold 'em, know when to fold 'em, know when to walk away, and know when to run." "You've got to know when to hold 'em" in the sense that most of the time the results you will get from large language models (LLMs) for code are surprisingly good, and you can use the results with little modification. You need to "know when to fold 'em" in that you will need to recognize when you need to change your prompts to get the results you want, and we discuss some strategies below. You need to "know when to walk away" in the sense that at the end of the day, these models are only combining code they have seen before and do not generalize logic well, so when the results are not getting better despite trying different prompts, you need to cut your losses and do the work manually. Finally, you need to know "when to run" and not use AI-assisted coding in the first place. If you are trying to use a language the model has not been trained on or a library built after the model's training date, it is more time-efficient not to use it at all. Moreover, if safety and security considerations are paramount and you do not already have the skill to understand when the results may be hallucinations or wrong, you shouldn't use AI-generated code.

WARNING: Although everything written here is up-to-date at the time of writing, models and tools are changing rapidly.

Currently, you can roughly divide up AI-assisted coding tools into two types of tools, Chatbots, and IDE integration.

Chatbots

Chatbots are used for the big picture, to explore ideas, and to set up the development of products. Although they can also be used for the details as well, modifying code is done more efficiently by using an IDE with integrated LLMs for a smoother workflow.

Tool1 Company Multimodal2 Strengths Weaknesses Run code3
ChatGPT OpenAI Paid only Industry Standard and paid versions offer possibly strongest model with API access and Plugins Results do not integrate search information after model date. Free version cannot reach out to internet. No
Gemini Google Yes Integrates Google Search so some info is up-to-date after model training date Yes
Copilot Microsoft Yes Integrates Bing Search so some info is up-to-date after model training date. Handy learn more feature. Cannot reach out to Internet, despite hallucination that it can. No
ClaudeAI Anthropic Upload only More tokens than free version of ChatGPT. Results do not integrate search information after model date and cannot reach out to internet. No
Phind PHIND Paid only Accesses current search information and has very useful source feature for code Paid
  1. All chatbots have free and paid tiers.
  2. Multimodal is the ability to handle images and other media types.
  3. By run code we mean you can ask the chatbot to run code for you.

Chatbot Prompts

Before we start, keep in mind that the results from large language models (LLMs) are very brittle. The results can vary widely due to a minor change in the prompt, using a different LLM, or even an updated version of the same LLM.

Large language models operate by predicting the most likely next word in a sequence, generating text one word at a time. They calculate the probability of each possible word in their vocabulary and choose one based on those probabilities. Some chatbots, such as ChatGPT, have a "temperature" setting that controls the randomness of this selection process: with a low temperature (like 0), the model typically chooses the word with the highest probability, leading to more consistent but potentially repetitive outputs. A higher temperature introduces more randomness, allowing for more varied and creative responses, but at the risk of nonsensical or off-topic outcomes, known as "hallucinations." For programming, where reproducibility is valuable, a low temperature setting is recommended.

You should have an initial template to set the overall context, called the system message. This can be copied and pasted into the prompt (or some services allow it to be set up as Custom Instructions, so it doesn't have to be added each time you restart). Example:

        You are an experienced professional software developer that always uses up-to-date industry best practices. 
Provide correct responses to questions noting any uncertainties. 
I am aware of AI limitations so please skip explanations about your limitations. 
You should prompt me for more information and give suggestions of how to write better prompts so you can better answer as necessary.

      

Prompting Strategies

Keep prompts short and specific, then build on them with multiple prompts to guide the AI to what you are looking for. If necessary, label parts of the prompt, for example: Context, Input, Instruction, Restriction, Output Format.

  • Zero-shot prompting - no examples provided, relies entirely on the model's training.
  • Few-shot prompting - a few examples provided or Q&A examples.
  • Chain prompting - provide a series of intermediate reasoning steps.

Finally, don't forget you can ask the chatbot to format the output as JSON, XML, table, list, markdown, HTML, MathJax, LaTeX, etc.

For a more detailed look try this Prompt Engineering Guide.

Examples

General Examples

        The following code is throwing a NullPointerException when calling Method(). Can you identify the cause and suggest a fix?
How can I make the following code run faster?
Summarize in a structured outline the important changes in the following change log of version 2.1.4: """
{Change Log}
"""

      

Project Example

        Examples for a project to scrape data from Wikipedia.
What are some examples on GitHub to scrape data from a website like Wikipedia?
The following are criteria for an app to scrape data from Wikipedia:
Uses JavaScript and Puppeteer

Generate a project outline
Show the project directory
Show the code for scraper.js
Create a package.json file for this project.


      

Use continue or go on to get around token output limits.

# IDE Integration

IDEs use the context that you have provided via the comments and code in your file and offer suggestions. They allow you to work on details while staying in your dev tooling.

Tool Free Version Integration Supported Languages Strengths Weaknesses
Github Copilot No VS, VS Code, Neovim, JetBrains, CLI, Mobile Many, especially Python, JS, TS, Ruby, Go, C#, C++ Offers most functionality for a reasonable price; more than 50% of the market; bespoke LLM possible Requires paid subscription
Amazon Q Yes VS, VS Code, JetBrains, AWS Management Console Many, especially Java, Python, JS, TS, C#, Go, PHP, Rust, Kotlin, SQL Working with AWS Services
Sourcegraph Cody Yes VS Code, Neovim, JetBrains Many popular languages Supports Claude 3, GPT-4 Turbo, Mixtral-8x7B, Amazon Bedrock, and Azure OpenAI; code graph with Code Search Less user-friendly interface compared to some competitors
Tabnine Yes VS, VS Code, JetBrains, Webstorm, PyCharm, AppCode, Android Studio, Eclipse, CLion, Goland, PHPStorm, RubyMine Many popular languages On-premises, on VPC, or as secure SaaS
Codeium Yes 40+ 70+ Full repo context awareness
Gemini Code Assist No after June 11th VS Code, IntelliJ, JetBrains, Google Cloud Bash, C, C++, C#, Dart, Go, GoogleSQL, Java, JavaScript, Kotlin, Lua, MatLab, PHP, Python, R, Ruby, Rust, Scala, SQL, Swift, TypeScript, YAML Includes Google Cloud-specific content like documentation and sample code Roughly twice the price of Github Pilot
Pieces Yes VS Code, JetBrains, CLI, Sublime, browser, Teams, Obsidian, other integrations Many popular languages depending on your choice of LLM Allows choice of LLM, RAG, and tool integration
Replit Yes Web-based IDE Best with JS and Python, includes Bash, C, C#, C++, CSS, Go, Java, JavaScript, HTML, PHP, Perl, Python, R, Ruby, Rust, SQL Web-based IDE with code completion; good for learners and those not wanting to maintain infrastructure Primarily web-based, may not be ideal for complex projects; free version compute extremely limited

IDE Prompts

Most IDE integration is driven by code comments and code that you write, which the AI assistant then gives a suggestion or multiple suggestions for completion. The most popular AI assistants for IDEs use the [Tab] key to accept the code suggestion or the [Escape] key to skip it. Beyond that, you need to look up, and it is worth the time, the keyboard shortcuts and the functionality for the specific AI coding assistant that you have chosen. Most IDE integrations also support selecting code and then choosing to have it explained, fixed, refactored, optimized, or sent to an integrated chatbot.

Amazon Q screenshot

Examples

General Coding

        # Define a Python class called Employee
# It should have the following attributes:
# - id
# - name
# - birth date
# - department
# - title
# The attributes cannot be None and only department and title can be modified after construction
# However, we should be able to access the attributes using methods

// TypeScript function to parse dates from strings. The function handles both YYYY-MM-DD, MM/DD/YYYY formats and returns a datetime object.

      

Code Quality

        What does this code snippet do? Explain how it works.
Code analysis - What is wrong with this code?
Are there good candidates for extract method refactoring?
How can I refactor this code to make the naming more consistent?
List out any security concerns with this code.
What code smells, if any, does this program contain? List and explain them. … How do I fix …
Write a code review for the code below.

      

There are also specialized tools for code quality, such as Code Climate and Treno.

Minified code can be unminified by using the prompt "fix this code:". The quality of the results will vary. There are also open-source LLMs such as LLM4Decompilespecifically for decompiling binary code to C.

You can use AI tools for many things, such as linting, calculating cyclomatic complexity, Halstead complexity, maintainability index, code coverage, etc. However, as these items have 100% reliable deterministic tools available, use those instead, and you do not have to worry about hallucinations or mistakes.

Data

        For a <small/large> amounts of <numeric, text, real-time, image, geospatial> data, which free database is best?
Make a <sqlite/MariaDB/Postgresql/SQL Server> database schema design for <inventory/blog/web scraping>. It should include tables and relationships between them including primary and foreign keys.
The sql to create those tables
Create the SQL to insert this CSV data ‘0, Babbage, Inventor’ into the user table.
Represent this JSON array: [{‘id’: 1, ‘name’: ‘Turing’, ‘job’: ‘Scientist’}, {‘id’: 2, ‘name’: ‘Bob’,‘job’: ‘Designer’}] in a SQL table format.
Create the sql for 20 rows of demo data for the user table.

      

APIs

Chatbots are useful for both creating and exposing your own APIs as well as finding and consuming other people’s APIs.

        Create a Python CRUD rest API for the people TABLE.
What are some publicly available stock price APIs?
Some example Python code for accessing the Finnhub api to get stock prices.

      

UI

OpenArt and Canva are sites specifically for creating images and professional designs. Teleporthq (React, Vue, Angular, HTML&CSS, or UIDL) and v0 by Vercel (React and Tailwind CSS) are sites specifically for creating website front-ends. Anima and Locofy are plugins for Figma to generate React, Vue, or HTML code to create website front-ends, with Locofy also supporting Adobe XD as a source and Next.js, Gatsby, or React Native as output targets.

        Provide the CSS for a html page to have three columns of text in a newspaper style.
Create an <hero image png\SVG logo> that incorporates the letter S for a technology company.

      

Testing

Note: GitHub Copilot allows you to select the code you want to unit test and then ask Copilot #selection write a unit test for this code. Similarly, for Amazon CodeWhisperer, you can select the code you want to test and then right-click 'Send to Amazon CodeWhisperer' -> 'Send to prompt', and in the Amazon CodeWhisperer chat window, type write a unit test for.

For more sophisticated integration and behavior testing, you may need to write a little or even a lot of the test case code manually before your AI assistant has enough context to be helpful.

        How should I test this program? What would be the most suitable testing framework.
Create a unit test for index.js
Write <unit/integrated/behavior> test cases using XXXX testing framework.
Use a mock object for scraper.js
Simulate user behaviour for this program.

      

Documentation

The AI assistant generated documentation may not be perfect, but it can be much quicker to edit than doing it all from scratch.

  • You can ask your LLM to generate documents such as user manuals, FAQs, README files, API documentation, troubleshooting guides, etc.
  • It can generate UML, Mermaid, C4 models (Context, Containers, Components, Code), and other text diagrams.
  • Use purpose-built software documentation tools such as:
  • Build your own Chatbot with ChatGPT Assistants
        What does this code do?
Generate documentation for this API.

      

Deployment

It may not be the readily apparent, but LLMs can also be helpful for pull requests, virtual machines, cloud infrastructure, and various other aspects of deployment.

        I added real-time streaming prices to the application using sockets. Write a PR description for this.
Create a Dockerfile for this JavaScript app. The main class is index.js. Use the latest Node and install the dependencies using the package.json file in this directory.
Create a Terraform file to provision an AWS EC2 instance of type t2.micro, installs the Docker daemon, and returns the instance's hostname.
Create a GitHub Actions workflow that builds the scraper application and on every merge to the master branch deploys it to EKS.


      

Ongoing Maintenance, Updates, and Migrations

        Are there more up-to-date libraries for package.json
Update the code to Java 21
Translate this Javascript code to Python

      
  • UI designer Uizard
  • Bug reporter JamGPT Chrome extension
  • Security Synk finds and fixes security issues in proprietary code
  • Issue tracker Bugasura like JIRA with built-in AI
  • Issue tracker Tegon open-source, AI-first alternative to Jira, Linear

Books

AI-Assisted Programming (all stages of code creation)
AI-Powered Developer (thorough example of doing a significant project)
Coding with AI for Dummies (looks at a variety of tools)

Future (Agents, etc.)

  • GitHub Copilot Workspace allows high-level specification and planning similar to using the Chats, but it is also integrated into your IDE and can actually make changes in the project files for you. GCW has a nice feature where it discusses a specification of proposed changes it will make before making the changes. Then, after you approve the steps in the plan, it will make the actual changes in your code for you.
  • ChatGPT Plus + LangChain
  • Devin More hype than delivery as of time of writing



Attribution 4.0 International (CC BY 4.0) James Sullivan

This article is licensed under a Creative Commons Attribution 4.0 International license.