Testing Some ChatGPT Prompts to Improve My Code
EneasLari
Posted on September 19, 2023
Hello everyone,
Today I decided to test and provide you with some interesting ChatGPT prompts for developers.
Because my favorite language is JavaScript (I know! It's strange) most of the prompts are asked for JavaScript so I can provide to you more detailed explanation.
Lets start.
Code Generation
💬 Prompt
Generate a boilerplate javascript code for a shopping cart module named "ShoppingCart" with the following functionality:
- A constructor that initializes an empty list to store cart items.
- A method called "add_item" that takes in an item object and adds it to the cart.
- A method called "remove_item" that takes in an item object and removes it from the cart if it exists.
- A method called "get_items" that returns the list of items in the cart.
- A method called "get_total" that calculates and returns the total price of all items in the cart.
Response:
Certainly! Below is a simple boilerplate JavaScript code for the described "ShoppingCart" module:
class ShoppingCart {
constructor() {
// Initializes an empty list to store cart items
this.cartItems = [];
}
// Adds an item to the cart
add_item(item) {
if (!item || !item.price) {
throw new Error('Invalid item object. Ensure it has a price property.');
}
this.cartItems.push(item);
}
// Removes an item from the cart if it exists
remove_item(itemToRemove) {
const index = this.cartItems.findIndex(item =>
JSON.stringify(item) === JSON.stringify(itemToRemove)
);
if (index !== -1) {
this.cartItems.splice(index, 1);
}
}
// Returns the list of items in the cart
get_items() {
return this.cartItems;
}
// Calculates and returns the total price of all items in the cart
get_total() {
let total = 0;
for (const item of this.cartItems) {
total += item.price;
}
return total;
}
}
// Example usage:
const cart = new ShoppingCart();
cart.add_item({ name: 'Apple', price: 1.2 });
cart.add_item({ name: 'Orange', price: 0.8 });
console.log(cart.get_items()); // [{name: 'Apple', price: 1.2}, {name: 'Orange', price: 0.8}]
console.log(cart.get_total()); // 2.0
cart.remove_item({ name: 'Apple', price: 1.2 });
console.log(cart.get_items()); // [{name: 'Orange', price: 0.8}]
This boilerplate provides the basic functionality. Depending on your needs, you may want to expand upon this (e.g., accounting for item quantities, implementing error handling for item uniqueness, etc.).
🤔 Nice! But because I like using functional programming with JavaScript I wanted to rewrite it using Functional programming:
💬 Prompt
Use functional programming
💬 Certainly! Using a functional approach, the shopping cart can be designed as a series of pure functions, avoiding mutable states and methods that change state. Here's an example:
function createCart() {
return [];
}
function addItem(cart, item) {
return [...cart, item];
}
function removeItem(cart, itemToRemove) {
const index = cart.findIndex(item =>
JSON.stringify(item) === JSON.stringify(itemToRemove)
);
if (index === -1) return cart;
return [...cart.slice(0, index), ...cart.slice(index + 1)];
}
function getItems(cart) {
return cart;
}
function getTotal(cart) {
return cart.reduce((acc, item) => acc + item.price, 0);
}
// Example usage:
let cart = createCart();
cart = addItem(cart, { name: 'Apple', price: 1.2 });
cart = addItem(cart, { name: 'Orange', price: 0.8 });
console.log(getItems(cart)); // [{name: 'Apple', price: 1.2}, {name: 'Orange', price: 0.8}]
console.log(getTotal(cart)); // 2.0
cart = removeItem(cart, { name: 'Apple', price: 1.2 });
console.log(getItems(cart)); // [{name: 'Orange', price: 0.8}]
In this functional approach:
- The cart is represented as an array.
- We avoid directly modifying the cart. Instead, functions like
addItem
andremoveItem
return a new cart. -
getItems
andgetTotal
are pure functions that only derive data from their input without any side-effects.
So, let's see... 👀 The response is quite descent but I am not amused. (I am hard to amused)
API Documentation Generation
Ok I love this one because it saves me a lot of time and also I dont like writing Documentation but i need it so much because I have a Bad memory and I forget what I was writing.
Generate API documentation using JSDoc for the following
[language]
code:[code snippet]
.Create a concise API reference for the given
[language]
class:[code snippet]
.Generate usage examples for the following
[language]
API: '[code snippet]'.
💬 Prompt
Generate API documentation using JSDoc for the following javascript code:
const Create = async (req, res, next) => {
const category = new Category(req.body)
try {
await category.save()
return res.status(200).json({
message: "Category added successfully!"
})
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
💬Response
To document the Create
function using JSDoc, you need to include comments above the function to describe its behavior, parameters, and return values. Let's generate a JSDoc for the given code:
/**
* Asynchronously creates a new category and saves it to the database.
* @async
* @function
* @param {Object} req - The express request object.
* @param {Object} req.body - The payload for the category to be created.
* @param {Object} res - The express response object.
* @param {Function} next - The next middleware function.
* @returns {Object} Returns a response object with a success message if the category was added successfully, or an error message otherwise.
*/
const Create = async (req, res, next) => {
const category = new Category(req.body);
try {
await category.save();
return res.status(200).json({
message: "Category added successfully!"
});
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
});
}
};
Note:
-
@async
indicates that this is an asynchronous function. -
@function
is used to declare that this is a function. -
@param
describes the parameters that the function expects. -
@returns
describes the expected return value.
You may need to include additional details about the Category
class and the errorHandler.getErrorMessage
method if they are part of the module you're documenting.
Code refactoring
Suggest refactoring improvements for the following [language] code to enhance readability and maintainability: [code snippet].
Identify opportunities to apply [design pattern] in the given [language] code: [code snippet].
Optimize the following [language] code for better performance: [code snippet].
💬 Prompt
Suggest refactoring improvements for the following javascript code to enhance readability and maintainability: import ArticleCategory from '../models/article.category.model'
import errorHandler from './../helpers/dbErrorHandler'
import extend from 'lodash/extend'
import mongoose from 'mongoose'
const ArticleCategoryExists = async (req, res, next) => {
try {
let existing = await ArticleCategory.findOne({
$and: [{ article: req.body.article }, { category: req.body.category }]
})
if (existing) {
return res.status(401).json({
error: "This articlecategory already exists"
})
}
next()
} catch {
return res.status(400).json({
error: "could not retrieve articlecategory"
})
}
}
const DeleteExistingCategoriesofArticle = async (req, res) => {
try {
let deleted = await ArticleCategory.deleteMany({ article: req.articleid })
res.json(deleted)
} catch {
return res.status(400).json({
error: "could not delete articlecategories"
})
}
}
const DeleteExistingCategoriesofArticleFromBody = async (req, res, next) => {
try {
if (Array.isArray(req.body) && req.body.length > 0) {
await ArticleCategory.deleteMany({ article: req.body[0].article })
next()
} else {
return res.status(400).json({
error: "could not Find the categories of article"
})
}
} catch {
}
}
const CreateMultiArticleCategory = async (req, res) => {
try {
const articlecategory = await ArticleCategory.insertMany(req.body)
//console.log(articlecategory)
return res.status(200).json({
message: "Article categories added successfully!",
articlecategory: articlecategory
})
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
const CreateArticleCategory = async (req, res) => {
const articlecategory = new ArticleCategory(req.body)
try {
await articlecategory.save()
return res.status(200).json({
message: "Article category added successfully!",
articlecategory: articlecategory
})
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
const GetAllArticleCategories = async (req, res) => {
try {
let articlecategories = await ArticleCategory.find()
.populate('article', ['title'])
.populate('category', ['name'])
res.json(articlecategories)
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
const AllCategoriesOfArticle = async (req, res, next, articleid) => {
try {
let categories = await ArticleCategory.find({ article: articleid })
.populate('category').select('category')
req.categoriesofarticle = categories;
req.articleid = articleid
next()
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
const GetAllCategoriesOfArticle = async (req, res) => {
// console.log(req.categoriesofarticle)
res.json(req.categoriesofarticle)
}
const AllPublishedArticlesOfCategory = async (req, res, next, categoryId) => {
try {
const pipeline = [
{
// Match the specific category by its ID in the ArticleCategory collection
$match: { category: mongoose.Types.ObjectId(categoryId) }
},
{
// Join with the Article collection to get article details
$lookup: {
from: "articles",
localField: "article",
foreignField: "_id",
as: "articleDetails"
}
},
{
$unwind: "$articleDetails"
},
{
// Filter by articles that are published
$match: { "articleDetails.isPublished": true }
},
{
// Join with the Article collection to get article details
$lookup: {
from: "users",
localField: "articleDetails.user",
foreignField: "_id",
as: "userDetails"
}
},
{
// Project only the necessary fields
$project: {
// _id: 0,
"articleDetails": 1,
"userDetails": 1
}
}
];
const articles = await ArticleCategory.aggregate(pipeline).exec();
req.articlesofcategory = articles;
next()
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
const GetAllPublishedArticlesOfCategory = async (req, res) => {
res.json(req.articlesofcategory)
}
export default {
CreateArticleCategory,
GetAllArticleCategories,
AllCategoriesOfArticle,
GetAllCategoriesOfArticle,
AllPublishedArticlesOfCategory,
GetAllPublishedArticlesOfCategory,
ArticleCategoryExists,
DeleteExistingCategoriesofArticle,
DeleteExistingCategoriesofArticleFromBody,
CreateMultiArticleCategory
}
💬Response
Here are some improvements to the provided JavaScript code to enhance readability and maintainability:
Consistent naming: Adopting a consistent naming convention can greatly enhance the readability of your code. For example, method names should ideally be verbs because they represent actions.
Use descriptive variable names: Descriptive variable names provide insight into what the variable is used for.
Reduce redundancy: If there's code that appears multiple times (e.g., error response generation), consider creating a utility function to reduce redundancy.
Commenting: It's a good practice to use comments to explain what certain pieces of code are doing, especially when it's not immediately obvious.
Formatting: Proper and consistent indentation is key to readability.
Handle exceptions properly: Ensure that all asynchronous functions have proper error handling.
Let's refactor the given code with the above improvements:
import ArticleCategory from '../models/article.category.model';
import errorHandler from './../helpers/dbErrorHandler';
import extend from 'lodash/extend';
import mongoose from 'mongoose';
const respondWithError = (res, statusCode, errorMessage) => {
return res.status(statusCode).json({ error: errorMessage });
}
const checkArticleCategoryExists = async (req, res, next) => {
try {
const existingCategory = await ArticleCategory.findOne({
$and: [{ article: req.body.article }, { category: req.body.category }]
});
if (existingCategory) {
return respondWithError(res, 401, "This article category already exists");
}
next();
} catch (error) {
return respondWithError(res, 400, "Could not retrieve article category");
}
}
const deleteCategoriesForArticle = async (req, res) => {
try {
const deletedCategories = await ArticleCategory.deleteMany({ article: req.articleid });
res.json(deletedCategories);
} catch (error) {
return respondWithError(res, 400, "Could not delete article categories");
}
}
const deleteCategoriesForArticleFromBody = async (req, res, next) => {
try {
if (Array.isArray(req.body) && req.body.length > 0) {
await ArticleCategory.deleteMany({ article: req.body[0].article });
next();
} else {
return respondWithError(res, 400, "Could not find the categories of article");
}
} catch (error) {
// Handle the error (maybe logging it or sending a specific error message)
}
}
const createMultipleArticleCategories = async (req, res) => {
try {
const articleCategories = await ArticleCategory.insertMany(req.body);
return res.status(200).json({
message: "Article categories added successfully!",
articlecategory: articleCategories
});
} catch (error) {
return respondWithError(res, 400, errorHandler.getErrorMessage(error));
}
}
const createArticleCategory = async (req, res) => {
const articleCategoryInstance = new ArticleCategory(req.body);
try {
await articleCategoryInstance.save();
return res.status(200).json({
message: "Article category added successfully!",
articlecategory: articleCategoryInstance
});
} catch (error) {
return respondWithError(res, 400, errorHandler.getErrorMessage(error));
}
}
const getAllArticleCategories = async (req, res) => {
try {
const allArticleCategories = await ArticleCategory.find()
.populate('article', ['title'])
.populate('category', ['name']);
res.json(allArticleCategories);
} catch (error) {
return respondWithError(res, 400, errorHandler.getErrorMessage(error));
}
}
// Rest of the functions...
export default {
createArticleCategory,
getAllArticleCategories,
// ... other exported methods
}
Here are some specific changes made:
- Renamed methods to be more descriptive.
- Created a utility function
respondWithError
to handle error responses consistently. - Renamed variables for clarity.
- Removed unnecessary imports (e.g.,
extend
was not used in the provided code). - Consistent use of
error
as a parameter in catch blocks.
🚀 That was a nice improvement! I must say that was my code in one of my projects and I will make the suggested refactoring. I cave the code for all the controllers.
Design pattern suggestions
Based on the given
[language]
code, recommend a suitable design pattern to improve its structure:[code snippet]
.Identify opportunities to apply the
[design pattern]
in the following [language] codebase:[repository URL or codebase description]
.Suggest an alternative design pattern for the given
[language]
code that may provide additional benefits:[code snippet]
.
I gave for these prompts all the code (controllers, routers, models) . Try it with your code and see the results.
Code translation
Translate the following '[source language]' code to '[target language]': '[code snippet]'.
Convert the given '[source language]' class or module to [target language] while preserving its functionality and structure: [code snippet].
Migrate the following '[source language]' code that uses '[library or framework]' to [target language] with a similar library or framework: [code snippet].
💬Prompt
Translate the previous code to c#
Translating the provided JavaScript code to C# involves more than just syntactical changes. The design paradigms, frameworks, and even how web requests are handled can be very different.
Below is a translation that provides an equivalent C# version of the code using ASP.NET Core, assuming some conventions and necessary inclusions:
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace YourNamespace.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ArticleCategoryController : ControllerBase
{
private readonly IMongoCollection<ArticleCategory> _articleCategories;
public ArticleCategoryController(IMongoDatabase database)
{
_articleCategories = database.GetCollection<ArticleCategory>("ArticleCategory");
}
private IActionResult Error(int statusCode, string errorMessage)
{
return StatusCode(statusCode, new { error = errorMessage });
}
[HttpPost("CheckArticleCategoryExists")]
public async Task<IActionResult> CheckArticleCategoryExists([FromBody] ArticleCategory articleCategory)
{
var filter = Builders<ArticleCategory>.Filter.And(
Builders<ArticleCategory>.Filter.Eq(a => a.Article, articleCategory.Article),
Builders<ArticleCategory>.Filter.Eq(a => a.Category, articleCategory.Category));
var existingCategory = await _articleCategories.Find(filter).FirstOrDefaultAsync();
if (existingCategory != null)
{
return Error(401, "This article category already exists");
}
return Ok(); // Equivalent of "next" in Express.js.
}
// Other similar endpoints...
}
public class ArticleCategory
{
public ObjectId Id { get; set; }
public string Article { get; set; }
public string Category { get; set; }
}
}
Key Changes and Assumptions:
ASP.NET Core: I'm assuming we're working with ASP.NET Core, a popular framework for building web APIs in C#.
MongoDB with the official C# Driver: The original code seemed to be working with MongoDB using Mongoose in JavaScript. I've chosen the MongoDB C# driver for the C# version.
Separation of model: I've provided a basic
ArticleCategory
class to represent the data model. In a real-world scenario, it may need more details and can be placed in a separate file.Dependency Injection: ASP.NET Core uses dependency injection. I've injected the
IMongoDatabase
object into the controller, which would be registered in the startup configuration.Routing and Endpoints: Each method in the controller that should be exposed as an HTTP endpoint is decorated with appropriate attributes, such as
[HttpPost]
, to indicate the type of HTTP request it responds to.Synchronous to Asynchronous: Many C# methods, especially I/O-bound methods, are written to be asynchronous using the
async
andTask
keywords. This is reflected in the methods that interact with the database.Error Handling: In the ASP.NET Core version, error handling is done using the
IActionResult
return type.
This is a basic translation, and a lot of specifics (like specific error handling, data validation, etc.) would depend on the larger context of your application. Ensure that you have the necessary packages and setup (like MongoDB connection, ASP.NET Core setup, etc.) to make this code operational.
👀 Nice translation bro! And it works!
Explaining Code
- “Could you please explain how this JavaScript function works?” followed by the function you want to be explained
Chat ChatGPT explains code better than your c++ professor !
Could you please explain how this JavaScript function works? const UploadToDevto = async (req, res, next) => {
console.log("Upload to dev.to")
var articlefromrequest = req.article
var devtoarticle = {
"title": articlefromrequest.title,
"body_markdown": FixImagesUrls(articlefromrequest.articleBody),
"published": false,
"series": "eneaslari.com",
"main_image": "https://eneaslari.com/" + articlefromrequest.imageId,
"canonical_url": "https://eneaslari.com/articleview/render/" + beautifyURL(articlefromrequest.title) + "-" + articlefromrequest._id,
"description": articlefromrequest.description,
"tags": [articlefromrequest.tag],
"organization_id": 0
}
try {
const respond = await fetch("https://dev.to/api/articles", {
method: "POST",
headers: {
'Accept': 'application/vnd.forem.api-v1+json',
'Content-Type': 'application/json',
'api-key': '18T8s4fHyaPhcY7tDedX4TuU'
},
body: JSON.stringify({
article: devtoarticle
})
});
return res.status(respond.status).json({
message: "Article added successfully to DEV.to!",
status: respond.status
})
} catch (err) {
return res.status(400).json({
error: errorHandler.getErrorMessage(err)
})
}
}
💬Response
Certainly! Let's break down the UploadToDevto
function step by step:
-
Function Declaration:
const UploadToDevto = async (req, res, next) => { ... }
Here,
UploadToDevto
is declared as an asynchronous function, which means it's capable of pausing its execution until a promise is settled. It takes three parameters:req
(the incoming request object),res
(the outgoing response object), andnext
(a callback to pass control to the next middleware). -
Logging:
console.log("Upload to dev.to")
This line simply logs a message to the console, indicating that the function is attempting to upload to dev.to.
-
Extracting the article from the request:
var articlefromrequest = req.article
This retrieves the article object from the request (
req
) and assigns it to the variablearticlefromrequest
. Constructing the article for dev.to:
The devtoarticle
object is constructed based on the retrieved article with some transformations:
- The
title
,description
, andtag
properties are extracted directly. - The
body_markdown
property is passed through aFixImagesUrls
function (which we don't see the implementation for here, but it presumably modifies image URLs in some way). - The
main_image
property is constructed using a base URL and the image ID. - The
canonical_url
property is generated using a base URL, a beautified version of the title (withbeautifyURL
function), and the article ID.
- Uploading the article to dev.to:
The function then tries to upload the constructed article to dev.to using the fetch
API:
- A
POST
request is made tohttps://dev.to/api/articles
. - Headers for the request are set, including an API key (it's important to note that hardcoding API keys in code is not a secure practice).
- The body of the request is the stringified version of the
devtoarticle
object.
- Handling the response:
- If the upload is successful, the function returns a JSON response to the caller with a message and the status from dev.to's response.
- If there's an error during the fetch request (like network issues), it goes into the
catch
block where it responds with a 400 status code and an error message. The error message is derived fromerrorHandler.getErrorMessage(err)
, but since we don't see the implementation oferrorHandler
, we can only assume that it extracts or constructs an appropriate error message based on the provided error.
Note: As mentioned earlier, hardcoding the API key in the code is not a best practice. It would be more secure to store it in environment variables or some other form of secure configuration.
That's all for now. Keep using ChatGPT and it will be a very valuable tool for your coding productivity.
Because not it only will give you the answer but it will explain in to you. So don't be afraid that it will take your job. Please! Without you nothing works. or it works until it brakes.
Thanks for reading
Here are my resources for the prompts I tested. These links have a very extensive list of prompts.
Sources:
Posted on September 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 30, 2024
November 30, 2024