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

程序员如何学习更好的知识_成为更好的程序员必须学习的四件事程序员如何学习更好的知识 Take off the coding training wheels 卸下编码训练轮 My partner recently enrolled in a virtual coding boot camp As I watched her tackle her first programming

大家好,我是讯享网,很高兴认识大家。

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

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;
});
小讯
上一篇 2025-04-06 15:24
下一篇 2025-02-15 13:57

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/86567.html