Leveraging AI for Coding
AI Generated Picture

Leveraging AI for Coding

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. The latest and complete version of this article can be found at https://www.solutions.asia/menu/portfolio/blog/leverage-ai-for-coding

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.

Article content

  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.

Article content

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, ...


continues at https://www.solutions.asia/menu/portfolio/blog/leverage-ai-for-coding#ide-prompts




To view or add a comment, sign in

More articles by James Sullivan

Others also viewed

Explore content categories