Using AI to develop and debug code

At different times throughout my career, I’ve found myself either part of a small team of web developers, or the sole developer within an advertising agency. And, like a lot of developers, I’m mostly a self-taught tinkerer and consider myself a designer first, and programmer, second. One thing is for certain, I’ve always had to find educational resources, either co-workers, industry peers, or externally by dissecting blog articles or tutorials to shore up my knowledge while learning new web technologies and adopting new workflows.

So, now that it’s been a full year since ChatGPT’s entry to the cultural zeitgest, I find myself using AI prompts for different tasks and experiencing various degrees of success. One thing I’ve found extremely helpful is to use it as my “coding buddy”, of sorts, to review my code, suggest approaches, and cite aspects of open source documentation. I’ve been using ChatGPT to do simple tasks such as discerning the root cause of a warning in an error log while debugging a custom WordPress theme for PHP 8.2 compatibility, or using it to combine it’s knowledge of the WordPress Codex and Gravity Forms Docs to help build a custom function that updates a WordPress User based off some values in a customized Gravity Form. I figured out an initial path to pursue, but throughout my development and subsequent testing I used ChatGPT to refine the code, squash bugs and point out which functions in the documentation to focus in on to achieve my goal.

*** Here’s an example of a conversation I had with the ChatGPT bot: ***

You (aka me):
In my php error log, this is the error that returns: [23-Jan-2024 16:05:23 UTC] Related User Field Value: 1

ChatGPT:
It seems that the ‘related_user’ field is returning a user ID (1) instead of a user object. In that case, you should use the user ID directly in your code. Modify your function to directly use the user ID without attempting to retrieve a user object. Here’s the updated code:

add_action( 'gform_advancedpostcreation_post_after_creation_4', 'value_increment', 10, 4 );

function value_increment( $post_id, $feed, $entry, $form ) {

	$related_user_id = get_field('related_user', $post_id); // user ID

	// Add additional checks for $related_user_id
	if (!$related_user_id) {
		error_log('Related User ID not found for post ID: ' . $post_id);
		return;
	}

	$value = get_field('value', 'user_' . $related_user_id ); // number
	$value_update = $value + 1;

	// Debugging statements
	error_log('Related User ID: ' . $related_user_id);
	error_log('Current Value: ' . $value);
	error_log('Updated Value: ' . $value_update);

	// Update the user value
	update_post_meta( 'user_' . $related_user_id , 'field_65ae8d5b42069', $value_update);

}

Now, the code directly uses the retrieved user ID without attempting to convert it into a user object. This should resolve the issue you were facing. After making this change, trigger the form again and check the error logs for any further issues.

You (aka me):
the last line doesn’t seem to be updating the user meta.

update_post_meta( 'user_' . $related_user_id , 'field_65ae8d5b42069', $value_update);

I changed it to this, and it still didn’t save the new value to the user profile:

update_post_meta( 'user_' . $related_user_id , 'value', $value_update);

ChatGPT:

If the update_post_meta function is not updating the user meta, there are a few things you can check:

  1. Verify User ID:Confirm that $related_user_id contains the correct user ID. You can add a debugging statement to check its value:
    error_log('Related User ID: ' . $related_user_id)
  2. Check Field Key:Confirm that the field key used in update_post_meta is correct. If you’re using ACF, you might want to double-check the field key associated with the ‘coins’ field. You can find the correct field key in the ACF settings.
  3. Check User Meta:Verify that the ‘coins’ user meta is being stored with the correct key. You can check this directly in the WordPress database or use a tool like phpMyAdmin.
  4. Try Using update_user_meta:Instead of update_post_meta, you can use the update_user_meta function directly to update user meta. Here’s how you can modify the code:
    update_user_meta( $related_user_id, 'value', $value_update );

This is assuming that the ‘value’ field is stored as user meta.

After making any necessary adjustments, trigger the form again and check the error logs for any debugging statements. If there are still issues, the debugging statements will help identify where the problem might be occurring.

