Code Refactoring
Use when someone wants working code made clearer without changing what it does. Shows every rule before and after, checks the results match and splits the work into steps that can be undone.
Ce que fait cette compétence
Les instructions complètes que suit votre agent. Rien ici n’est caché derrière une connexion.
Refactoring
Make working code easier to read, and show that every result stays the same.
When to use
Use it to simplify, rename, split up or tidy code that works. Use code-review to decide whether to merge a change, debugging to fix a failure, and test-generation to write tests. If the user wants different results, say so and keep the two changes separate.
How to work
- List everything the code does: rules, limits, rounding, the order of calculations, errors, and anything it reads or changes outside itself. Give each rule a letter: A, B, C…
- Keep public names, parameters, return types and errors. If you can't see every caller, don't add names that code outside could see. Keep odd code if its reason isn't clear, and ask about it.
- Make the smallest change that helps: flatter branches, clearer names, one copy of a repeated rule. Keep the user's style. Don't add future features or a helper for a single use. Keep the same arithmetic:
t - t * 0.1andt * 0.9can give different results. - Work out every result in the old and new code: each example, both sides of each limit, empty and missing input, errors and effects outside the code. Undo any change that gives a different result.
- Choose the first verdict that fits. Leave as is: no change makes it clearer, or a result must change. Refactor after adding tests: no automated tests were supplied. Refactor: anything else.
The result
Always use these sections, in this order. If one is empty, say why.
- Cover: the verdict and why. Big number: Same checks out of all, such as "17/17". Facts: what got simpler, "Not run" if so.
- Before and after: the main visual:
e-bawith ane-codein each half. Show each rule's code in both, with its letter in<i>on its first line. Put one count in both<h3>s, such as the deepest nesting (each loop,iforelseinside another adds 1). Break lines at 30 characters, only where the code means the same. Below, list each letter and its rule. For Leave as is, put the code on the left and why it stays on the right. - Scope: a list: what changes, what doesn't and every known caller.
- Behavior check: a 3-column table: case, old and new result, and Same, Can't tell or Changed ("By hand" or "Run" in an
<em>). Same: both results match. Can't tell: the result depends on something you can't see. Changed: they differ. None may be Changed. - New code: the whole replacement in one
e-codeper file, with nothing left out. For Leave as is, write "No change proposed" here and in section 6. - Small steps: a 3-column table of 2–6 steps: change, check, undo. The code must work after each one. If there are no tests, adding them comes first.
- Found, not changed: a list. Suspected bugs first: the line, what happens now and what to ask. Then up to 3 ideas. Strong: the code shows the problem now. Worth exploring: needs more facts. Speculative: helps only after a future change.
- Not checked: a list.
Build the page
Save one .html file (under 7,000 characters) with save_output. Copy the markup below with every part (leads and sources too), changing only text and numbers. Plain <p>, <br>, <ul> and <ol> are fine, but nothing else: no CSS, scripts, images, fonts or outside links. Keep <style> as is: endue adds its design there. Set lang and write every heading, label, status and verdict, even those named above, in that language only. In text and code, write < for < and & for &.
If the task sets another format, use it with the same sections in the same order. If you can't save files, give them in chat. After saving, reply with only the conclusion and what needs attention.
<!doctype html><html lang="ko"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Title</title><style>/*endue-kit*/</style></head><body><main class="e">
<header class="e-cover"><p>topic · date</p><h1>Conclusion, up to 60 characters (30 in Korean or Japanese)</h1><p>The main reason</p><ul><li>key fact</li></ul><div class="e-big"><b>12%</b><span>what it measures</span></div></header>
<section><h2>Heading</h2><p class="e-lead">The point in one sentence</p><!-- blocks --></section>
<footer class="e-src"><b>Sources</b><ol><li>source, date</li></ol></footer>
</main></body></html>
- The big number proves the conclusion in up to 12 characters. Without one, leave out
e-big. - Variables take plain numbers (
1240or0.5, not1,240).--maxis the block's largest value. Show every value as text too. Counts in sentences must match the blocks. - Put
hionly on what proves the block's point. Usegood,warnandbadonly when clear, with a word beside them. Say what each change is compared with.
Blocks to use here:
- Before and after:
<div class="e-ba"><div><h3>Before</h3><p>text</p></div><div><h3>After</h3><p>text</p></div></div> - Code:
<pre class="e-code"><code>18 kept</code><code><del>19 removed</del></code><code><ins><i>1</i>19 added</ins></code></pre>. Each line gets its own<code>. Use<del>,<ins>, or<mark>for a line to look at.<i>1</i>at the start of a line is a marker in the margin, not its line number. - Table, up to 5 columns:
<div class="e-table"><table><thead><tr><th>Task</th><th class="n">Cost</th></tr></thead><tbody><tr class="hi"><td>text</td><td class="n">1,200</td></tr><tr class="total"><td>Total</td><td class="n">1,200</td></tr></tbody></table></div>.nis for numbers.<em>in a cell adds a small line below.
Before you finish
- Every letter is in both halves and the list below.
- Every example and limit is in the behavior check, and the count on the cover matches.
- The new code is complete and has the same names, parameters and errors.
Never
- Fix a suspected bug.
- Claim the code is faster, or that fewer lines make it better.
- Say you ran code or tests that you didn't, or invent a caller.
Après votre inscription, ajoutez Code Refactoring à votre agent en un clic.