8 Best Practices to Generate Code Using AI Tools
By Qodo
Summary
## Key takeaways - **Break down complex tasks for AI**: AI coding assistants perform best when handling focused, specific tasks. Break down complex features into smaller units of work for better results. [00:20] - **Context is key for AI accuracy**: Providing context in each ask helps AI assistants understand the scope and purpose of the task, leading to more accurate code generation. [00:29] - **Iterate and refine AI code**: AI-generated code might need refinement. Iterating helps improve accuracy by building on previous interactions and ensuring coherent code. [02:41] - **Use advanced commands for precision**: Leverage advanced predefined commands or settings offered by AI coding assistants to trigger more precise and tailored responses for specific asks. [05:55] - **Ask AI to explain generated code**: Understanding the generated code enhances your ability to modify or expand it. Asking the AI for explanations can clarify complex sections. [06:10]
Topics Covered
- Structured Prompting Optimizes AI Code Generation
- Developers Remain the Driver of AI-Generated Code
- Proactively Test AI-Assisted Code for Quality
- Iteratively Refine AI-Generated Code for Accuracy
- Leverage Advanced AI Commands for Secure Code
Full Transcript
welcome devs today we're going to go
over the eight best practices to
generate code using AI tools I'm going
to use three different examples and I'm
going to use three different AI plugins
codium 8 co-pilot and AI assistant let's
begin number one we're going to break
the request into smaller units of work
AI coding assistants perform best when
handling Focus specific tasks number two
provide context in each ask context
helps AI assistants understand the scope
and purpose of the task number three be
clear and specific AI models perform
better with clear and Specific
Instructions number four keep requests
distinct and focused focused asks reduce
confusion and ensure the assistant does
not mix up tasks let's see these four in
action okay so I'm going to use co-pilot
for this example and this is a small
unit of work we're going to verify
credentials we're being distinct on
saying return success or a failure and
we're being very focused on saying use
bcrypt so we're not just letting the AI
think of how will it do this verifying
credentials we're giving it hints or
telling it this information I'm also
going to give it a context of the user
store to make sure that it knows that it
needs to be used in this
answer and now it comes back with our
answer and or our example code and we
see that and we like it so it's up to
you you're still in the driver's seat I
say that that looks pretty good and so
I'm going to import that in now how do I
know if this is exactly right who's your
first consumer of this code we don't
want to check this in and then say well
let's try to use this this in another
piece that's where our unit tests come
in
so this is the whole key to this is that
your your writing of code and your flow
is now just being enhanced by AI we were
able to get some of the example we were
looking at it we might need to update it
or not and then you get your test so now
this looks like a pretty good test to me
and I'm going to go ahead and just for
our example I'm going to go and copy
this and I'm going to paste it in there
now you can see it gave me the whole
class I could have saved that in there
but for the sake of time
I'm just going to run this and have it
in that file and you can see that we
have our makito going and we have our
behaviors tested number five and six
iterate and refine and leverage previous
conversations and generate code these
two work together where AI generated
code might need refinement iterating
helps improve accuracy by building on
previous interactions if you maintain a
coherent flow let's take a look
so I'm going to use AI assistant to
demonstrate the iterate and refine and
use the already existing generated code
so here is our generated code that we've
did and we're going to rightclick on
that and say find problems now ai
assistant is going to go ahead and spit
out a whole bunch of different
descriptions for us and some changes
that is suggesting so it's saying look
you can check for username and passwords
null and stored information there and it
also has come up with some more
information so here is the final revised
method so you'll see it's iterating over
and saying oh here's some stuff
recommended here's a little bit more and
now here's the final part for the
verified credentials so I have that
verified credentials highlighted and I
can simply go ahead and put that in
there and we've iterated over this so
right now that was the problem that it
found
we can go ahead now and test this and we
know that currently it's going to pass
because we're not checking for any nulls
so in the iterating over this we've seen
that well we probably should check for a
username and password being null and not
even go ahead and do that call up or
check for it so let's go ahead now with
this we can actually create some more
tests so I'm going to go use the AI
assist assistant this time time and
generate unit tests so this open up my
unit test and it will start doing this
diff and generate some tests for us so
as this is
generating we'll go ahead and we're just
waiting
here takes a little bit now you're going
to have to read this and understand it
so you need to get there isn't any kind
of like explanation what behaviors
you're testing so once this is done I
can say accept all and then I will need
to go through this and kind of
understand it and sometimes it's a
little bit off but all you have to do is
kind of clean it up you're still the
driver and the driver seat here to make
sure your code's working so let's go
ahead and test this
out and we see that we've met all the
requirements let's see if we have a null
in there so there's a password null so
that we should be getting a um a
verified of a false and then the
username is false too so it's come up
and um put in some uh good mocking and
even an invalid password or wrong
password check so that is the iterating
and refining and making it just that
much better of a codebase now checking
for the username and
password number seven use Advanced
predefined commands for specific
specific asks many AI coding assistants
offer Advanced commands or settings that
trigger more precise and tailored
responses and number eight ask the
explanation when needed understanding
the generated code enhances your ability
to modify or expand it let's dive deeper
into these points with an example so I'm
going to go ahead and check this whole
method and I'm going to do a prepared
command to say
improve and what this will do is this is
a predefined command one of our best
practices to do this and Cody M's going
to go out and come up with some uh
different suggestions so we can extract
credentials we can use this constant
time and this is the timing attacks that
can happen so that's pretty good for the
security let's go ahead and look at it
and it's using the message digest to do
this so let's go head and select that
one and prepare the code to for the
change and we can show the diff we can
see that we're changing this piece right
here and I'm going to go ahead and apply
that um I have to import it so I import
the class and now we have that improved
now um this will tell you a little bit
more about that and we can do some more
um commands here too so I can you know
select let's just select all of this and
and this is the last best practice which
is the explain so I might want or I'm
new here to looking at this
authorization service and I want to have
a better understanding of what this
authorization service is doing and so
this is going to explain the code for me
and explain everything that we've been
doing here so it says it hashes the
provided password and Compares it to
store to Hash using the is equal um
returns true or false on this um that
checks the username if it exists in the
store all of these different flows that
it's doing and its outputs and um you
can see the references that it's doing
so it actually uh referenced the user
store and the authorization service and
is just giving us some examples of how
to use this too if you wanted to use it
so that's the power of these
predefined commands and also to actually
explain your code base to get a better
understanding before you go about doing
your incremental changes or your
requests to wrap up we've explored some
key best practices for using AI tools to
generate code the main takeaway from
this demo is that code generation has
become a standard capability but the
real focus should be on producing
highquality maintainable code with
strong in Integrity this isn't just
about generating code it's about using
AI throughout the entire development
life cycle to assist you while ensuring
you stay in the driver's seat
Loading video analysis...