6 ChatGPT Coding Prompts to Level Up Your Skills

A short list of ChatGPT prompts and responses for coding

If you write code and haven’t been using ChatGPT then you’re really missing out. It can do so much that it’s like having a junior developer right there next to you. Drop your JIRA ticket right into ChatGPT and code comes out!

Sounds like utopia, right? But of course there are caveats. Currently, ChatGPT is very confident in its answers even if it’s completely wrong (remember that it was trained on human text mostly grabbed from the internet). With coding this can mean we get some really funny results. The most common that I get is a call to a function, or a reference to a library that simply doesn’t exist. Of course, you can always request it create the missing method and that sometimes does work.

Below you’ll find 6 prompts and responses that I’ve found useful in day-to-day programming. My prompts are in C# but of course most any language can be used. And don’t forget the first rule of thumb when writing code in 2023 and beyond: if you were going to Google it, ChatGPT it first!

Prompts and Responses

Parse JSON to a C# model

Prompt:

Create the appropriate C# classes to contain the below JSON object.

{
"projectName": "TODO Application",
"projectDescription": "A simple application to manage daily tasks",
"projectManager": "Jane Doe",
"teamMembers": [
{
"name": "John Smith",
"role": "Developer"
},
{
"name": "Sarah Johnson",
"role": "Developer"
},
{
"name": "Mike Williams",
"role": "Quality Assurance"
}
],
"status": "In progress",
"completionDate": "2023-06-30"
}

Response:

public class Project
{
public string projectName { get; set; }
public string projectDescription { get; set; }
public string projectManager { get; set; }
public List<TeamMember> teamMembers { get; set; }
public string status { get; set; }
public string completionDate { get; set; }
}

public class TeamMember
{
public string name { get; set; }
public string role { get; set; }
}

Implement an interface

Prompt:

Create a C# class ListTodoService which implements the methods in ITodoService below. Items should be persisted in a list.

public interface ITodoService
{
void AddTodoItem(string title, string description, DateTime dueDate);
void MarkTodoItemAsCompleted(int todoItemId);
void DeleteTodoItem(int todoItemId);
List<TodoItem> GetAllTodoItems();
List<TodoItem> GetCompletedTodoItems();
List<TodoItem> GetIncompleteTodoItems();
}

Response:

public class ListTodoService : ITodoService
{
private readonly List<TodoItem> _todoItems;

public ListTodoService()
{
_todoItems = new List<TodoItem>();
}

public void AddTodoItem(string title, string description, DateTime dueDate)
{
var todoItem = new TodoItem
{
Title = title,
Description = description,
DueDate = dueDate,
IsCompleted = false
};

_todoItems.Add(todoItem);
}

public void MarkTodoItemAsCompleted(int todoItemId)
{
var todoItem = _todoItems.FirstOrDefault(item => item.Id == todoItemId);
if (todoItem != null)
{
todoItem.IsCompleted = true;
}
}

public void DeleteTodoItem(int todoItemId)
{
_todoItems.RemoveAll(item => item.Id == todoItemId);
}

public List<TodoItem> GetAllTodoItems()
{
return _todoItems;
}

public List<TodoItem> GetCompletedTodoItems()
{
return _todoItems.Where(item => item.IsCompleted).ToList();
}

public List<TodoItem> GetIncompleteTodoItems()
{
return _todoItems.Where(item => !item.IsCompleted).ToList();
}
}

Create an API over an existing service

Prompt:

Implement a C# API controller which will expose the methods of a service implementing ITodoService.

public interface ITodoService
{
void AddTodoItem(string title, string description, DateTime dueDate);
void MarkTodoItemAsCompleted(int todoItemId);
void DeleteTodoItem(int todoItemId);
List<TodoItem> GetAllTodoItems();
List<TodoItem> GetCompletedTodoItems();
List<TodoItem> GetIncompleteTodoItems();
}

Response:

public class TodoController : ApiController
{
private readonly ITodoService _todoService;

public TodoController(ITodoService todoService)
{
_todoService = todoService;
}

[HttpPost]
public void AddTodoItem(TodoItem item)
{
_todoService.AddTodoItem(item.Title, item.Description, item.DueDate);
}

[HttpPut]
public void MarkTodoItemAsCompleted(int todoItemId)
{
_todoService.MarkTodoItemAsCompleted(todoItemId);
}

[HttpDelete]
public void DeleteTodoItem(int todoItemId)
{
_todoService.DeleteTodoItem(todoItemId);
}

[HttpGet]
public List<TodoItem> GetAllTodoItems()
{
return _todoService.GetAllTodoItems();
}

[HttpGet]
public List<TodoItem> GetCompletedTodoItems()
{
return _todoService.GetCompletedTodoItems();
}

[HttpGet]
public List<TodoItem> GetIncompleteTodoItems()
{
return _todoService.GetIncompleteTodoItems();
}
}

Architect a composable console application for ETL

Prompt:

