Module 7 Lesson 1: Writing Code with ChatGPT
How to use ChatGPT as a Senior Developer. Writing boilerplate, implementing logic, and translating between languages.
Writing Code with ChatGPT
Whether you are a seasoned engineer or a complete beginner, ChatGPT is a force multiplier for coding speed. It excels at Boilerplate, Logic Implementation, and Translation.
1. Generating Boilerplate
Writing the basic structure of a project is tedious.
- "Generate a Python FastAPI project structure with three endpoints: /user, /data, and /health. Include a requirements.txt file."
2. Implementing Specific Logic
Don't ask for "the whole app." Ask for specific functions.
- "Write a recursive Python function to find the sum of all nested values in a deeply nested JSON object."
graph LR
Goal[English Requirement] --> AI[ChatGPT Engine]
AI --> Code[Source Code]
Code --> Test[Compiler/Interpreter]
Test -->|Fail| Dev[Human Fix/Refine]
Test -->|Pass| Ship[Final Product]
3. Translation Between Languages
Knowing one language well allows you to write in others using AI.
- "Translate this Javascript
mapfunction into a Python List Comprehension."
4. Best Practices for Coding
[!IMPORTANT] Always verify AI code. It can generate Insecure Code (like hardcoded passwords) or use Outdated Libraries.
Use Markdown Snippets
When asking for code, ask for it in Markdown blocks for easy copying.
- "Provide the final code in a single Python markdown block."
Hands-on: The "FizzBuzz" Test
- Prompt: "Write a Python script that prints the numbers from 1 to 100. For multiples of three, print 'Fizz' instead of the number. For multiples of five, print 'Buzz'. For numbers that are multiples of both three and five, print 'FizzBuzz'."
- Review: Look at the logic. Is it efficient? Could it be shorter?
Key Takeaways
- Use AI for specific functions, not giant systems.
- Always include the Environment (e.g., "Python 3.10").
- Treat the AI as a Pair Programmer, not a replacement.