当前位置:网站首页>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

  1. 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 times It will be better than one a Easier to understand .
    • If the meaning of variables is deep , Add a note . For students who are not good at English times It may be understood as time , Add a note to remind you of the number of times .
  2. Consider what variables you need before programming , Push you to do some planning work .
  3. Avoid subtle errors in the program that are difficult to find , That is, the spelling of the variable name is wrong . for example RADIUSI and RADIUS1 It's two variables , But at first glance, I can't tell .
  4. 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 :

  1. 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 .

原网站

版权声明
本文为[sayWhat_ sayHello]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020554154971.html