[程序员英语口语][Clean code]Chapter1-3 How to name things correctly

Clean code

chapter1 naming

1.3 How to name things correctly

So before we explore any bad or dirty code, Let’s dive right in. How do we name things correctly in our code. Well, let’s differentiate between these three main blocks which I outlined. And Let’s have a look at the different rules.

Variable and constants and, of course, also properties and objects which would be in the same category, are all data containers you could say. we use them to store some data. That could be some data entered by a user.

For example, in a web application. It could be the result of validation the user input, it could be a list of products, a single product. Anything like that.

Here for the names of these variables constants and proterties . We typically wanna use nouns or short phrases with adjectives which describe what’s inside of the data container. For example something like useData or just User.

Something like isValid if we store a boolean. These would be good names. and we are going to see way more examples throughout this section, No worries.

So that’s how we wanna name our variables, and constants and properties. We wanna describe what is being stored in them. Now when it comes to functions and methods, the idea is different one.

Here we don’t store something in that function, I mean there is code inside it, but that code executes once a function is called, and therefore, in the end, function and methods and, of course, commands which we can executes on demand. or we calculate values which we return. So there are commands or calculated values you could say.

For example, we could have a function which sends data to a server. We could have a function or method which validates some user input. And therefore, for function and method names, we typically go with verbs, or again, short phrases with adjectives.

So here we could have something like sendData as a name for the function which sends data to a server.Or inputsValid for the validation function.maybe also just isValid. This would also be okay.

So here we could have the same name as for the variable on the left, because we wanna describe a value which is produced by the function here then. Though I will say the booleans are a special case here.

If we have a function which produces a user object, for example, we might wanna call that function getUser or createUser or something like that. But again, you will see more examples for these different scenario throughout this section.

Now when it comes to naming classes. it’s important to understand and keep in mind that we use classes in our code to create things, to create object.

For example a user object or a product or an HTTP request body which we wanna parse, something like this. And therefore for naming classes, we also use nouns or short phrases with nouns. So simply multiple nouns combined with each other. something like user Or requestBody. Which would be these multiple nouns being combined.

That’s how we wanna use classes. And if you keep these basic rules in mind, you’ve already got a good foundation for naming your code correctly, for naming the things in you code correctly. But of course, we’re now also going to explore the different niche cases, variations, and then also how to pick the best noun for a given value being stored in a variable, for example, because using a noun is just the start. Of course you wanna use the best noun which describes what’s inside a variable.

or what’s inside a function and so on. so that’s , of course, what we’re going to explore throughout this section.

  1. bad or dirty code 脏代码

    I want to refactor this bad code

  2. differentiate between 从…中区分

    How can I differentiate betweet success case and failed case?

  3. outlined 列出大纲

    I outlined a picture for the next iteration

  4. Let’s have a look at

    Let’s have a look at the test.

  5. different scenario

    The tests should cover all the different scenario.

  6. a good foundation

    This section could let you lay a good foundation of coding

Tips:这个单词/短语列表只是一些推荐学习的用词,但是此系列的重点不仅仅在于这些,而在于如何用英文表达一些逻辑,适合积累口语,大家可以仔细品读短文,找到自己喜欢的表达方式。

推荐的学习方法:先听几遍保证能听懂70%,再对照原文进行学习,找到自己喜欢的表达后反复造句让自己加深印象,跟着原文读一遍,然后再自己读一遍。

Source:【整洁代码 Clean Code-哔哩哔哩】 https://b23.tv/skgyGMC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值