程序员如何学习更好的知识_成为更好的程序员必须学习的四件事

程序员如何学习更好的知识

Take off the coding training wheels!

卸下编码训练轮!

My partner recently enrolled in a virtual coding boot camp. As I watched her tackle her first programming lessons, I was surprised to find out how much of what I learned in school did not translate to work.

我的搭档最近参加了一个虚拟的编码训练营。 当我观看她的第一门编程课程时,我很惊讶地发现我在学校学到的很多东西都没有转化为工作。

In this article, we will identify four programming constructs that may have been foundational when you first discover how programming works–but will slow you down on the next leg of your journey.

在本文中,我们将确定四种编程结构,当您初次发现编程的工作原理时,它们可能是基础的,但是会拖慢您下一个旅程的步伐。

取消循环 (Unlearn Loops)

Loops are one of the first constructs you learn as a student of computer programming. A simple while-loop demonstrates the power of automation, where you can repeatedly execute a set of instructions until meeting a condition.

循环是您在计算机编程专业中学习的第一批构造之一。 一个简单的while循环演示了自动化的力量,您可以在其中重复执行一组指令,直到满足条件为止。

You may be tempted to use a while-loop or a for-loop whenever you see a collection of items, but it is rarely the best method.

每当看到项目集合时,您可能都会尝试使用while循环或for循环,但这很少是最好的方法。

const groceries = [
{
name: 'Face Masks',
price: 17.50,
},
{
name: 'Disinfecting Wipes',
price: 24.99,
},
{
name: 'Goggles',
price: 8.99,
},
{
name: 'Gloves',
price: 25.99,
},
{
name: 'Hand Sanitizers',
price: 24.99,
},
];

For example, given an array of objects, where each object represents a grocery item. What would you do if you want to print out the name of each grocery item?

例如,给定一个对象数组,其中每个对象代表一个杂货。 如果要打印每个杂货项目的名称,该怎么办?

let index = 0;while (index < groceries.length) {
console.log(groceries[index].name);
index = index + 1;
}

This while-loop certainly achieves what you want, but you have to keep track of an index to reach into the array of groceries each time you want to grab an item. There is also the risk of entering an infinite loop if you forget to increment your index.

这种while循环当然可以实现所需的功能,但是每次要抓取一件物品时,都必须跟踪索引以进入杂货数组。 如果您忘记增加索引,也有进入无限循环的风险。

Consider this alternative approach:

考虑以下替代方法:

groceries.forEach((item) => {
console.log(item.name);
});

forEach is a higher-order function that takes in another function as an argument and executes the provided function once for each element in the array. By leveraging forEach, we remove the extraneous code for tracking and accessing array using an index, and zoom in on the meat of our logic–print out the name of each grocery item.

forEach是一个高阶函数 ,它将另一个函数作为参数,并对数组中的每个元素执行一次提供的函数。 通过forEach ,我们删除了使用索引来跟踪和访问数组的多余代码,并放大了逻辑的内容-打印出每个杂货项目的名称。

While and for loops are generally too broad to capture the intention of our code. If you want to write better code, start by replacing your loops with functions with more specificity.

while和for循环通常太宽泛,无法捕获我们的代码的意图。 如果要编写更好的代码,请首先使用功能更明确的函数替换循环。

Below are a few more related examples for mutating a list and summarizing a list.

以下是一些有关使列表变异和汇总列表的相关示例。

// Before:let index = 0;
const prices = [];while (index < groceries.length) {
prices.push(groceries[index].price);
index = index + 1;
}// After:groceries.map((item) => {
return item.price;
});
// Before:let index = 0;
let total = 0;while (index < groceries.length) {
total = total + groceries[index].price;
index = index + 1;
}// After:groceries.reduce((sum, item) => {
return sum += item.price;
}, 0);

取消学习条件 (Unlearn Conditionals)

Every time you add an else statement, you increase the complexity of your code by two-fold. Conditional constructs like if-else and switch statements are foundational blocks in the world of programming. But they can also get in the way when you want to write clean, extensible code.

