Clean code
chapter1 naming
1.5 Naming variables & properties -Theory
So Let’s now dig a bit deeper and let’s start with variables, constants, and properties. How should this be named,?
We can differentiate three main kinds of values which are being stored in them.
Our values could be objects and this includes lists or maps or any other more complex data structure.
Our value could be a simple number or string, or our value could be a boolean.
And you can already tell by the color coding here that there seems to be a difference between boolean and the other kinds of values.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lDp9UCTv-1642644790004)(/Users/fengyiling/Library/Application Support/typora-user-images/image-20220119232400372.png)]
Now if your value, which you store in a variable constant or property is an object or an object to like thing(not sure) then the name which you assigned to your variable or property should simply describe the value.
So if you are storing a user object in your variable, name the variable user
. If you are storing an object which describes a database and offers database operations, you might wanna name the variable database
.
If your value is a number or a string, so some text, then you also just wanna describe the value. If you have a variable that holds the name of a user, you might just wanna call it name
.
The same of course if it’s a property in a user object. If you have a variable or property which holds an age, you might wanna call it age
, just like that.
Now for booleans, there’s a special case. For booleans your name should simply be a question that can be answered with true or false.
So you might name you variables something likeisActive
or loggedIn
, because that can be yes or no and therefore true or false.
You are active, or you are not. it’s an either/or choice.
So for boolean, we want this is something naming or names like loggedIn
, which also can be answered with yes or no.
Now you might wanna consider providing more details and more information where it makes sense but you don’t wanna introduce extra redundancy.
For example, if your user in a certain branch of your code is always an authenticated user, you could consider naming that variable authenticatedUser
to make it really clear to everyone who reads that code that you’re only acting on an authenticated user here.
If it’s a customer, you could name it customer
instead of user
to have this more specific name.
If it’s a SQL database, you could use that(sqlDatabase) as a name to make it really clear which kind of database you are working on.
If you store just a first name of a user in a certain field. you might wanna name it firstName
instead of just name
, to be more specific about the kind of name being stored in there.
On the other hand, I would argue that age
is already pretty specific. So you probably can’t be more specific there.
So it’s not about making your names as long as possible. but just about making them as clear and as easy to understand as possible.
And sometimes shorter is better, sometimes longer is better. Obviously this is also something which will come with experience but also with help of the demos and exercises you will see through out this course.
Now for booleans it’s the same. You can add more details if it makes sense.
For example, if isActive
refers to a user, you could name it isActiveUser
. loggedIn
on the other hand will typically always refer to a user, So for example, using userIsloggedIn
instead of just loggedIn
is logger but not necessarily better.
本文开始,不再提供更多的短语摘抄和举例,仅对生词进行摘抄,其他的好的表达或者要注意的细节会直接在文中用加粗表示。
-
dig a bit deeper 深挖一些
Shall we dig a bit deeper?
-
redundancy 冗余
There is a high degree of redundancy in the code.
-
authenticated 认证过的
How can I know this is an authenticated user?
Tips:此系列的重点在于如何用英文表达一些逻辑,适合积累口语,大家可以仔细品读短文,找到自己喜欢的表达方式。
推荐的学习方法:先听几遍保证能听懂70%,再对照原文进行学习,找到自己喜欢的表达后反复造句让自己加深印象,跟着原文读一遍,然后再自己读一遍。
Source:【整洁代码 Clean Code-哔哩哔哩】 https://b23.tv/skgyGMC