Why should swift and python abandon + + \ –?

Time:2022-5-18

Today, let’s talk about why swift and python abandon + + –?

Original text|<u> Address < / u >

Easy to use + +–

When it comes to the self increasing (+ +) \ self decreasing (- -) operators, my friends should be familiar with them. They often appear in the code of many programming languages.

  • For example, commonly usedforsentence
for (int i = 0; i < n; i++) {
    // TODO
}

  • For example, the classic one line code realizes string copy
//Copy the contents of SRC to dest
void strcpy(char *dest, char *src) {
    while (*dest++ = *src++);
}

int main() {
    char s1[10], *s2 = "xmg_mj";
    strcpy(s1, s2);
    printf("%s", s1); // xmg_mj
    return 0;
}

If used properly, the self increasing (+ +) \ self decreasing (- -) operator can indeed make the code concise and elegant.

however

2 popular programming languagesSwiftPythonSelf increasing (+ +) and self decreasing (- -) operators are not supported. Why?

Here are some reference links for interested partners to read:

Here are just a few obvious reasons

  • With powerful and concisefor-inforStatements can be completely free of + +–
// C++
for (int i = 0; i < 5; i++) {
    cout << i << endl;
}

// Swift
for i in 0..<5 {
    println(i)
}

// Python
for i in range(5):
    print(i)

  • althoughwhile (d++ = s++);It seems simple and elegant, but it is by no means simple for beginners, which will increase the learning cost. andSwiftPythonI prefer to hope that anyone can quickly start this programming language.

  • When + + and — with prefix and suffix are mixed

    • It will reduce the readability of the code, such aswhile (n++ > –k), experienced programmers must also stop and think about what the code means
    • The operation results may be uncertain

Uncertainty of operation results

Here are two pieces of code. What is the result of variable b? (it is worth mentioning that we will not write this in actual development. It is listed here just to discuss some technical details)

int a, b;

//Segment 1 code
a = 1;
b = a++ + ++a + a++ + ++a;

//Segment 2 code
a = 1;
b = a++ + a++ + a++ + a++;

In fact, the results of the above C language code under MSVC and MinGW compilers are not completely consistent

  • MSVC: produced by Microsoft
  • MinGW: produced by GNU (can be understood as GCC of Windows version)

Segment 1 code

The results are consistent and meet the expectations of most people, so we won’t discuss it

a = 1;
b = a++ + ++a + a++ + ++a;
// MSVC:b = 1 + 3 + 3 + 5 = 12
// MinGW:b = 1 + 3 + 3 + 5 = 12

Segment 2 code

Inconsistent results

  • The result of MSVC is 1 + 1 + 1 + 1 = 4
  • The result of MinGW is 1 + 2 + 3 + 4 = 10
a = 1;
b = a++ + a++ + a++ + a++;
// MSVC:b = 1 + 1 + 1 + 1 = 4
// MinGW:b = 1 + 2 + 3 + 4 = 10

You may be curious: how do you know that the calculation process of MinGW is 1 + 2 + 3 + 4? According to the final result 10, did you guess it? NO! If it’s an insult to the programmer’s profession, that’s it.

If you want to know the true nature of code that is not easy to understand from the surface, you need to move out a powerful and accurate weapon. It isAssembly language

Briefly explain the useassembly languageReasons for:

  • as everyone knows,C languageThe code will eventually be compiled intomachine languageCode (also calledMachine instruction, consisting of 0 and 1 only)
  • That’s through researchMachine instructionTo exploreC languageThe nature of code? becauseMachine instructionIt is extremely obscure and difficult to understand, so it is not an efficient method for ordinary people
  • The best way is to study itC languagemachine languageBetweenassembly languagecode
    • C languageassembly languagemachine language
    • assembly languageCode ratioMachine instructionReadability is much higher
    • EachMachine instructionHave correspondingassembly languagecode
    • So you studyassembly languageCode is basically equivalent to researchMachine instructionBoth readability and accuracy

Look at the assembly code in MSVC environment

Why should swift and python abandon + + \ --?
  • Red box code: assign the result of adding 4 A to B. since the initial value of a is 1, B = 1 + 1 + 1 + 1 = 4
  • Green box code: let a execute the operation of self incrementing 1 for 4 times, which is equivalent to executing a + = 1 for 4 times

Look at the assembly code in the MinGW environment

Why should swift and python abandon + + \ --?
  • In order to ensure that you can basically understand this assembly code, it is suggested that you can understand that [rbp-0x4] represents variable a and [rbp-0x8] represents variable B
  • Green box code: let a execute the operation of increasing by 1, which is equivalent to executing a + = 1
  • Red box code: add up the value before a increases by 1 each time, and finally assign it to B
  • It can be seen that the green box and red box codes are executed alternately, so the final B = 1 + 2 + 3 + 4 = 10

The last two codes

Finally, put out two more pieces of code. The results under MSVC and MinGW are also inconsistent

a = 1;
b = ++a + ++a + ++a + ++a;
// MSVC:b = 5 + 5 + 5 + 5 = 20
// MinGW: b = 3 + 3 + 4 + 5 = 15

a = 1;
b = ++a + ++a + a++ + a++;
// MSVC:b = 2 + 3 + 3 + 4 = 12
// MinGW:b = 3 + 3 + 3 + 4 = 13

According to the previous explanations, I believe you can infer the result of MSVC now.

But the result of MinGW may still feel strange: it actually makes the first two first++aExecute the operation of a self increment 1, and the next 2++a\a++As usual, so eventually B = 3 + 3 +

Well, stop here. I suggest not to tangle with these details, because this writing method is not recommended. You only need to know that when multiple prefixes and suffixes are used together, the result is uncertain.

Generally speaking, + +, — is a double-edged sword. Moreover, it is not indispensable in the coding process, so it isSwiftPythonAbandonment is also normal.

About Compilation

I often see people say:assembly languageThey are all ancient programming languages, which are useless. Some people even say that C \ C + + is such an ancient language, which has no learning value. I personally do not agree with these views. Mastering assembly can better understand the essence of code and eliminate some basic knowledge misunderstandings.

Due to the relationship between time and length, this article does not explain in detail the function of each sentence of assembly code. If you are interested in compilation, you can refer to the following pictures

Why should swift and python abandon + + \ --?

Previously, some compilation tutorials were uploaded on station B. if necessary, the little partner can send the word “compilation” to the official account to obtain the tutorial address

The last question

Finally, leave a thinking question, and you can leave a comment directly on the thinking results

Not thatPythonDoes not support the self increment (+ +) \ self decrement (- -) operator? WhyPythonCan the code run successfully?

a = 10
b = ++a

c = a++ + ++a

Recommended Today

Java program leads to the overall understanding of JVM!

Developers all know that, for example, after entering a few lines of code in idea and running, they can output the results on the console. This is our routine operation. Do you really understand the whole process? Next, I’ll take you to analyze Take the following example code as an example: The operation results are […]