每次添加else语句时,代码的复杂度都会增加两倍。 if-else和switch语句之类的条件构造是编程领域的基础块。 但是,当您要编写简洁,可扩展的代码时,它们也会妨碍您的工作。

Fortunately, there are a few tricks to get rid of conditionals.

幸运的是,有一些技巧可以摆脱条件限制。

数据结构 (Data Structures)

Consider the following function for calculating discount:

考虑以下用于计算折扣的函数:

const discount = (amount, code) => {
switch (code) {
case 'DIJFNC':
return amount * 0.80;
case 'XPFJVM':
return amount * 0.75;
case 'FJDPCX':
return amount * 0.50;
}
};

You have to add a new case in the switch statement each time you want to add a new discount code. And if you make a mistake–you break the whole calculation.

每次要添加新的折扣代码时,都必须在switch语句中添加新的案例。 而且,如果您犯了一个错误,则会破坏整个计算。

Now let’s replace the conditional with an object.

现在让我们将条件替换为对象。

const DISCOUNT_MULTIPLIER = {
'DIJFNC': 0.80,
'XPFJVM': 0.75,
'FJDPCX': 0.50,
};const discount = (amount, code) => {
return amount * DISCOUNT_MULTIPLIER[code];
};

This rewrite effectively decouples the data from the core calculation logic, which makes it easier to modify either independently.

这种重写有效地将数据与核心计算逻辑解耦,这使得更容易独立修改它们。

多态性 (Polymorphism)

The second way to replace conditionals is by leveraging a key feature of object-oriented programming languages–polymorphism.

替换条件的第二种方法是利用面向对象编程语言的一个关键功能-多态。

const checkout = (amount, paymentMethod) => {
switch (paymentMethod) {
case 'credit-card':
// Complex code to charge ${amount} to the credit card.
break;
case 'debit-card':
// Complex code to charge ${amount} to the debit card.
break;
case 'cash':
// Complex code to put ${amount} into the cash drawer.
break;
}
};const customers = [
{
amount: 75.00,
paymentMethod: 'credit-card',
},
{
amount: 50.00,
paymentMethod: 'debit-card',
},
{
amount: 25.00,
paymentMethod: 'cash',
},
];customers.forEach(({ amount, paymentMethod }) => {
checkout(amount, paymentMethod);
});

The lines of code for handling various payment methods interspersed with the switch statement makes the code hard to read. And to add insult to injury, any time you want to modify the logic for a specific payment method, you risk breaking the other two because they all live inside the same function.

用于处理各种付款方式的代码行散布在switch语句中,使代码难以阅读。 而且,如果要修改特定付款方式的逻辑,还会给人带来伤害,这是冒着破坏其他两个风险的风险,因为它们都属于同一功能。

class CreditCardCheckout {
static charge(amount) {
// Complex code to charge ${amount} to the credit card.
}
}class DebitCardCheckout {
static charge(amount) {
// Complex code to charge ${amount} to the debit card.
}
}class CashCheckout {
static charge(amount) {
// Complex code to put ${amount} into the cash drawer.
}
}const customers = [
{
amount: 75.00,
paymentMethod: CreditCardCheckout,
},
{
amount: 50.00,
paymentMethod: DebitCardCheckout,
},
{
amount: 25.00,
paymentMethod: CashCheckout,
},
];customers.forEach(({ amount, paymentMethod}) => {
paymentMethod.charge(amount);
});

Polymorphism helps us break up the lengthy switch statement. Each class is responsible for precisely one payment method.

多态有助于我们打破冗长的switch语句。 每个班级仅负责一种付款方式。

One thing to note here is that even though we successfully remove the conditional statement from our refactored code, in reality, we merely shift the decision of choosing the payment method upstream. Whoever creates customers is now responsible for assigning payment method classes.

这里要注意的一件事是,即使我们成功地从重构代码中删除了条件语句,实际上,我们只是将选择付款方式的决定移到了上游。 创建客户的人现在负责分配付款方式类。

