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. 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
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.
The JVM will use the same reference for values in the interval [-128, 127]. Even stranger, the same behavior exists in Python.
So far, nothing particularly surprising.
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.
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.
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.
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.
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. *
* 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.
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
The big explosion of the sun's death created ...
"There's some gold foil on the chicken n...
There are many cases of people becoming famous ov...
How to easily obtain accurate traffic. Maybe you ...
The content of the communication is different fro...
Affected by the Sino-US trade friction, Chinese c...
The blockchain game "Eden" makes money ...
The 2018 Spring Festival holiday has ended, and t...
Author: Huang Yanhong Duan Yuechu In daily life, ...
Baidu has a bidding course. You can learn the bas...
Amazon CEO Bezos has been all over the news on CN...
Staying up late, working overtime, and being unde...
We all know that animals have their own world. Ev...
Promotion goals Compared with the complete Intern...
Since the death of Steve Jobs in 2011 and the lea...