📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
Coding with AI Chris Minnick
📄 Page
4
Coding with AI For Dummies® Published by: John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030-5774, www.wiley.com Copyright © 2024 by John Wiley & Sons, Inc., Hoboken, New Jersey Published simultaneously in Canada No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the Publisher. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/ go/permissions. Trademarks: Wiley, For Dummies, the Dummies Man logo, Dummies.com, Making Everything Easier, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHORS HAVE USED THEIR BEST EFFORTS IN PREPARING THIS WORK, THEY MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES REPRESENTATIVES, WRITTEN SALES MATERIALS OR PROMOTIONAL STATEMENTS FOR THIS WORK. THE FACT THAT AN ORGANIZATION, WEBSITE, OR PRODUCT IS REFERRED TO IN THIS WORK AS A CITATION AND/OR POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT THE PUBLISHER AND AUTHORS ENDORSE THE INFORMATION OR SERVICES THE ORGANIZATION, WEBSITE, OR PRODUCT MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. THIS WORK IS SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING PROFESSIONAL SERVICES. THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SITUATION. YOU SHOULD CONSULT WITH A SPECIALIST WHERE APPROPRIATE. FURTHER, READERS SHOULD BE AWARE THAT WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ. NEITHER THE PUBLISHER NOR AUTHORS SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES. For general information on our other products and services, please contact our Customer Care Department within the U.S. at 877-762-2974, outside the U.S. at 317-572-3993, or fax 317-572-4002. For technical support, please visit https://hub.wiley.com/community/support/dummies. Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media such as a CD or DVD that is not included in the version you purchased, you may download this material at http://booksupport.wiley.com. For more information about Wiley products, visit www.wiley.com. Library of Congress Control Number: 2024931771 ISBN: 978-1-394-24913-8 (pbk); 978-1-394-24915-2 (ebk); 978-1-394-24914-5 (ebk)
📄 Page
5
Contents at a Glance Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Part 1: Techniques and Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . 7 CHAPTER 1: How Coding Benefits from AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 CHAPTER 2: Parsing Machine Learning and Deep Learning . . . . . . . . . . . . . . . . . . . . . 37 CHAPTER 3: AI Coding Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 4: Coding with Chatbots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Part 2: Using AI to Write Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 CHAPTER 5: Progressing from Plan to Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 CHAPTER 6: Formatting and Improving Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 CHAPTER 7: Finding and Eliminating Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 CHAPTER 8: Translating and Optimizing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Part 3: Testing, Documenting, and Maintaining Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 CHAPTER 9: Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 CHAPTER 10: Documenting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 CHAPTER 11: Maintaining Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Part 4: The Part of Tens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 CHAPTER 12: Ten More Tools to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 CHAPTER 13: Ten AI Coding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
📄 Page
6
(This page has no text content)
📄 Page
7
Table of Contents v Table of Contents INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Foolish Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Icons Used in This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 Beyond the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 Where to Go from Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .5 PART 1: TECHNIQUES AND TECHNOLOGIES . . . . . . . . . . . . . . . . . 7 CHAPTER 1: How Coding Benefits from AI . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Banishing Boring Tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9 Spotting boring tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Letting AI write the template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10 Crafting CRUD with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14 Helping with Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Stop remembering trivial details . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20 Hinting at code mastery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .21 Adapting to new syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22 Linting with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24 Detecting bad code with static code analysis . . . . . . . . . . . . . . . . . .25 Integrating AI with static code analysis . . . . . . . . . . . . . . . . . . . . . . .26 Using AI as a Tutor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26 Studying AI’s potential in education . . . . . . . . . . . . . . . . . . . . . . . . . .26 Avoiding potential pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Pairing Up with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27 Overview of pair programming styles . . . . . . . . . . . . . . . . . . . . . . . .28 Understanding the pros and cons of pair programming with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .28 AI pair programming session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29 CHAPTER 2: Parsing Machine Learning and Deep Learning . . . . . 37 Decoding Machine and Deep Learning . . . . . . . . . . . . . . . . . . . . . . . . . .38 Defining key concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .38 Thinking about neural networks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .39 Training and testing models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .41 Demystifying Natural-Language Processing . . . . . . . . . . . . . . . . . . . . . .43 History of NLP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44 Overcoming the challenges of NLP . . . . . . . . . . . . . . . . . . . . . . . . . . .45 Understanding Transformers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Learning to pay attention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .48 Getting tokens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .49
📄 Page
8
vi Coding with AI For Dummies Illuminating Generative AI Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53 Recognizing AI’s Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Language models are bad at math . . . . . . . . . . . . . . . . . . . . . . . . . . .54 Language models are wordy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54 AI has limited knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 AI lacks common sense . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .55 AI has accuracy issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 AI has the potential to be biased . . . . . . . . . . . . . . . . . . . . . . . . . . . . .56 CHAPTER 3: AI Coding Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Navigating GitHub Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .57 Installing the Copilot plug-in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .58 Working efficiently with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59 Using keyboard shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .63 Exploring Tabnine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64 Installing Tabnine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65 Setting up Tabnine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .66 Understanding Tabnine’s AI-driven code completion . . . . . . . . . . .67 Reviewing Replit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68 Starting a website with Replit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .70 Exploring the Replit workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71 Pairing up with Replit AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73 CHAPTER 4: Coding with Chatbots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Improving Your Prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79 Adjusting the temperature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80 Deciphering the elements of a prompt . . . . . . . . . . . . . . . . . . . . . . .82 Open-ended versus closed-ended prompts . . . . . . . . . . . . . . . . . . .83 Using different types of prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . .83 Prompting like a pro . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .86 Chatting with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Understanding slash commands . . . . . . . . . . . . . . . . . . . . . . . . . . . .87 Knowing Copilot’s agents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88 Getting the most out of Copilot Chat . . . . . . . . . . . . . . . . . . . . . . . . .88 Chatting with ChatGPT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Signing up and setting up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91 Setting custom instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92 Diving into the OpenAI Platform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96 Checking your credits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .97 Messing around in the playground . . . . . . . . . . . . . . . . . . . . . . . . . . .98 Running examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99 Playing the roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100 Adjusting the model’s settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101 Getting an API key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102 Developing a Chatbot with OpenAI . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
📄 Page
9
Table of Contents vii PART 2: USING AI TO WRITE CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 CHAPTER 5: Progressing from Plan to Prototype . . . . . . . . . . . . . . . . . 111 Understanding Project Requirements . . . . . . . . . . . . . . . . . . . . . . . . . .111 Determining the software requirements . . . . . . . . . . . . . . . . . . . . .112 Domain requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .112 Functional requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .113 Non-functional requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .114 Writing an SRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115 Generating Code from an SRS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 Using a zero-shot approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .118 Breaking down the problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120 Blending Manually Written and AI-Generated Code . . . . . . . . . . . . . .121 Writing the prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121 Writing the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .122 Submitting follow-up prompts . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Testing the server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125 Implementing few-shot prompting on the server . . . . . . . . . . . . .127 Improving the client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .130 Moving logic from AI to the client . . . . . . . . . . . . . . . . . . . . . . . . . . .133 Tips and Tricks for Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . .136 Don’t stop coding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Be specific . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Think in steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Ask follow-up questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Check the official documentation . . . . . . . . . . . . . . . . . . . . . . . . . . .137 Use examples and context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Prioritize security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Keep learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Keep your tools updated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 Be mindful of AI’s limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138 CHAPTER 6: Formatting and Improving Your Code . . . . . . . . . . . . . . 139 Using AI Tools for Code Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 Setting up your formatting tools . . . . . . . . . . . . . . . . . . . . . . . . . . . .140 Using Prettier to automate code formatting . . . . . . . . . . . . . . . . . .142 Refactoring with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Recognizing code smells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147 Detecting code smells with Copilot . . . . . . . . . . . . . . . . . . . . . . . . . .151 Refactoring safely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .153 Generating Refactoring Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Setting event listeners correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . .154 Removing the magic number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155 Reducing global data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156
📄 Page
10
viii Coding with AI For Dummies Fixing long functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157 Fixing inconsistent naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158 Lack of comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159 Making AI play tic-tac-toe better . . . . . . . . . . . . . . . . . . . . . . . . . . . .160 CHAPTER 7: Finding and Eliminating Bugs . . . . . . . . . . . . . . . . . . . . . . . . . 167 Knowing Your Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Strategies for detecting bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .168 Identifying common types of bugs . . . . . . . . . . . . . . . . . . . . . . . . . .168 AI-assisted bug reporting with Jam . . . . . . . . . . . . . . . . . . . . . . . . . .170 Preventing Bugs with Linting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Installing a linter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .173 Installing the ESLint extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .174 Fixing your code with a linter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .176 Combining linting with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .177 Changing the rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180 Detecting Bugs with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .182 Automating Bug Fixes with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Introducing Snyk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185 Executing automated bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .188 Verifying automated bug fixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190 Knowing when to automate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191 CHAPTER 8: Translating and Optimizing Code . . . . . . . . . . . . . . . . . . . . 195 Translating Code to Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . .195 Preparing your code for translation . . . . . . . . . . . . . . . . . . . . . . . . .196 Exploring translation strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . .197 Translating a complete program using GPT-4 . . . . . . . . . . . . . . . . .201 Verifying translated code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .203 Optimizing Your Code with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204 Getting code optimization suggestions . . . . . . . . . . . . . . . . . . . . . .205 Avoiding premature optimization . . . . . . . . . . . . . . . . . . . . . . . . . . .209 PART 3: TESTING, DOCUMENTING, AND MAINTAINING YOUR CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 CHAPTER 9: Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Writing a Test Plan . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214 Deciding between formal and agile . . . . . . . . . . . . . . . . . . . . . . . . .214 Stepping through the test planning process . . . . . . . . . . . . . . . . . .214 Understanding the role of AI in test planning . . . . . . . . . . . . . . . . .215 Identifying core functionalities . . . . . . . . . . . . . . . . . . . . . . . . . . . . .216 Generating test scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217
📄 Page
11
Table of Contents ix Working with a Testing Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Installing Jest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .219 Running Jest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Generating test cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222 Reading a coverage report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Analyzing test results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .226 Test-Driven Development with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .231 CHAPTER 10: Documenting Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Working with Documentation Bots . . . . . . . . . . . . . . . . . . . . . . . . . . . . .240 Building your own documentation bot . . . . . . . . . . . . . . . . . . . . . .240 Testing your documentation assistant . . . . . . . . . . . . . . . . . . . . . . .243 Generating Code Comments and Annotations . . . . . . . . . . . . . . . . . . .245 Installing and testing Mintlify Doc Writer . . . . . . . . . . . . . . . . . . . . .245 Commenting on Underscore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247 Creating Visual Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250 Generating a sequence diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . .251 Generating a requirements diagram . . . . . . . . . . . . . . . . . . . . . . . .252 Automating API Documentation with AI . . . . . . . . . . . . . . . . . . . . . . . .255 Documenting a REST API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .256 Creating an API documentation chatbot . . . . . . . . . . . . . . . . . . . . .260 CHAPTER 11: Maintaining Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Knowing the Four Types of Maintenance . . . . . . . . . . . . . . . . . . . . . . .267 Corrective software maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . .268 Adaptive software maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . .269 Perfective software maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . .269 Preventative software maintenance . . . . . . . . . . . . . . . . . . . . . . . . .269 Utilizing AI for Code Maintenance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270 Enhancing Code Quality with AI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .271 Understanding technical debt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .272 Getting started with Code Climate . . . . . . . . . . . . . . . . . . . . . . . . . .273 Enabling the text coverage report . . . . . . . . . . . . . . . . . . . . . . . . . . .276 Analyzing code quality metrics . . . . . . . . . . . . . . . . . . . . . . . . . . . . .276 Making AI-assisted code quality improvements . . . . . . . . . . . . . . .277 PART 4: THE PART OF TENS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 CHAPTER 12: Ten More Tools to Try . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Amazon CodeWhisperer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287 Sourcegraph Cody . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .288 DeepMind AlphaCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .289 Google Bard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .290 Codeium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .291
📄 Page
12
x Coding with AI For Dummies Claude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .292 Microsoft IntelliCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .293 Sourcery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .294 Bugasura . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295 UserWay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296 CHAPTER 13: Ten AI Coding Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 Code.org’s AI Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 Kaggle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .300 Google’s Dataset Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .301 edX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .302 Edabit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303 StatQuest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .304 AI4All Open Learning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .305 Gymnasium . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .306 fast.ai . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .307 Microsoft Learn . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .308 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
📄 Page
13
Introduction 1 Introduction I started writing this book almost a year after OpenAI launched ChatGPT. That launch and the subsequent releases of generative AI tools by Microsoft, Google, Facebook, and others have begun to change how we think about creating con- tent. At the same time, we’re facing important questions about what the future of work will look like — especially for those of us whose job primarily involves the things that tools such as ChatGPT are pretty good at. My own feelings about generative AI are mixed. On one hand, I worry that the skills in writing and programming that I’ve spent more than half my life working on will no longer be useful. On the other hand, I see that AI has the potential to take on some of the most boring and least rewarding work I do, saving me time and effort that I can devote to the more creative parts of writing and programming. I also worry that when I do write things using old-fashioned methods (aka “I think of them and write them”), people will assume that I used AI. This happened with a book I wrote last year on a relatively current topic. People who didn’t bother to read the book commented that “it was probably written by AI.” As a result of this experience, I announced that I’d livestream the process of writing my next book. I had no idea at the time that my next book would be about AI. So, here I am, writing a book about coding with AI while live-streaming my writing processes in an attempt to prove to future readers that the book wasn’t generated by AI. If you have any doubt that I wrote this book the old-fashioned way, or if you have a few hundred hours to spare, you can see the entire book being written by going to https://bit.ly/codingwithai. Even though I refuse to use AI to write my books, and I’m generally against other people using AI to write books, I feel differently about using AI tools to generate computer code. The history of computer programming has been about people inventing better tools that make coding easier. When I worked at Software Develop- ment Magazine in the 1990s, the technical editor was Roger Smith. One day, while we were talking about a new programming tool, Roger told me that he believed that in the future we’d be able to use natural language to write software. I was skeptical. Almost 30 years later, it turns out that Roger was right. The pace of change in AI is fast. Technologies and tools that are new and interest- ing this month will be replaced by better ones next month. Because I write about
📄 Page
14
2 Coding with AI For Dummies technology and programming, there’s always the risk that something I write today will be outdated when the book is released. However, even though AI and AI software development tools will certainly have improved, the techniques I write about here will be just as applicable — unless, of course, AI has made the profes- sion of software developer obsolete and everyone who used to be a software devel- oper now gets paid to hang out on the beach (or whatever your idea of relaxation and fun is). Whether you embrace this new era of AI-assisted coding or resist it, there’s no denying that it’s here. In this book, I show you how these tools work and how you can use them to not only make writing code easier and faster but to help you write better code. I hope you enjoy reading this book and that you find it useful. If you have any questions or comments, please reach out to me at chris@minnick.com. About This Book When it comes to coding with generative AI, we’re all dummies at this point. Whether you’re a new programmer or a veteran, this book will teach you what you need to know to benefit from the new tools that are rapidly becoming available. I cover these topics: » Understanding foundational principles of machine learning (ML), deep learning (DL), and generative AI (GenAI) » Working with AI responsibly, safely, and ethically » Using some of the latest tools for coding with AI » Using AI to help with • Automating monotonous coding tasks • Learning new skills • Improving your code • Testing your code • Documenting your code • Maintaining your code
📄 Page
15
Introduction 3 As you go through the book, keep the following in mind: » You can read the book from beginning to end, but feel free to skip around if you like. If a topic interests you, start there. You can always return to previous chapters, if necessary. » At some point, you will get stuck, and something you try will not work as intended. Do not fear! There are many resources to help you, including support forums, others on the internet, AI chatbots, and me! You can contact me via email at chris@minnick.com or through my website (https://www. chrisminnick.com). Additionally, you can sign up for my Substack (https:// chrisminnick.substack.com) to receive occasional updates from me about AI, programming, and learning. » Code in the book appears in a monospaced font like this: <h1>Hi there!</h1>. Some web addresses break across two lines of text. If you’re reading this book in print and want to visit one of these web pages, simply key in the web address exactly as it’s noted in the text, pretending as though the line break doesn’t exist. If you’re reading this as an e-book, you have it easy — just click the web address to be taken directly to the web page. Foolish Assumptions I do not make many assumptions about you, the reader, but I do make a few. I assume you have some experience or familiarity with programming in a com- puter language. It doesn’t matter which language you code in, just that you know what programming is and you’ve done it before. If you’re new to computer pro- gramming, many excellent books and tutorials are available that can give you the background you need for this book in a few days. I recommend Coding All-in-One For Dummies, 2nd Edition (written by me and an awesome team of other coding experts), which contains an introduction to all the languages and techniques you use in this book. In particular, read the chapters about Python and JavaScript. Most of the examples in this book are JavaScript code, because that’s the pro- gramming language I know the best. However, this is not a JavaScript-specific book and the techniques and tools I use to help write or improve my JavaScript code work with any language. The code examples are generally simple enough to be understood without a specific knowledge of JavaScript.
📄 Page
16
4 Coding with AI For Dummies I assume you have a computer running a modern web browser. You will do most of the exercises in this book by using web-based resources. Although it may be possible to complete these exercises using a smartphone or tablet, I don’t recommend it. I assume you have access to an internet connection. Because the language models we’ll be working with are far too large to install on your computer, an internet connection will be essential to completing the hands-on element. I assume you can download and install free software to your computer. Often- times, the computer you use at work will have restrictions on what can be installed by the user. Using your own computer to develop and run the applications in this book should work without a problem. Icons Used in This Book Here are the icons used in the book to flag text that should be given extra attention or can be skipped. This icon flags useful information or explains a shortcut to help you understand a concept. This icon explains technical details about the concept being explained. The details might be informative or interesting but are not essential to your understanding of the concept. Try not to forget the material marked with this icon. It signals an important con- cept or process that you should keep in mind. Watch out! This icon flags common mistakes and problems that can be avoided if you heed the warning.
📄 Page
17
Introduction 5 Beyond the Book A lot of extra content that you won’t find in this book is available at www.dummies. com. Go online to find the following: » The source code for the examples in this book: Go to www.dummies.com/ go/codingwithaifd. The source code is organized by chapter. The best way to work with a chapter is to download all the source code for it at one time. » The cheat sheet: Go to www.dummies.com and, in the search field, typing Coding with AI for Dummies. You’ll find helpful prompting tips for coding with AI, a list of dangers when using AI-generated code, and a tongue-in-cheek look at what AI coding assistants can’t do. » Updates: AI is changing rapidly, and I don’t expect it to stop doing so after this book is published, so the commands and syntax that work today may not work tomorrow. You can find any updates or corrections by visiting www.dummies. com/go/codingwithaifd or https://github.com/chrisminnick/ coding-with-ai. Where to Go from Here As you embark on your journey of learning to code with AI, keep an open mind but also a large dose of skepticism and patience. In spite of how impressive the cur- rent generation of GenAI tools is (and they’re surely much better by the time you read this), we’re still in the infancy of this stuff. If you want to get a basic understanding of AI-assisted coding, go to Chapter 1. If you want to find out more about how these tools work and about machine learning in general, read Chapter 2. If you want to learn about some of the tools that are available today for coding with AI, see Chapters 3 and 4. If you want to get right into experimenting with the combination of coding and AI, skip to Chapter 5. Congratulations on taking your first step towards AI-assisted coding, and thank you for trusting me as your guide.
📄 Page
18
(This page has no text content)
📄 Page
19
1Techniques and Technologies
📄 Page
20
IN THIS PART . . . Discover how AI-enhanced tools can help make you a better and more efficient programmer. Dig into the fundamental concepts behind machine learning and deep learning. Explore AI pair programming tools. Converse with the latest generative models to assist with coding tasks.