*** End of Chatbot Conversation ***

But let’s be clear on one thing: I did not rely solely on ChatGPT to build this custom feature for me. I would merely ask it questions, and the answers would lead me down a particular path. I’d still have to do some course-correcting and a lot of code refinements along the way. I’m using it to enhance my approach and ultimately get there faster.

I’d test and iterate, and ask more questions. A lot of time, ChatGPT’s answers were obviously wrong, or didn’t pertain to my particular use case. Most of the time, that was due to my phrasing of the question, and the resulting path I was led down. If I found myself stumped, I’d revisit the next day with a fresh ChatGPT thread and tried to rephrase the question to get an answer closer to my needs. Test, refine, and test some more. I’d input the code I’ve written and ask for syntax suggestions. I’d point out an error log entry and it would cite the reason why, and I’d use that information to quickly solve the issue at hand. All-in-all, it helped me build a unique feature for a website that was either slightly out of reach, or, simply just reduced my development time by helping me get there faster.

At the end of the day, AI is simply one more tool in the arsenal that helps use get our job done. It’s not perfect, but neither are we.

What is the long-term viability of AI-generated code?

In the web development community, there are some concerns over using AI to generate large swaths of code that need to be maintained on long-term basis. If you’re copy-pasting large passages of AI-generated code you’ve got to consider how you’re going to manage this code throughout the life of the project.

I guess, the main take away is this: while AI can bring greater short-term productivity, if you become too depended on AI code-generation tools it could present challenges five or ten years down the line. Is the AI-generated code easy to iterate? Only time will tell.

Here’s a great podcast on this particular aspect of using AI generated code in your projects. Trigger warning: it really gets into the weeds.

How are you using AI to simplify, or otherwise enhance routines in your work?

To stay topical and truly flex AI technology, I fed Chat GPT the contents of this article and asked it to develop a featured image to use for this blog post. It distilled this article to the following image prompt prior to generating the image you see in this article:

ChatGPT:

Based on your article, I'll create an image that captures the essence of a developer's journey with AI assistance, particularly focusing on the partnership between a web developer and ChatGPT in solving coding challenges and enhancing web development workflows. Here’s the visualization I have in mind:

Scene Composition:

The background splits into two distinct but blending halves. The left side shows a cluttered desk with a computer displaying code, symbolizing the traditional web development environment. The right side morphs into a more streamlined, futuristic interface representing AI assistance, with digital code seamlessly integrating into the workspace.

A central figure, the developer, sits at the desk, bridging both worlds. The developer's posture suggests engagement and focus, with one hand on the keyboard (left side) and the other interacting with a holographic screen (right side) that displays ChatGPT-like dialogue bubbles.

Key elements like open source documentation, error logs, and snippets of PHP code float between the two realms, symbolizing the transfer of knowledge and the blending of human expertise with AI capabilities.

Subtle motifs like gears and neural networks overlay the transition area, indicating the merging of technology and human creativity.

The lighting contrasts the two halves: cooler, blue tones on the left suggest traditional computing, while warmer, softer glows on the right hint at the approachable, user-friendly nature of AI.

Key Themes to Highlight:

Partnership: The relationship between the developer and AI is collaborative, not one replacing the other.

Innovation: The futuristic elements contrasted with traditional coding highlight how AI tools like ChatGPT are changing the landscape of web development.

Problem-solving: The presence of error logs and code snippets underscores the practical challenges developers face and how AI can offer solutions.

This visualization should convey the narrative of how AI, represented by ChatGPT, has become an indispensable tool in the web developer’s arsenal, enhancing their capabilities and enabling them to achieve more with less.

Let's proceed with generating this image.

More and more marketing leaders indicate that they expect AI to be a part of their brand’s future. Surveys also suggest that they are experiencing uncertainty about that future. Jigsaw is already incorporating AI into our processes and solutions, while maintaining human oversight built upon time-tested marketing strategies. If you would like help making the transition to a more digital future, reach out to Jigsaw today for a consultation.