取消学习文字变量名称 (Unlearn Literal Variable Names)

Programming tutorials are littered with lousy variable and function names partly because the code examples often do not need to provide the full context to illustrate whatever point it is trying to explain. For example:

编程教程中充斥着糟糕的变量和函数名,部分原因是代码示例通常不需要提供完整的上下文来说明其试图解释的任何要点。 例如:

const arr = [
'Breakfast Cereal',
'Candy and Snack',
'Dairy',
'Paper Products and Cleaning Supplies',
];const func = (n) => {
const i = arr.findIndex(i => i === n);
console.log(i);
};func('Dairy');

This code block does a fine job of demonstrating the usage of findIndex but is utterly inappropriate on a real project. The variable names tell us nothing about why they exist. It is hard for the reader and even harder to modify when we have no clues about what purposes they serve.

该代码块很好地演示了findIndex的用法,但完全不适用于实际项目。 变量名称没有告诉我们它们为什么存在。 当我们对他们的服务目的一无所知时,对读者来说很难,甚至更难修改。

To write better names, we have to start with knowing the context. There is a list of common grocery store aisle names. The aisle names are ordered in a meaningful way so that when we call the function with a given aisle name, we will return its position on this list, representing the aisle number. We may rewrite the code as follows:

要写出更好的名字,我们必须从了解上下文开始。 这里有一个常见的杂货店过道名称列表。 过道名称以有意义的方式排序,以便当我们使用给定的过道名称调用该函数时,我们将返回其在此列表中的位置,代表过道编号。 我们可以如下重写代码:

const aisles = [
'Breakfast Cereal',
'Candy and Snack',
'Dairy',
'Paper Products and Cleaning Supplies',
];const printAisleNumber = (name) => {
const number = aisles.findIndex((aisleName) => {
return aisleName === name;
}); console.log(number);
};printAisleNumber('Dairy');

When you come up with the right variable name, it shows that you truly grasp the context of the problem your code solves. Purposeful variable names will elevate your code from mere computational instructions to a user guide that assists the readers in learning about your work.

当您提出正确的变量名称时,它表明您真正掌握了代码要解决的问题的上下文。 有目的的变量名将使您的代码从单纯的计算指令升级为用户指南,以帮助读者了解您的工作。

取消了解全球范围 (Unlearn Global Scope)

When you first pick up programming, you likely start from a simple hello world program. And from there, you learn to write code in a single file and watch the program execute your code line by line from top to bottom. You never suspect that a variable you declared at the beginning of the file will become unavailable somewhere else.

第一次学习编程时,您可能会从一个简单的hello world程序开始。 然后,您将学习在单个文件中编写代码,并观察程序从上到下逐行执行代码。 您永远不会怀疑在文件开头声明的变量在其他地方不可用。

This idea of everything you write lives inside a global space accessible to all–it is an obstacle to working effectively with abstractions. You have no impetus to create abstractions that encapsulate your code.

您编写的所有内容的想法都存在于所有人都可以访问的全局空间中,这是有效使用抽象的障碍。 您没有动力创建封装代码的抽象。

My advice would be to forget about the global scope. Treat every function, object, and class as a new universe. Focus on how to create abstracts to represent your ideas and how those ideas interact with one another.

我的建议是忘记全球范围。 将每个函数,对象和类都视为一个新的Universe。 专注于如何创建摘要来表示您的想法以及这些想法如何相互影响。

TL; DR (TL;DR)

  • Replace loops with higher-order functions

    高阶函数替换循环

  • Replace conditionals with data structures and polymorphism

    数据结构多态替换条件

  • Replace literal variable names with purposeful variable names

    用有目的的变量名替换文字 变量名

  • Forget global scope exists

    忘记全球范围的存在

升级编码 (Level Up Coding)

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程

翻译自: https://levelup.gitconnected.com/4-things-you-have-to-unlearn-to-become-a-better-programmer-547adf476445

程序员如何学习更好的知识

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值