Three Ways ChatGPT Can Help You Write Better Code
If you’ve been online in the past 3 months, you’ll know the chatbot ChatGPT has captured the attention of people around the world with entertaining responses to anything imaginable, even boasting the capability to simulate a Linux terminal!
Simulating a terminal is a fun gimmick, but you’re probably more interested in knowing when AI is going to flip the world on its head. Luckily for software developers, we can start today.
Software development is a great domain to start in because it follows well-defined rules and there are plenty of public code examples for AI to learn from.
Before we start, take a moment to remember you should be careful about putting information private to you, your company, or your customers into third parties like ChatGPT.
1. Command line & MySQL
As devs, we’ve all experienced the shell command or SQL query that was supposed to take thirty seconds to write but instead took thirty minutes.
ChatGPT can help in those scenarios by either writing the command for you or refining the code you write. For instance, I recently wanted to write a command that would delete all files with a certain extension from a directory. Doing this manually would have required referencing the manual or counseling online resources. Much better is to instead ask ChatGPT, which gives you the command and an explanation of how it works.
Here’s how I use ChatGPT to create my file deletion command in under 10 seconds:
When dealing with more complex tasks, you can ask ChatGPT to suggest a fix for your failing command or query. This is great for everyone, from those new to the language, to experienced developers who want pesky syntax errors fixed!
2. Improving readability & modularity
We've all been there – you start coding before you know exactly how to approach the problem and end up with an ugly mess. Why not ask ChatGPT to make it better?
Request that it "make my code shorter, more readable, and more modular", and you'll get back a much more organized version of your code. You can even give custom instructions like "add comments to explain the purpose of each block", "split this function into two smaller functions", or use a particular library. Make sure your code is under 30 lines for maximum effect!
3. Finding bugs & edge cases
ChatGPT can help you detect potential issues with your code before running it, saving you time and ensuring your program runs smoothly in production. Just feed your code into ChatGPT and ask it to identify any potential edge cases or bugs. It will analyze your code and give you a list of problems to look at. Here's a simplified version of a real bug I’ve encountered in production – ChatGPT would've been a huge help!
Got your solution and need to test it? Just ask ChatGPT!
This article was written by ChatGPT...just kidding...maybe.
You may also like
Mar 2, 2023
Three Reasons Why Your Company Should Be Having 'Voice of the Customer' Meetings