Week Three: Modernize Your Legacy Code with GitHub Copilot [Copilot Skills Challenge] #186361
-
Welcome to part three of the GitHub Copilot Skills Series! This week, we'll explore how GitHub Copilot can help you breathe new life into legacy codebases, making them more maintainable, efficient, and aligned with modern best practices. Missed the previous weeks? Check out Part One and Part Two, then join us back here. Step One: Skills Exercise 💪To begin, complete the Modernize Your Legacy Code with GitHub Copilot exercise where you'll learn how to:
Then, come back for part two to deepen your learning with our challenge and be entered to win a GitHub Shop Voucher! Step Two: Community Challenge 🧠Answer these multiple choice questions in the comments section: Question One: When modernizing legacy code with GitHub Copilot, what should be your first priority? Question Two: GitHub Copilot suggests converting a legacy callback-based function to use async/await. What factors should you consider before accepting this suggestion? Question Three: You're refactoring a 500-line function with GitHub Copilot's help. What's the recommended approach? Question Four: When GitHub Copilot suggests replacing deprecated methods in your legacy code, what should you verify first? Question Five: How can GitHub Copilot best assist with improving legacy code documentation? 🎯 Community Engagement**Share Your Transformation! ** 🚀 We want to see your legacy code transformations! In the comments below, share:
The most impressive transformation will receive special recognition in next week's post! We'll be sharing the answers in the comments on Friday 🧠 Use the discussion below to share additional resources, ask questions for our team to answer, respond to the challenge, and showcase your before/after code transformations. **No Purchase Necessary. Open only to Github community members 14+. Game ends February 17, 2026. For details, see Official rules. |
Beta Was this translation helpful? Give feedback.
Replies: 22 comments 3 replies
-
|
Missed Week 2? Check that out here! Week Two: Copilot Code Review [Copilot Skills Challenge] |
Beta Was this translation helpful? Give feedback.
-
Answers to Community Challenge Questions1: b) Understanding the existing functionality and creating tests before refactoring When modernizing legacy code, the priority is to ensure behavior does not unintentionally change, which requires first understanding what the code does and capturing that behavior in tests before refactoring. Before converting callbacks to async/await, we must check that the rest of the codebase supports async/await patterns, that error handling still behaves correctly, and that the minimum runtime version supports these features, so all listed factors matter. Refactoring a large legacy function works best by extracting smaller, single‑responsibility, testable functions, which improves maintainability and lets Copilot assist in safer, incremental changes. 4: a) That the new methods are backwards compatible with your deployment environment When replacing deprecated methods, we must first ensure the proposed alternatives are supported and behave correctly in the deployment environment so we do not introduce runtime issues. Copilot can best help documentation by generating richer docstrings, inline comments, and higher‑level docs like README updates that align with the newly refactored code, rather than just removing or deferring documentation work. |
Beta Was this translation helpful? Give feedback.
-
|
Answers: Question One: Question Two: Question Three: Question Four: Question Five: |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
My Legacy Code Modernization Journey with GitHub CopilotModernizing legacy code changed my mindset completely. ✅ Key Learnings from Each QuestionQ1: First Priority in Modernizing Legacy CodeCorrect Answer: (b) I learned that without understanding the current behavior, changes can easily break production features. Q2: Converting Callbacks to async/awaitCorrect Answer: (d) All of the above Before accepting Copilot’s suggestion, I must consider:
Modern syntax is helpful — but only when it fits the environment. Q3: Refactoring a 500-Line FunctionCorrect Answer: (b) Initially, I thought rewriting everything at once was efficient. Q4: Replacing Deprecated MethodsCorrect Answer: (a) If production doesn’t support it, modernization becomes a problem instead of an improvement. Q5: Improving DocumentationCorrect Answer: (b) Good documentation improves collaboration and long-term maintainability. 🔄 Before
✨ After
🎯 Impact
💡 Final InsightModernizing legacy code is not about rewriting everything. |
Beta Was this translation helpful? Give feedback.
-
My Legacy Code Modernization with GitHub Copilot (Nuxt Ads)I modernized my Nuxt Ads project (a Nuxt-based ad server) by using GitHub Copilot and AI agents to refactor the codebase toward a cleaner, more hexagonal-style architecture, improve maintainability, and add tests. Before: Legacy Nuxt Ads CodeIn the
Process: How I Used GitHub Copilot and AgentsI approached the modernization as a guided, multi-phase refactor, with Copilot and a few AI agents embedded into the workflow:
After: Modernized Hexagonal-Style Nuxt AdsIn the
Impact:
Supporting ArtifactsThe ASCII diagram below shows a concise before/after snapshot of the repository layout. It is included to illustrate the key structural changes introduced by the refactor. Test result screenshot:
|
Beta Was this translation helpful? Give feedback.
-
|
Question One: b) of course 😅 |
Beta Was this translation helpful? Give feedback.
-
Answers to the Community ChallengeQuestion One: Question Two: Question Three: Question Four: Question Five: 🔄 Legacy Code Modernization – Node.js ProjectBefore
After (with GitHub Copilot)
Impact
GitHub Copilot was especially helpful for incremental refactoring, identifying outdated Node.js patterns, migrating async code safely, and generating documentation aligned with the modernized codebase. Modernized using GitHub Copilot in VS Code on a Node.js project. |
Beta Was this translation helpful? Give feedback.
-
|
Entiendo muy poco, empiezo el aprendizaje |
Beta Was this translation helpful? Give feedback.
-
|
=== Before (main-before-refactor-2026-02-05) === nuxt-ads/ === After (main) === nuxt-ads/ |
Beta Was this translation helpful? Give feedback.
-
|
Week 3 Answers:
Before (legacy snippet, sanitized)function getActivities(userId, callback) {
if (!userId) return callback("missing userId");
db.findUser(userId, function (err, user) {
if (err) return callback(err);
db.findRegistrations(user.id, function (err2, regs) {
if (err2) return callback(err2);
const active = regs.filter(function (r) {
return r.status === "active";
});
callback(null, active);
});
});
}After (modernized with Copilot)async function getActivities(userId) {
if (!userId) throw new Error("missing userId");
const user = await db.findUser(userId);
const registrations = await db.findRegistrations(user.id);
return registrations.filter((r) => r.status === "active");
}Impact
|
Beta Was this translation helpful? Give feedback.
-
|
b) Understanding the existing functionality and creating tests before refactoring |
Beta Was this translation helpful? Give feedback.
-
|
week 3 challange: |
Beta Was this translation helpful? Give feedback.
-
|
Answer 1: b) Understanding the existing functionality and creating tests before refactoring |
Beta Was this translation helpful? Give feedback.
-
|
When will week 4 is going to out. And if it is live then please share the link with me |
Beta Was this translation helpful? Give feedback.
-
|
Question One: When modernizing legacy code with GitHub Copilot, what should be your first priority? Question Two: GitHub Copilot suggests converting a legacy callback-based function to use async/await. What factors should you consider before accepting this suggestion? Question Three: You're refactoring a 500-line function with GitHub Copilot's help. What's the recommended approach? Question Four: When GitHub Copilot suggests replacing deprecated methods in your legacy code, what should you verify first? Question Five: How can GitHub Copilot best assist with improving legacy code documentation? |
Beta Was this translation helpful? Give feedback.
-
|
Week Four is live - our last week, let's finish strong 💪🏼 Join the discussion to get this week's Skills course and challenge
|
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
|
Question One: b) Understanding the existing functionality and creating tests before refactoring Question Two: d) All of the above Question Three: b) Break it down into smaller, testable functions with single responsibilities Question Four: a) That the new methods are backwards compatible with your deployment environment Question Five: b) By generating comprehensive docstrings, inline comments, and updating README files based on the refactored code Legacy Code Transformation: COBOL to Node.jsBefore: COBOL Legacy Code (1960s-era)
IDENTIFICATION DIVISION.
PROGRAM-ID. Operations.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 OPERATION-TYPE PIC X(6).
01 AMOUNT PIC 9(6)V99.
01 FINAL-BALANCE PIC 9(6)V99 VALUE 1000.00.
LINKAGE SECTION.
01 PASSED-OPERATION PIC X(6).
PROCEDURE DIVISION USING PASSED-OPERATION.
MOVE PASSED-OPERATION TO OPERATION-TYPE
IF OPERATION-TYPE = 'DEBIT '
DISPLAY "Enter debit amount: "
ACCEPT AMOUNT
CALL 'DataProgram' USING 'READ', FINAL-BALANCE
IF FINAL-BALANCE >= AMOUNT
SUBTRACT AMOUNT FROM FINAL-BALANCE
CALL 'DataProgram' USING 'WRITE', FINAL-BALANCE
DISPLAY "Amount debited. New balance: " FINAL-BALANCE
ELSE
DISPLAY "Insufficient funds for this debit."
END-IF
END-IF
GOBACK.After: Modern Node.js with Async/Await
/**
* Operations - Business Logic Layer
* Converted from operations.cob
*/
class Operations {
constructor(dataProgram) {
this.dataProgram = dataProgram;
this.rl = null;
}
/**
* Debit Account Operation
* Withdraws funds from account with insufficient funds validation
* @returns {Promise<void>}
*/
async debitAccount() {
return new Promise((resolve) => {
this.rl.question('Enter debit amount: ', (input) => {
const amount = parseFloat(input);
if (isNaN(amount)) {
console.log('Invalid amount. Please enter a numeric value.');
resolve();
return;
}
// Read current balance
const currentBalance = this.dataProgram.read();
// Business Rule: Check for sufficient funds
if (currentBalance >= amount) {
// Calculate new balance
const newBalance = currentBalance - amount;
// Write new balance
this.dataProgram.write(newBalance);
console.log(`Amount debited. New balance: ${this.formatBalance(newBalance)}`);
} else {
// Insufficient funds - transaction rejected
console.log('Insufficient funds for this debit.');
}
resolve();
});
});
}
formatBalance(balance) {
return balance.toFixed(2).padStart(9, '0');
}
}Impact: Measurable ImprovementsReadability
Testability
Maintainability
Developer Experience
Error Handling
Documentation
Business Continuity
Key Metrics Summary
|
Beta Was this translation helpful? Give feedback.
-
🐞 Bug Buster's Legacy Code Modernization SubmissionQuiz AnswersQuestion 1: b) Understanding the existing functionality and creating tests before refactoring 🚀 My Transformation: Authentication System ModernizationBEFORE: Legacy Callback-Based Auth (9 Critical Bugs) // Legacy callback-based authentication
function authenticateUser(username, password, callback) {
var user = null;
// 🐞 SQL Injection vulnerability
db.query("SELECT * FROM users WHERE username = '" + username + "'", function(err, results) {
// 🐞 No error handling
user = results[0];
// 🐞 No null check - crashes if user not found
// 🐞 Weak comparison ==
if (user.password == password) {
callback(user); // 🐞 No error parameter
} else {
callback(null); // 🐞 Silent failure
}
});
}
// 🐞 Callback hell - unmaintainable
authenticateUser(req.body.username, req.body.password, function(user) {
if (user) {
getUserProfile(user.id, function(profile) {
getRecentActivity(user.id, function(activity) {
sendResponse(user, profile, activity);
});
});
}
}); <html>
<body>
<!--StartFragment--><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="box-sizing: border-box; font-weight: 600;">FTER: Modernized with Bug Buster + Copilot</strong></p><figure class="CodeBlock-module__container___ry3V CodeBlock-module__immersive__Gmg6o" aria-labelledby="_r_1np_" style="box-sizing: border-box; display: block; margin: 0px 0px 16px; --header-height: 3rem; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div class="CodeBlock-module__header__tJzrz" style="box-sizing: border-box; background-color: rgb(21, 27, 35); height: 48px; padding-left: 16px; padding-right: 40px; align-items: center; gap: 4px; color: rgb(145, 152, 161); border-radius: 12px 12px 0px 0px; border-color: rgba(61, 68, 77, 0.7); border-style: solid; border-width: 0.8px; border-image: none 100% / 1 / 0 stretch; display: flex;"><span class="LanguageDot-module__languageDot__WZEoH" style="box-sizing: border-box; background-color: rgb(241, 224, 90); border-radius: 9999.01px; border-color: rgba(255, 255, 255, 0.15); border-style: solid; border-width: 0.8px; border-image: none 100% / 1 / 0 stretch; width: 10px; height: 10px; display: inline-block;"></span><span id="_r_1np_" class="CodeBlock-module__languageName__iQIeg" style="box-sizing: border-box; margin-left: 4px; font-size: 14px; flex-grow: 1;">JavaScript</span></div><div class="CodeBlock-module__copyContainer__eNjNy" style="box-sizing: border-box; top: 48px; position: sticky;"><div class="CodeBlock-module__copyContent__qV51h" style="box-sizing: border-box; right: 8px; height: 48px; align-items: center; display: flex; position: absolute; bottom: 0px;"></div></div><div class="CodeBlock-module__codeContainer___2Otq" style="box-sizing: border-box; background-color: rgb(13, 17, 23); border-color: rgba(61, 68, 77, 0.7); border-style: solid; border-width: 0px 0.8px 0.8px; border-image: none 100% / 1 / 0 stretch; border-radius: 0px 0px 12px 12px; overflow: hidden;"><pre class="CodeBlock-module__code__N23o9" tabindex="0" style="box-sizing: border-box; font-family: "Monaspace Neon", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; font-size: 13.6px; margin: 0px !important; tab-size: 4; overflow-wrap: normal; padding: 16px 24px !important; color: rgb(240, 246, 252); background: 0px 0px !important; border-radius: 0px !important; line-height: 1.45; overflow: auto;"><code class="" style="box-sizing: border-box; font-family: "Monaspace Neon", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace; font-size: 14px; tab-size: 4; white-space: pre; background: 0px 0px rgba(0, 0, 0, 0); border-radius: 6px; margin: 0px; padding: 0px; word-break: normal; border: 0px; line-height: inherit; overflow-wrap: normal; display: inline; overflow: visible;"><span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">/**
* Authenticate user with modern async/await pattern
* <span class="hljs-doctag" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">@param</span> {<span class="hljs-type" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">string</span>} <span class="hljs-variable" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">username</span> - User's username
* <span class="hljs-doctag" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">@param</span> {<span class="hljs-type" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">string</span>} <span class="hljs-variable" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">password</span> - User's password
* <span class="hljs-doctag" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">@returns</span> {<span class="hljs-type" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">Promise<Object></span>} Authenticated user object
* <span class="hljs-doctag" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">@throws</span> {<span class="hljs-type" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">ValidationError|AuthenticationError|DatabaseError</span>}
*/</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">async</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">function</span> <span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">authenticateUser</span>(<span class="hljs-params" node="[object Object]" style="box-sizing: border-box;">username, password</span>) {
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Input validation</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (!username || <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">typeof</span> username !== <span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'string'</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">new</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">ValidationError</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Username must be a non-empty string'</span>);
}
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (!password || <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">typeof</span> password !== <span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'string'</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">new</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">ValidationError</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Password must be a non-empty string'</span>);
}
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">try</span> {
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Parameterized query prevents SQL injection</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> results = <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">await</span> db.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">query</span>(
<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'SELECT id, username, email, password_hash FROM users WHERE username = ?'</span>,
[username.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">toLowerCase</span>().<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">trim</span>()]
);
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Explicit null check</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (!results || results.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">length</span> === <span class="hljs-number" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">0</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">new</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">AuthenticationError</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Invalid username or password'</span>);
}
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> user = results[<span class="hljs-number" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">0</span>];
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Secure password hashing with bcrypt</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> isValidPassword = <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">await</span> bcrypt.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">compare</span>(password, user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">password_hash</span>);
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Strict comparison</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (isValidPassword !== <span class="hljs-literal" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">true</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">new</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">AuthenticationError</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Invalid username or password'</span>);
}
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Return user without sensitive data</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">return</span> {
<span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">id</span>: user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">id</span>,
<span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">username</span>: user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">username</span>,
<span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">email</span>: user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">email</span>
};
} <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">catch</span> (error) {
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Proper error handling with context</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (error <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">instanceof</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">AuthenticationError</span> || error <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">instanceof</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">ValidationError</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> error;
}
<span class="hljs-variable language_" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">console</span>.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">error</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Authentication error:'</span>, error.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">message</span>);
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">throw</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">new</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">DatabaseError</span>(<span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Authentication failed'</span>);
}
}
<span class="hljs-comment" node="[object Object]" style="box-sizing: border-box; color: rgb(145, 152, 161);">// ✅ Clean async/await (no callback hell)</span>
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">async</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">function</span> <span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">handleLogin</span>(<span class="hljs-params" node="[object Object]" style="box-sizing: border-box;">req, res</span>) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">try</span> {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> user = <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">await</span> <span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">authenticateUser</span>(req.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">body</span>.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">username</span>, req.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">body</span>.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">password</span>);
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> profile = <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">await</span> <span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">getUserProfile</span>(user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">id</span>);
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">const</span> activity = <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">await</span> <span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">getRecentActivity</span>(user.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">id</span>);
res.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">json</span>({ user, profile, activity });
} <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">catch</span> (error) {
<span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (error <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">instanceof</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">ValidationError</span>) {
res.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">status</span>(<span class="hljs-number" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">400</span>).<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">json</span>({ <span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">error</span>: error.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">message</span> });
} <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">else</span> <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">if</span> (error <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">instanceof</span> <span class="hljs-title class_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">AuthenticationError</span>) {
res.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">status</span>(<span class="hljs-number" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">401</span>).<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">json</span>({ <span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">error</span>: error.<span class="hljs-property" node="[object Object]" style="box-sizing: border-box;">message</span> });
} <span class="hljs-keyword" node="[object Object]" style="box-sizing: border-box; color: rgb(255, 123, 114);">else</span> {
res.<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">status</span>(<span class="hljs-number" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">500</span>).<span class="hljs-title function_" node="[object Object]" style="box-sizing: border-box; color: rgb(210, 168, 255);">json</span>({ <span class="hljs-attr" node="[object Object]" style="box-sizing: border-box; color: rgb(121, 192, 255);">error</span>: <span class="hljs-string" node="[object Object]" style="box-sizing: border-box; color: rgb(165, 214, 255);">'Server error'</span> });
}
}
}
</code></pre></div></figure><hr style="box-sizing: border-box; height: 0.25em; overflow: hidden; border: 0px; background: 0px 0px rgb(61, 68, 77); margin: 24px 0px; padding: 0px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><h3 style="box-sizing: border-box; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em; font-weight: 600; line-height: 1.25; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">📊 Impact</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="box-sizing: border-box; font-weight: 600;">🐞 Bugs Fixed:</strong><span> </span>9 critical issues eliminated</p>
Bug | Before | After
-- | -- | --
SQL Injection | Direct string concat | Parameterized queries
Error Handling | None | Try/catch with custom errors
Null Checks | Missing | Explicit validation
Comparison | Weak == | Strict ===
Async Pattern | Callback hell | Clean async/await
Failures | Silent null | Clear error messages
Passwords | Plain text | bcrypt hashing
Input Validation | None | Comprehensive checks
Documentation | None | Full JSDoc
<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="box-sizing: border-box; font-weight: 600;">✅ Security:</strong><span> </span>SQL injection prevented, password hashing, input validation<br style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: 600;">✅ Readability:</strong><span> </span>7 nested callbacks → 4 sequential async calls<br style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: 600;">✅ Maintainability:</strong><span> </span>Single responsibility, clear error types, well-documented<br style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: 600;">✅ Testability:</strong><span> </span>0% → 95% test coverage<br style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: 600;">✅ Debugging:</strong><span> </span>Hours → Minutes (clear error messages)</p><hr style="box-sizing: border-box; height: 0.25em; overflow: hidden; border: 0px; background: 0px 0px rgb(61, 68, 77); margin: 24px 0px; padding: 0px; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><h3 style="box-sizing: border-box; margin-top: 24px; margin-bottom: 16px; font-size: 1.25em; font-weight: 600; line-height: 1.25; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">🐞 Bug Buster Approach</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">I use a<span> </span><strong style="box-sizing: border-box; font-weight: 600;">debugging-first mindset</strong><span> </span>for legacy code modernization:</p><ol style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; padding-left: 2em; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><li style="box-sizing: border-box;"><strong style="box-sizing: border-box; font-weight: 600;">Test Before Refactoring</strong><span> </span>- Capture current behavior, prevent breaking edge cases</li><li style="box-sizing: border-box; margin-top: 0.25em;"><strong style="box-sizing: border-box; font-weight: 600;">Fix Bugs While Modernizing</strong><span> </span>- Don't just update syntax, eliminate bugs</li><li style="box-sizing: border-box; margin-top: 0.25em;"><strong style="box-sizing: border-box; font-weight: 600;">Verify Compatibility</strong><span> </span>- Test on deployment environment first</li><li style="box-sizing: border-box; margin-top: 0.25em;"><strong style="box-sizing: border-box; font-weight: 600;">Document Everything</strong><span> </span>- Prevent future bugs from misunderstanding</li><li style="box-sizing: border-box; margin-top: 0.25em;"><strong style="box-sizing: border-box; font-weight: 600;">Incremental Changes</strong><span> </span>- Small, tested improvements reduce risk</li></ol><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 16px; color: rgb(240, 246, 252); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(13, 17, 23); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="box-sizing: border-box; font-weight: 600;">Bug Buster Motto:</strong><span> </span><em style="box-sizing: border-box;">"Modernize carefully, test thoroughly, prevent bugs permanently."</em></p><!--EndFragment-->
</body>
</html> |
Beta Was this translation helpful? Give feedback.
-
|
Week three answers: DetailsAnswer KeyQuestion One: When modernizing legacy code with GitHub Copilot, what should be your first priority? Question Two: GitHub Copilot suggests converting a legacy callback-based function to use async/await. What factors should you consider before accepting this suggestion? Question Three: You're refactoring a 500-line function with GitHub Copilot's help. What's the recommended approach? Question Four: When GitHub Copilot suggests replacing deprecated methods in your legacy code, what should you verify first? Question Five: How can GitHub Copilot best assist with improving legacy code documentation? Check out week 4 here: Week Four: Build Applications with Copilot Agent [Copilot Skills Challenge] |
Beta Was this translation helpful? Give feedback.
-
|
Answer Q2: d) All of the above Q3: b) Break it down into smaller, testable functions with single responsibilities Q4: a) That the new methods are backwards compatible with your deployment environment Q5: b) By generating comprehensive docstrings, inline comments, and updating README files based on the refactored code |
Beta Was this translation helpful? Give feedback.






Week Four is live - our last week, let's finish strong 💪🏼 Join the discussion to get this week's Skills course and challenge