Describe how to compose a C# console app which can support several ETL flows. Each flow should be easily composable from nodes. Create sample interfaces. Include an implementation of two nodes. Write all explanations as comments.

Response:

using System;
using System.Collections.Generic;

namespace ETLConsoleApp
{
// Interface for a node in an ETL flow
public interface INode
{
// Execute the node's function
void Execute();
}

// Interface for an ETL flow
public interface IFlow
{
// Add a node to the flow
void AddNode(INode node);

// Execute all nodes in the flow
void Execute();
}

// Class for a simple ETL flow
public class SimpleFlow : IFlow
{
// List of nodes in the flow
private List<INode> _nodes;

public SimpleFlow()
{
_nodes = new List<INode>();
}

public void AddNode(INode node)
{
_nodes.Add(node);
}

public void Execute()
{
// Execute all nodes in the flow
foreach (INode node in _nodes)
{
node.Execute();
}
}
}

// Example implementation of a node that multiplies a number by 2
public class MultiplyByTwoNode : INode
{
private int _input;
private int _output;

public MultiplyByTwoNode(int input)
{
_input = input;
}

public void Execute()
{
_output = _input * 2;
Console.WriteLine($"Multiplying {_input} by 2 to get {_output}");
}
}

// Example implementation of a node that adds two numbers
public class AddTwoNumbersNode : INode
{
private int _input1;
private int _input2;
private int _output;

public AddTwoNumbersNode(int input1, int input2)
{
_input1 = input1;
_input2 = input2;
}

public void Execute()
{
_output = _input1 + _input2;
Console.WriteLine($"Adding {_input1} and {_input2} to get {_output}");
}
}

class Program
{
static void Main(string[] args)
{
// Create a new simple flow
IFlow flow = new SimpleFlow();

// Add some nodes to the flow
flow.AddNode(new MultiplyByTwoNode(5));
flow.AddNode(new AddTwoNumbersNode(2, 3));

// Execute the flow
flow.Execute();
}
}
}

Write unit tests

This request can be added on to code that is already in the context of the current chat. But if starting a new chat it can be provided as well.

Prompt:

Create C# unit tests using NUnit for the class provided below:

// Class for a simple ETL flow
public class SimpleFlow : IFlow
{
// List of nodes in the flow
private List<INode> _nodes;

public SimpleFlow()
{
_nodes = new List<INode>();
}

public void AddNode(INode node)
{
_nodes.Add(node);
}

public void Execute()
{
// Execute all nodes in the flow
foreach (INode node in _nodes)
{
node.Execute();
}
}
}

Response:

using NUnit.Framework;

namespace SimpleFlowTests
{
public class SimpleFlowTests
{
[Test]
public void TestAddNode()
{
// Arrange
var flow = new SimpleFlow();
var node = new TestNode();

// Act
flow.AddNode(node);

// Assert
Assert.Contains(node, flow._nodes);
}

[Test]
public void TestExecute()
{
// Arrange
var flow = new SimpleFlow();
var node1 = new TestNode();
var node2 = new TestNode();
flow.AddNode(node1);
flow.AddNode(node2);

// Act
flow.Execute();

// Assert
Assert.AreEqual(2, TestNode.ExecuteCount);
}
}

public class TestNode : INode
{
public static int ExecuteCount = 0;

public void Execute()
{
ExecuteCount++;
}
}
}

Create a list of ideas for sample projects

Prompt:

Give me a list of ideas for sample applications that could be used for programming training material. Each item should indicate what type of application it is.

Response:

  1. A to-do list manager (web application) – This could be a simple application that allows users to add, edit, and delete tasks from a list. It could be built using a combination of HTML, CSS, and JavaScript.
  2. A budget tracker (mobile application) – This could be a mobile app that helps users track their spending and save money. It could be built using a programming language like Java or Kotlin for Android, or Swift for iOS.
  3. A game of tic-tac-toe (desktop application) – This could be a simple game that can be played on a computer. It could be built using a programming language like C# or Python.
  4. A website for a small business (web application) – This could be a simple website for a small business, including pages for the company’s products or services, a contact form, and a homepage. It could be built using a combination of HTML, CSS, and JavaScript.
  5. A social media platform (web application) – This could be a more complex application that allows users to create profiles, connect with friends, share updates and photos, and more. It could be built using a combination of technologies such as HTML, CSS, JavaScript, and a back-end programming language like PHP or Python.
  6. A calculator (mobile application) – This could be a simple mobile app that performs basic math calculations. It could be built using a programming language like Java or Kotlin for Android, or Swift for iOS.
  7. An e-commerce platform (web application) – This could be a more complex application that allows users to browse and purchase products online. It could be built using a combination of technologies such as HTML, CSS, JavaScript, and a back-end programming language like PHP or Python.

Keep Going

When responses get too long ChatGPT cuts them off early. You can just reply with “keep going”, “continue”, or something along those lines to get it to keep going. If it’s a single long method then it may try to restart from the beginning and not be able to complete the response again and again. In this case you can use a prompt like:

keep going, starting with the comment // Use the transform function to transform the data