- Promptstacks
- Posts
- Using Generative AI to Code
Using Generative AI to Code
đšâđ» Transform Your Tech Literacy
TLDR;Â
6 Minute Read đ
This week, we'll be revisiting an article that highlights how ChatGPT can be a game-changer in your journey to become more tech literate. In today's competitive job market, employers increasingly seek candidates with a solid grasp of technology, including the ability to navigate coding frameworks and implement code snippets.
Even if you're new to coding, this article will undoubtedly provide you with invaluable insights and practical value based around implementing code through ChatGPT.
Midjourney: A David Hockney of a man coding
ChatGPT has completely changed the way we code
Many software developers, data professionals, and tech-savvy individuals have yet to realise the immense benefits of incorporating ChatGPT into their coding endeavours. However, this is a missed opportunity. By using ChatGPT as a learning tool, one can significantly accelerate their understanding of programming languages.
The best techniques for using ChatGPT to code
Explaining Concepts: It can provide simple, human-readable explanations for many programming concepts, from basic principles like âwhat is a loopâ or âhow does a function workâ, to more complex topics like âhow does asynchronous programming workâ or âwhat is the difference between OOP and functional programmingâ.
Syntax Help: If youâre uncertain about how to write a particular line of code or construct it in a certain language, you can ask ChatGPT for help. For example, you can ask âhow do I write a for loop in Python?â or âwhatâs the syntax for declaring a variable in JavaScript?â
Debugging Assistance: While ChatGPT doesnât have the capability to execute or debug code, it can provide suggestions based on the code snippets or error messages you provide. For example, if youâre seeing a common error message like a âNullReferenceExceptionâ in C# or âTypeErrorâ in Python, you can ask for common reasons why that error might occur and get general advice on how to fix it.
Coding Best Practices: ChatGPT can give advice on best coding practices, such as design patterns, naming conventions, comment usage, code organisation, and more.
Code Examples: You can ask ChatGPT to generate simple code examples in many popular programming languages. Itâs important to note, however, that while the code examples should be generally correct, they may not always be perfect or optimal due to the nature of the AIâs training.
Learning Resources: ChatGPT can provide recommendations for online resources, books, tutorials, and courses to help you learn or master a programming language or concept.
Leveraging Generative AI to help you code can bring various advantages. Rather than embarking on the coding journey alone, one can use ChatGPT as a coding assistant to circumvent many of the usual obstacles encountered. Now, let's delve into the specifics and explore a few additional examples.
#1. Generate your coding skeleton
ChatGPT is a secret weapon when it comes to building new projects from scratch. With a few prompts (inputs into ChatGPT), it can generate a skeleton for any code structure needed.
For instance, imagine I want to use an external API (Application Programming Interface). I can ask ChatGPT to draft an initial code structure for me.
Generate boiler plate code that integrates an external API using Python.
And ChatGPT answers like magic:
Boiler plate generation
Or I can ask for a skeleton (a basic layout of a project without any actual data) to scrape (website scraping is when a computer program automatically collects information from a website. It's like using a robot to copy and paste information, which can be useful for research or making tools) LinkedIn using Selenium, and ChatGPT gives me the following output.Â
#2. Research and compareÂ
Making decisions on how to implement something can be tough, especially when there are multiple options to choose from. My go-to method is to create a basic proof of concept for each approach and then compare them. But, with the help of ChatGPT, this process just got a lot easier.
I can now ask it for its opinion on which option or library is best for my code development. This saves me time and effort in the decision-making process and ensures that I am using the best tools for the job.
Letâs imagine I want to work with geospatial data but I am not sure whether I should use Geopandas or Plotly. I can ask ChatGPT to compare them for me with a type included ââ âand the large language model answers immediately with itâs output explaining the main differences between both libraries.
ChatGPT explains the differences between GeoPandas and Plotly
Now, assume I want to scrape a website, I can ask whatâs the best library to do so is. ChatGPT answers with a list of the most popular web-scraping libraries in Python.
ChatGPT explains the most popular scraping website.
You can even ask what the best option for the website you want to scrapeâ isââeven though ChatGPT will most likely warn you that it will be against that websiteâs content policyâââso be careful.
Whatâs the best option to scrape a social network?
ChatGPT explains the best option to scrape a social network.
#3. Understanding code
Weâve all been there - struggling to understand a codebase that wasnât created by us. Navigating through a complex and poorly-organised codeâ setââalso known as spaghetti codeâââcan be a frustrating and time-consuming task.
But, with ChatGPT, understanding a new codebase just got a lot easier. I can now simply ask it to explain the functionality of the code, which means I can understand it in absolutely no time at all. No more time wasting and wasted effort trying to decipher poorly-written code.
Letâs imagine I am trying to scrape LinkedIn and I find a random code on the internet that is supposed to scroll down the LinkedIn job offers website, but Iâm not entirely sure what it does at each point. Well, we can sort this issue out.
What does the following code do? [insert code here]
Again, ChatGPT answers right away with a really detailed explanation of the code above. You can even ask for more details in a given part of the code â just play a bit with ChatGPT and youâll discover what an advantage it gives you.
ChatGPT explains the code I didnât understand
#4. Adding comments to our code
ChatGPT can also help us to improve the quality and maintainability of our own code. By asking it to add comments line by line, we can ensure that our code is properly documented before shipping. This eliminates the need for uncommented code and makes it easier for others to understand and work with our code.
ChatGPT can significantly improve readability, maintainability and collaboration with others.
*be careful here, if your code is extremely private I wouldnât suggest using it in ChatGPT
Add comments to the following code [insert code here]
Adding comments to your code.
#5. Rewriting our code using style
ChatGPT is not only a valuable tool for understanding unfamiliar code, but it can also help us to ensure our own code follows industry standards and conventions. By asking it to correct our code to conform with the Pep-8 convention â or even create a custom convention for our coding style â we can avoid the need for costly and time-consuming refactoring when merging code from different repos or teams.
This helps to streamline the collaboration process and make it more efficient.
Is we ask ChatGPT to write the previous code using Pep-8 standard, it will directly give us the re-factorised code.
Can you rewrite the following code using Pep8 standard [Insert code here]
ChatGPT giving our code following Pep8 standard.
ChatGPT can both help you learn to code and augment your code.
However, It's important to remember that we still need to think critically when working with AI models like ChatGPT, just like we do with our human colleagues. So, before you jump into copying AI-generated responses, take some time to check the prompts. Believe me, you get out what you put in.
Promptstacksâ Accelerator Course
Participants will gain a comprehensive understanding of ChatGPT's strengths, weaknesses, and limitations. An excellent opportunity for professionals seeking to enhance skills with cutting-edge AI. *Includes 500 prompts.
28 lessons included.
Convened by Sean Melis (Ex-Deloitte AI Consultant).
"No regrets... The course material is concise and well laid-out. I love how the author added plenty of prompts for me to try. I've taken several ChatGPT courses already but this one is the best!!! Thank you, Sean!" â Maury
Editors Product Pick đș
Introducing our top pick for the week: Chatbase! This innovative tool empowers users to create customized AI chatbots with ease, trained on any corpus, content, or data. Imagine being able to effortlessly access specific information from sources like research papers, databases, and more.
Currently Chatbase has free options.
Make Me A MidJourney đ€Â
Wonders of the World - Mispelled
The Great Ball of China
The falling tower of Pizza
The Gremlin, Russia
Are you happy with this newsletter? |
Author This Week
Josep Ferrer
If youâd like to write for Promptstacks sign up here.