#人工智能
我继续用《A Philosophy of Software Design》里的设计理念总结的skill来重构Rockraft的代码,它这一次没有改代码的实现,只是修改了代码的注释(pr),理由是:improve documentation to explain "why" not "what"。
以其中一段代码注释的前后修改为例:
修改前:
/// Write a log entry to the raft cluster
///
/// This function writes a LogEntry to the raft log and waits for it to be applied.
/// It can only be called on the leader node.
///
/// The `time_ms` field of the entry will be set to the current timestamp before writing.
///
/// # Arguments
/// * `entry` - The LogEntry to write
///
/// # Returns
/// * `Ok(AppliedState)` - The result of applying the log entry
/// * `Err(Error)` - If the operation failed or this node is not the leader
修改后:
/// Write a log entry to the raft log (leader-only)
///
/// Appends the entry to the local log and replicates it to followers.
/// Returns after the entry is committed (majority acknowledgment).
///
/// # Leader Requirement
/// This method **must** only be called when this node is the leader.
/// Callers should use `RaftNode::assume_leader()` to verify leadership first.
///
/// # Timestamp
/// The `time_ms` field is automatically set to the current time before writing.
可以看到,修改前的注释,只是简单列举函数的参数和返回值(重复了函数签名(signature)),而修改后增加了行为的解释和函数的前置要求(需要是leader)。
回到最开始做这个注释重构的动机上,注释、文档应该“explain "why" not "what"”这属于更偏代码审美品位的事情了,没有之前大量的代码训练实践,可能不会具备这样的品味。
而现在,如果AI一开始替代了大量手搓代码的实践,还能通过什么方式积累这样的品味?