正在学习
10.2 Summarizing and Navigating Large Codebases
Before refactoring
import hashlib
import sqlite3
class AuthManager:
def __init__(self, db_path):
self.db_path = db_path
def register_user(self, username, password):
hashed = hashlib.sha256(password.encode()).hexdigest()
conn = sqlite3.connect(self.db_path)
cur = conn.cursor()
cur.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, hashed))
conn.commit()
conn.close()
def login(self, username, password):
conn = sqlite3.connect(self.db_path)
cur = conn.cursor()
cur.execute("SELECT password FROM users WHERE username=?", (username,))
row = cur.fetchone()
conn.close()
if row:
return row[0] == hashlib.sha256(password.encode()).hexdigest()
return False
The code above mixes hashing, data access, and business logic. Let’s prompt Claude to refactor incrementally:
“Claude, refactor this class to separate database operations from authentication logic. Introduce a reusable database utility, improve docstrings, and maintain the same behavior.”
Claude’s refactored result might look like this:
练习题
In the original AuthManager class, what is the purpose of the register_user method?
A. To authenticate a user by checking username and password
B. To insert a new user's username and hashed password into the database
C. To connect to the SQLite database
D. To close the database connection
What is the main issue with the original AuthManager class as mentioned in the source material?
A. It does not use hashing for passwords
B. It lacks proper database connection handling
C. It mixes hashing, data access, and business logic
D. It does not support user registration
Which of the following are steps performed in the login method of the original AuthManager class? (Select all that apply)
A. Connecting to the SQLite database
B. Executing a SELECT query to retrieve the user's hashed password
C. Hashing the provided password using SHA256
D. Inserting a new user into the database
E. Comparing the retrieved hashed password with the newly hashed password
The original AuthManager class uses SHA256 hashing for passwords.
In the original AuthManager class, the ___ method is responsible for adding a new user to the database.
Explain why it is important to separate database operations from authentication logic in the AuthManager class.
What instruction was given to Claude for refactoring the AuthManager class?
A. To add more features to the class
B. To remove all database operations from the class
C. To separate database operations from authentication logic, introduce a reusable database utility, improve docstrings, and maintain the same behavior
D. To rewrite the class in a different programming language
Which of the following are benefits of effective context sharing when collaborating with AI like Claude? (Select all that apply)
A. Reduces duplication of effort
B. Promotes standardization
C. Strengthens engineering culture
D. Increases the complexity of the codebase
E. Improves the accuracy of AI suggestions
In large-scale projects, Claude can process the entire project at once without any context limitations.
Describe a primary technique of effective context management when working with Claude on large projects.
What is the primary issue with the original AuthManager class implementation?
A. It uses SHA-256 hashing which is too slow
B. It mixes hashing, data access, and business logic
C. It doesn't use SQLite for database operations
D. It lacks proper docstrings for all methods
The refactored AuthManager should maintain the same behavior as the original while improving its structure.
Why is it important to separate database operations from authentication logic in the AuthManager class?
登录后解锁笔记、知识点解析、AI 问答
立即登录