Some evil spells in programming languages, never use them

Some evil spells in programming languages, never use them

Ever since I watched Gary Bernhardt's highly regarded video Watt, I've been amazed at the weird behaviors of certain programming languages. Some programming languages ​​behave more unexpectedly than others. For example, there are entire books devoted to edge cases and oddities in Java. Likewise, you can read the C++ specification for about $200.

[[222543]]

Below is a collection of my favorite, surprising, hilarious, and still-effective spells. Generally speaking, taking advantage of these oddities is seen as a bad thing, as code shouldn't be unexpected. Thankfully, there are plenty of linters ready to laugh at you if you try most of the following silliness. Having said all that, knowledge is power, so let's get started.

The evil reassignment of True in Python 2

  1. >>> True = False  
  2. >>> True  
  3. False  

Thankfully, this will cause a SyntaxError in Python 3, since True, False, and None are now reserved words. It's still far less evil than the C++ shenanigans that snuck #define true false into a standard header file on a coworker's development machine.

Examples of weird behavior in Java and Python

The semantics of == are often confusing to new Java programmers. Even in trivial situations, the inconsistencies of this operator can complicate matters, even if the performance benefits are worth it.

  1. Integer a = 100;
  2. Integer b = 100;
  3. System. out .print(a == b); // prints true  
  4.   
  5. Integer c = 200;
  6. Integer d = 200;
  7. System. out .print(c == d); // prints false  

The JVM will use the same reference for values ​​in the interval [-128, 127]. Even stranger, the same behavior exists in Python.

  1. >>> x = 256
  2. >>> y = 256
  3. >>> x is y
  4. True  
  5.   
  6. >>> x = 257
  7. >>> y = 257
  8. >>> x is y
  9. False  

So far, nothing particularly surprising.

  1. >>> x = -5
  2. >>> y = -5
  3. >>> x is y
  4. True  
  5.   
  6. >>> x = -6
  7. >>> y = -6
  8. >>> x is y
  9. False  

It seems that the lower limit that the Python interpreter uses for the same example is... -5. Integers in the interval [-5, 256] have the same ID. Somehow this gets even weirder.

  1. >>> x = -10
  2. >>> y = -10
  3. >>> x is y
  4. False  
  5. >>> x, y = [-10, -10]
  6. >>> x is y
  7. True  

It seems that using destructuring assignment changes the rules here. I'm not sure why this is the case. In fact, I asked a question on Stack Overflow to try to understand it. My guess is that repeated values ​​in a list refer to the same object to save memory.

Inverted subscript notation in C

Reversed subscript notation is a headache for all developers.

  1. int x[1] = { 0xdeadbeef };  
  2. printf( "%xn" , 0[x]); // prints deadbeef

The reason this works is that array[index] is really just syntactic sugar for *(array + index). Due to the commutative nature of addition, we can swap the arrays and the indices and get the same result.

The "reciprocal" operator in C

The –> operator looks like a syntax error when you first see it. It looks like an undocumented language feature until you realize it compiles. Fortunately, it is neither.

  1. for (x = 3; x --> 0;) {  
  2. printf( "%d " , x); // prints 2 1 0
  3. }

The –> “operator” is really two operators, parsed in this context as (x–) > 0. It is well known that heavy use can lead to confusion, and it is simply evil.

sizeof operator in C

The sizeof operator is a compile-time operator, which gives it interesting properties.

  1. int x = 0;
  2. sizeof(x += 1);
  3. if (x == 0) {
  4. printf( "wtf?" ); // this will be printed
  5. }

Since the sizeof operator example is evaluated at compile time, (x += 1) will not run. Another interesting fact is that research shows that printf("wtf?") is the most common code that is not pushed.

Lua, Smalltalk, MATLAB and other languages, indexed starting at 1

/r/programminghumor has been having fun with the "indexing starts at 1" meme. A shocking number of programming languages ​​use array indexing starting at 1. A more comprehensive list can be found here.

0 in Ruby is considered true

… and only Ruby. *

In Ruby, this is the case. *

  1. if 0 then print 'thanks, ruby'   end # prints thanks, ruby

* edit: It was pointed out on reddit that this is true for Lua, Lisp, and Erlang as well.

* EDIT: Someone on Reddit pointed out that this also holds true in Lua, Lisp, and Erlang.

Trigraph, Digraphs, and Tokens in C

Trigraph, Digraph and Token in C

For historical reasons, non-alphabetic symbols in the C language have substitutes.

  1. if ( true   and   true ) { // same as if ( true && true )
  2. printf( "thanks, c" );
  3. }

Some foreign devices, such as the IBM 3270, do not provide certain commonly used symbols in C/C++, so digraph, trigraph, and token are provided to avoid excluding specific character sets.

<<:  Three amazing technical experts I met during my programming career

>>:  Apple is still considering whether to completely rebuild Siri

Recommend

Whose nerves were touched by Pechoin’s “high dissemination, low conversion”?

Recently, Pechoin ’s amazing advertisement has se...

One article is enough for the year-end marketing plan of wine promotion

How to promote alcoholic products? An article wil...

How to motivate users? 4 aspects!

This article combines some of the user operations...

In 2019, there are two modes of content operation!

All content production is the product of certain ...

Counterpoint: Global Cellular IoT Module Shipments to Exceed 1.2 Billion by 2030

By 2030 , global cellular IoT module shipments wi...

Build a training camp SOP from 0-1!

Training camp is one of the projects I have manag...

Night Hunters full version uncut HD Chinese subtitles

Ben Kingsley, Henry Cavill and Alexandra Daddario...

How to play and case analysis of limited-time flash sales in the community

Limited-time flash sales are a marketing applicat...

The cause of the accident was revealed! Why did the subway suddenly break down?

Audit expert: Hu Qizhou Associate Professor, Scho...

Analysis of Through Train Promotion Skills

What are the through train promotion techniques? ...