像一名教育者一样思考代码质量
# app/controllers/api/foo/bar/baz_controller.rbdef showend
app/views/api/foo/bar/baz内部寻找show.html.erb或show.jbuilder来作为响应。但如果你不是一个 Rails 开发者... 你不会知道这些!你所看到的只是一个空方法,它似乎什么也没做!更重要的是,你没办法搞明白。答案不是隐藏在一些父类或 mixin 中,而是藏在这种部落知识的书中。https://adamzerner.bearblog.dev/using-obscure-features-of-a-programming-language/?fileGuid=rU8e3yc0h4Mztn6T
for循环写过一个类似的程序。然后,他谈到了一篇论文作者采取的方法:他的解决方案,当然更简洁,是... 非常简洁。非常简洁。我也不知道我能不能读懂它。
几年前,一位著名的科学家曾告诉我,他是如何以比平时低得多的技术水平为自己的领域撰写一篇解释性文章的。他认为这对该领域以外的学者,甚至报道者,都会有用。这篇论文最终成为他所在领域最受欢迎的论文之一,被引用次数超过了他所写的其他任何文章。*
并不是他的同行科学家都很愚蠢,而是我们往往大大低估了正确理解事物所需的努力。*
git blame来增进自己对代码的理解。我会看到大部分代码是谁写的,在 Slack 上交流,然后他们会花费大约 20 分钟时间给我进行大致的讲解。我觉得这非常有用。那么,为什么不像这样记录一份讲解,并在文件头部以代码注释的形式链接到这份讲解呢?

.jpg结尾的 URL// https://example.com/code-images/modal.jpg
// What the heck is 86400000 for?setTimeout(blastOff, 86400000);
// Declare them as capitalized named constants.const MILLISECONDS_IN_A_DAY = 60 * 60 * 24 * 1000; //86400000;setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
const address = "One Infinite Loop, Cupertino 95014";const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;saveCityZipCode(address.match(cityZipCodeRegex)[1],address.match(cityZipCodeRegex)[2]);
const address = "One Infinite Loop, Cupertino 95014";const cityZipCodeRegex = /^[^,\\]+[,\\\s]+(.+?)\s*(\d{5})?$/;const [_, city, zipCode] = address.match(cityZipCodeRegex) || [];saveCityZipCode(city, zipCode);
function emailClients(clients) {clients.forEach(client => {const clientRecord = database.lookup(client);if (clientRecord.isActive()) {email(client);}});}
function emailActiveClients(clients) {clients.filter(isActiveClient).forEach(email);}function isActiveClient(client) {const clientRecord = database.lookup(client);return clientRecord.isActive();}
function addToDate(date, month) {// ...}const date = new Date();// It's hard to tell from the function name what is addedaddToDate(date, 1);
function addMonthToDate(month, date) {// ...}const date = new Date();addMonthToDate(1, date);
/*** 2016-12-20: Removed monads, didn't understand them (RM)* 2016-10-01: Improved using special monads (JP)* 2016-02-03: Removed type-checking (LI)* 2015-03-14: Added combine with type-checking (JR)*/function combine(a, b) {return a + b;}
////////////////////////////////////////////////////////////////////////////////// Scope Model Instantiation////////////////////////////////////////////////////////////////////////////////$scope.model = {menu: "foo",nav: "bar"};////////////////////////////////////////////////////////////////////////////////// Action setup////////////////////////////////////////////////////////////////////////////////const actions = function() {// ...};
function hashIt(data) {// The hashlet hash = 0;// Length of stringconst length = data.length;// Loop through every character in datafor (let i = 0; i < length; i++) {// Get character code.const char = data.charCodeAt(i);// Make the hashhash = (hash << 5) - hash + char;// Convert to 32-bit integerhash &= hash;}}
我注意到人们在工作中使用解释性注释,而我不会在那些场景中这样做,但我发现这些注释确实很有用;
这种“像教育者一样思考”的框架让我觉得它们很有价值。
问问你自己,是否会有其他人会很难理解你所写的代码
原文链接:
https://adamzerner.bearblog.dev/think-like-an-educator-about-code-quality/?fileGuid=rU8e3yc0h4Mztn6T
逆锋起笔是一个专注于程序员圈子的技术平台,你可以收获最新技术动态、最新内测资格、BAT等大厂大佬的经验、增长自身、学习资料、职业路线、赚钱思维,微信搜索逆锋起笔关注!
评论
