当前位置:网站首页>Benefits of declaring variables
Benefits of declaring variables
2022-06-11 03:38:00 【sayWhat_ sayHello】
Preface
- Recently, I am rereading 《C Primer Plus》 Part of it shows the benefits of declaring variables .
- Readers who know me should know that the main language I used before is JAVA, To use a variable, you must declare it in advance , There is no doubt about it . So of course, I never thought about why I should declare variables ~
- Now the popular language Python It is allowed to use... Directly without declaring variables .
- There are strong and weak types , Here also consider the benefits of strong and weak types when declared .
Text
C Primer Plus Extract the original text
- Putting all the variables together makes it easier for the reader to master the contents of the program .
- Give the variable a meaningful name . For example, the number of times
timesIt will be better than oneaEasier to understand . - If the meaning of variables is deep , Add a note . For students who are not good at English
timesIt may be understood as time , Add a note to remind you of the number of times .
- Give the variable a meaningful name . For example, the number of times
- Consider what variables you need before programming , Push you to do some planning work .
- Avoid subtle errors in the program that are difficult to find , That is, the spelling of the variable name is wrong . for example
RADIUSIandRADIUS1It's two variables , But at first glance, I can't tell . - If you do not declare variables, you will not be able to compile C Program .
Personal thinking
The first 1 spot 、 The first 2 Point of agreement . The first 4 A bit of nonsense .
About the first 3 spot , I don't quite understand . No matter what type . An undeclared variable will also be reported as an error when compiling . So I can't see the benefits .
look down Python:
RADIUSI = 5
print(RADIUS1)
# Error report after compilation
NameError: name 'RADIUS1' is not defined
Again C They will also make corresponding error reports .
error: 'RADIUS1' undeclared (first use in this function)
The benefits of not declaring variables :
- The amount of code may be reduced . Do you want to reduce the amount of code by declaring less types =_=!
# python Code price = 6 count = 5 total_price = price * count print(total_price)// java Code int price = 6; int count = 5; int total_price = price * count; System.out.println(total_price);
Declaration differences between strong and weak types :
Declaring a strong type is equivalent to fixing the scope of use of this variable , Weak types do not have this limitation , But the code is not clear , It is a little against the first principle above :
temp = 100
print(temp)
temp = "hello world"
print(temp)
temp = {
1, 2, 3, 4}
print(temp)
temp = [1,2]
print(temp)
Yes , If you see this kind of writing in the project , I guess I feel like vomiting ...
Variable naming techniques
This complements the technique seen in the book , But most of the time, it is still an agreement . namely : Put type abbreviations on variable names .
For example, declare variables i_num, s_title, d_money, i representative int,s representative string,d representative double. In this way, you can clearly get Type to variable , I think it is appropriate to use weak typed languages . But it's a little tedious , Use also varies from person to person .
边栏推荐
- {dataSource-1} closing ... {dataSource-1} closed
- postgresql copy语句
- Computer vision (AI) interview
- 潮玩力真火力!年轻人第一台巨幕影院?酷开电视Max 86“庞然来袭
- Jeecgboot learning_ Online form first experience
- OpenGL Chapter 8 material
- TweenMax五彩小球弹跳动画
- 实现发布订阅模式-----手撕js系列
- SQL | some indicators of the game industry
- The key data of music genuine rate is missing. What is the odds of Netease cloud music IPO?
猜你喜欢

net::ERR_ FILE_ NOT_ Found error

Azure kubernates service update | improve development experience and efficiency

OpenGL错误指南

J. Balanced Tree

用Fragment实现图片简易浏览

蓄力618 ,苏宁如何打下这场硬仗?

If no separation ----- > > login module nanny level source code analysis (0)

Xu Li 618, how can Suning fight this hard battle?

regular expression

突破中国品牌创新技术实力,TCL做对了什么?
随机推荐
The key data of music genuine rate is missing. What is the odds of Netease cloud music IPO?
SQL | 游戏行业部分指标
正则表达式
C. Jump and Treasure(dp + 单调队列优化)
OPENSSL ASN. 1, DER, PEM, X509
has been blocked by CORS policy: No ‘Access-Control-Allow-Origin‘ header is present on the requested
Azure Kubernates Service 更新|提升开发体验和效率
OpenGl第九章 光照贴图
[elt.zip] openharmony paper Club - multi tier storage hierarchical data compression
OpenSSL enc encryption and decryption
Right click PowerShell here function add
OpenGL Chapter 7 basic lighting
Solution to the problem of gd32f4 serial port DMA reception
Thinkphp3.2.3 deserialization using chain analysis
【ELT.ZIP】OpenHarmony啃论文俱乐部——快速随机访问字符串压缩
Canvas rotation drawing H5 animation JS effect
多线程四部曲之NSThread
Pthread in the multithreaded Trilogy
联易融一面(已过)
Difference between idea open and import project