Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Planning a Refactor

00:00 It’s time to start implementing your new features in your Python project, so go ahead and very briefly run the command uv run rpcontacts to open the interface of your contact book.

00:13 The buttons Delete and Clear All do not work. If you try highlighting one of the contacts and pressing the button Delete or using the keyboard shortcut D, nothing happens.

00:25 And similarly, if you press the button C to clear all contacts or if you click the button, nothing happens. So that’s what you’re going to use OpenCode for.

00:35 So go ahead, press Q to exit the application, confirm you want to quit, and now run OpenCode with the command opencode at the root of your project.

00:47 Once you’re in, you’ll want to press Tab once to switch to Plan mode or alternatively just use the forward slash command /agent,

00:58 which allows you to switch between the Build and the Plan mode. Select Plan mode and now ask for what you want, which in this case is to implement the behavior of the button Delete, which should delete the highlighted row, and the button Clear All, which should clear all the existing rows.

01:28 And you can press Enter to submit. The reason you’re going for Plan mode is because in general, models will perform better if they have a plan beforehand.

01:40 So forcing the model to create a plan will give you a document that you can review and that’s going to outline the changes that the model determines need to be done.

01:52 Using the Plan mode also gives you touch points in which you can interact with the model and help steer the model in the right direction. If the model was building right away, the model might make choices, implementation choices, design choices, assumptions that are wrong or suboptimal.

02:11 And by having a plan, first you can look at those assumptions, at those decisions and tweak them to your liking or in the direction that you prefer. And then you can switch to Build mode and have the model actually implement the changes.

02:26 Now, once the model is done with the plan, you’ll want to scroll up and read through the plan. Now, sometimes you might be using OpenCode to write something, to implement a feature, to create a project that you don’t fully grok, and that’s okay. But it’s always a good idea to read the plan because even if you don’t fully understand what’s going to be built, reading the plan is generally a good idea.

02:52 It’s going to help you become more acquainted with the project that you’re working on and it will also give you the opportunity to spot things that don’t look quite right.

03:04 So go ahead, take a look at the plan that’s generated by OpenCode for you. It’s not going to be 100% equal to the one that’s on the screen. And if you don’t like the plan, you can ask for further changes.

03:18 You can tweak the plan. For example, you can ask what keys, of course, this particular question makes sense in the face of this particular plan for you. It may not make sense, but you can ask further questions.

03:31 You could say, What keys will be bound to the two buttons?

03:38 Just make sure that the model picked those correctly. And then the model replied that based on the bindings, the Delete button will be bound to the D key and the Clear All button will be bound to the C key.

03:49 Now, strictly speaking, this isn’t something that the model needed to implement because it’s more or less already wired in. But since it wasn’t clear from the plan, it’s a valid question for you to ask.

04:01 What keys are going to be used? How do you set up those keys or those key bindings, rather? Make sure the plan looks sensible. And if it makes sense, you can move on to the next lesson where you’re going to actually implement the feature.

Become a Member to join the conversation.