Documenting code is a cool ChatGPT skill

TL;DR ChatGPT creates excellent JSDoc comments that explain method purpose, parameters and return values. Can't help to think: if a comment can be automatically and reliably generated by an AI, it shouldn't be part of the code but generated on demand when needed.

 (please note: I'm a JS newbie, sometimes I'm easy to amaze)

So I thought why not give ChatGPT the task to write good comments for the method I had asked it to create. So I took a class and a method and asked it to create a comment.

 And it did. Here's an example:

/** 
  * Equips a tool from the creature's bag to a specified limb.
  * @param {Tool} tool - The tool to be equipped on the limb.
  * @param {number} limbIndex - The index of the limb in the creature's limbs array.
  * @returns {boolean} - Returns true if the tool was successfully equipped on the limb, and false if the tool was not found in the bag,
  * the limb already has a tool equipped, or the tool is not compatible with the limb type.
  */
 

So in the dialog that unfolded, I learned that JS has something like Javadoc, that it's called JSdoc, how it works, and so on. Very enlightening. And I got my classes documented without much effort.

I just had to re-read every comment; sometimes ChatGPT remembered subtly wrong class implementations and created documentation for parameters that didn't exist. But by pasting the method into the prompt it always did the right thing.

Which opens up the interesting question whether 90% of all comments (like the one above) are irrelevant now that we have AIs like ChatGPT. If a comment can be reliable re-produced by a machine whenever you like, why have it in the code at all? 

There are comments about the "why" - why does the method do what it does, what is its purpose in the lifetime of an object, why was it implemented this way and not another. These are most likely elusive for AIs today, and have a reason to exist. But those comments that explain what the method does and what the parameters mean... might be a thing of the past, automatically generated by JSDocGPT :-)

Maybe the new copilots from amazon and others can do this? Create comments and/or hide them if they ae trivial?


Kommentare

Beliebte Posts aus diesem Blog

ChatGPT as a teacher

How it started...

Pathfinding and A*