Strangest language feature
Language AgnosticProgramming LanguagesLanguage Agnostic Problem Overview
What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
Language Agnostic Solutions
Solution 1 - Language Agnostic
In C, arrays can be indexed like so:
a[10]
which is very common.
However, the lesser known form (which really does work!) is:
10[a]
which means the same as the above.
Solution 2 - Language Agnostic
In JavaScript:
'5' + 3 gives '53'
Whereas
'5' - 3 gives 2
Solution 3 - Language Agnostic
In JavaScript, the following construct
return
{
id : 1234,
title : 'Tony the Pony'
};
returns is a syntax error due to the sneaky implicit semicolon insertion on the newline after undefined
return
. The following works as you would expect though:
return {
id : 1234,
title : 'Tony the Pony'
};
Even worse, this one works as well (in Chrome, at least):
return /*
*/{
id : 1234,
title : 'Tony the Pony'
};
Here's a variant of the same issue that does not yield a syntax error, just silently fails:
return
2 + 2;
Solution 4 - Language Agnostic
JavaScript truth table:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
" \t\r\n" == 0 // true
> Source: Doug Crockford
Solution 5 - Language Agnostic
Trigraphs in C and C++.
int main() {
printf("LOL??!");
}
This will print LOL|
, because the trigraph ??!
is converted to |
.
Solution 6 - Language Agnostic
Fun with auto boxing and the integer cache in Java:
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:
Integer foo = 42;
Integer bar = 42;
foo <= bar; // true
foo >= bar; // true
foo == bar; // true
Explanation
A quick peek at the Java source code will turn up the following:
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
Note: IntegerCache.high
defaults to 127
unless set by a property.
What happens with auto boxing is that both foo and bar the same integer object retrieved from the cache unless explicitly created: e.g. foo = new Integer(42)
, thus when comparing reference equality, they will be true rather than false. The proper way of comparing Integer value is using .equals;
Solution 7 - Language Agnostic
Quoting Neil Fraser (look at the end of that page),
try {
return true;
} finally {
return false;
}
(in Java, but behaviour is apparently the same in JavaScript and Python). The result is left as an exercise to the reader.
EDITED: As long as we are on the subject consider also this:
try {
throw new AssertionError();
} finally {
return false;
}
Solution 8 - Language Agnostic
APL (other than ALL of it), the ability to write any program in just one line.
e.g. Conway's Game of Life in one line in APL:
If that line isn't WTF, then nothing is!
And here is a video
Solution 9 - Language Agnostic
The weird things C++ templates can be used for, best demonstrated by "Multi-Dimensional Analog Literals" which uses templates to compute the area of "drawn" shapes. The following code is valid C++ for a 3x3 rectangle
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
Or, another example with a 3D cube:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
Solution 10 - Language Agnostic
Perl’s many built-in variables:
$#
— not a comment!$0
,$$
, and$?
— just like the shell variables by the same name$ˋ
,$&
, and$'
— weird matching variables$"
and$,
— weird variables for list- and output-field-separators$!
— likeerrno
as a number butstrerror(errno)
as a string$_
— the stealth variable, always used and never seen$#_
— index number of the last subroutine argument... maybe@_
— the (non)names of the current function... maybe$@
— the last-raised exception%::
— the symbol table$:
,$^
,$~
,$-
, and$=
— something to do with output formats$.
and$%
— input line number, output page number$/
and$\
— input and output record separators$|
— output buffering controller$[
— change your array base from 0-based to 1-based to 42-based: WHEEE!$}
— nothing at all, oddly enough!$<
,$>
,$(
,$)
— real and effective UIDs and GIDs@ISA
— names of current package’s direct superclasses$^T
— script start-up time in epoch seconds$^O
— current operating system name$^V
— what version of Perl this is
There’s a lot more where those came from. Read the complete list here.
Solution 11 - Language Agnostic
PHP's handling of numeric values in strings. See this previous answer to a different question for full details but, in short:
"01a4" != "001a4"
If you have two strings that contain a different number of characters, they can’t be considered equal. The leading zeros are important because these are strings not numbers.
"01e4" == "001e4"
PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000"
as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.
Solution 12 - Language Agnostic
Let's have a vote for all languages (such as PL/I) that tried to do away with reserved words.
Where else could you legally write such amusing expressions as:
IF IF THEN THEN = ELSE ELSE ELSE = THEN
(IF
, THEN
, ELSE
are variable names)
or
IF IF THEN THEN ELSE ELSE
(IF
is a variable, THEN
and ELSE
are subroutines)
Solution 13 - Language Agnostic
The JavaScript octal conversion 'feature' is a good one to know about:
parseInt('06') // 6
parseInt('07') // 7
parseInt('08') // 0
parseInt('09') // 0
parseInt('10') // 10
More details here.
Solution 14 - Language Agnostic
In C one can interlace a do/while with a switch statement. Here an example of a memcpy using this method:
void duff_memcpy( char* to, char* from, size_t count ) {
size_t n = (count+7)/8;
switch( count%8 ) {
case 0: do{ *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
}while(--n>0);
}
}
Solution 15 - Language Agnostic
Algol pass by name (illustrated using C syntax):
int a[3] = { 1, 2, 3 };
int i = 1;
void f(int j)
{
int k;
k = j; // k = 2
i = 0;
k = j; // k = 1 (!?!)
}
int main()
{
f(a[i]);
}
Solution 16 - Language Agnostic
In Python:
>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False
Not a WTF, but a useful feature.
Solution 17 - Language Agnostic
In Java:
int[] numbers() {
return null;
}
Can be written as:
int numbers() [] {
return null;
}
Solution 18 - Language Agnostic
http://en.wikipedia.org/wiki/INTERCAL_programming_language">INTERCAL</a> is probably the best compendium of strangest language features. My personal favourite is the COMEFROM statement which is (almost) the opposite of GOTO.
> COMEFROM is roughly the opposite of > GOTO in that it can take the execution > state from any arbitrary point in code > to a COMEFROM statement. The point in > code where the state transfer happens > is usually given as a parameter to > COMEFROM. Whether the transfer happens > before or after the instruction at the > specified transfer point depends on > the language used. Depending on the > language used, multiple COMEFROMs > referencing the same departure point > may be invalid, be non-deterministic, > be executed in some sort of defined > priority, or even induce parallel or > otherwise concurrent execution as seen > in Threaded Intercal. A simple example > of a "COMEFROM x" statement is a label > x (which does not need to be > physically located anywhere near its > corresponding COMEFROM) that acts as a > "trap door". When code execution > reaches the label, control gets passed > to the statement following the > COMEFROM. The effect of this is > primarily to make debugging (and > understanding the control flow of the > program) extremely difficult, since > there is no indication near the label > that control will mysteriously jump to > another point of the program.
Solution 19 - Language Agnostic
Not really a language feature, but an implementation flaw: Some early Fortran compilers implemented constants by using a constant pool. All parameters were passed by reference. If you called a function, e.g.
f(1)
The compiler would pass the address of the constant 1 in the constant pool to the function. If you assigned a value to the parameter in the function, you would change the value (in this case the value of 1) globally in the program. Caused some head scratching.
Solution 20 - Language Agnostic
Don't know if it can be considered a language feature, but, in C++ almost any compiler error related to templates delivers a fair amount of WTF to many C++ programmers around the world on daily basis :)
Solution 21 - Language Agnostic
The many name spaces of C:
typedef int i;
void foo()
{
struct i {i i;} i;
i: i.i = 3;
printf( "%i\n", i.i);
}
Or with characters:
typedef char c;
void foo()
{
struct c {c c;} c;
c: c.c = 'c';
printf( "%c\n", c.c);
}
Solution 22 - Language Agnostic
I would say the whole whitespace thing of Python is my greatest WTF feature. True, you more-or-less get used to it after a while and modern editors make it easy to deal with, but even after mostly full time python development for the past year I'm still convinced it was a Bad Idea. I've read all the reasoning behind it but honestly, it gets in the way of my productivity. Not by much, but it's still a burr under the saddle.
edit: judging by the comments, some people seem to think I don't like to indent my code. That is an incorrect assessment. I've always indented my code no matter what the language and whether I'm forced to or not. What I don't like is that it is the indentation that defines what block a line of code is in. I prefer explicit delimiters for that. Among other reasons, I find explicit delimiters makes it easier to cut and paste code.
For example, if I have a block indented 4 spaces and paste it at the end of a block that is indented 8 spaces, my editor (all editors?) have no idea if the pasted code belongs to the 8-space block or the outer block. OTOH, if I have explicit delimiters it's obvious which block the code belongs to and how it should be (re-)indented -- it does so by intelligently looking for block delimiters.
edit 2: some people who provide comments seem to think this is a feature I hate or that I think makes python a poor language. Again, not true. While I don't like it all that much, that's beside the point. The question is about the strangest language feature, and I think this is strange, by virtue of it being something very, very few (but >0) languages use.
Solution 23 - Language Agnostic
I struggled a bit about this:
1;
In perl, modules need to return something true.
Solution 24 - Language Agnostic
I always wondered why the simplest program was:
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Whereas it could be:
print "Hello World!"
Maybe this is to frighten computer science students in the first place ...
Solution 25 - Language Agnostic
I'm surprised that no one has mentioned Visual Basic's 7 loop constructs.
For i As Integer = 1 to 10 ... Next
While True ... End While
Do While True ... Loop
Do Until True ... Loop
Do ... Loop While True
Do ... Loop Until True
While True ... Wend
Because sticking an ! in front of your conditional is way too complicated!
Solution 26 - Language Agnostic
For those who don't know, bc
is an "arbitrary precision calculator language", and I use it quite often for quick calculations, particularly when the numbers involved are large ($
is the prompt):
$ bc -lq
12^345
20774466823273785598434446955827049735727869127052322369317059031795\
19704325276892191015329301807037794598378537132233994613616420526484\
93077727371807711237016056649272805971389591721704273857856298577322\
13812114239610682963085721433938547031679267799296826048444696211521\
30457090778409728703018428147734622401526422774317612081074841839507\
864189781700150115308454681772032
bc
has been a standard Unix command for a long time.
Now for the "WTF feature". This is from man bc
(emphasis mine):
> quit: When the quit statement is read, the bc processor is terminated, > regardless of where the quit statement is found. > For example, "if (0 == 1) quit" will cause bc to terminate. > > halt: The halt statement (an extension) is an executed statement that > causes the bc processor to quit only when it is executed. > For example, "if (0 == 1) halt" will not cause bc to terminate because the halt is not executed.
Solution 27 - Language Agnostic
JavaScript is object oriented, right? So running methods on literal strings and numbers should work. Like "hello".toUpperCase()
and 3.toString()
. Turns out that second one is a syntax error, why? Because the parser expects a number followed by a dot to be a floating point literal. That's not the WTF, the WTF is that you only have to add another dot to make it work:
3..toString()
The reason is that the literal 3.
is interpreted as 3.0
, and 3.0.toString()
works fine.
Solution 28 - Language Agnostic
In JavaScript:
2 == [2]
// Even stranger
2 == [[[2]]]
// And down-right nutty
var a = { "abc" : 1 };
a[[[["abc"]]]] === a["abc"]; // this is also true
Luckily the kind folks at stackoverflow.com explained the whole thing to me: https://stackoverflow.com/questions/1724255/why-does-2-2-in-javascript
Solution 29 - Language Agnostic
My biggest most hated feature is any configuration file syntax which includes conditional logic. This sort of thing is rife in the Java world (Ant, Maven, etc. You know who you are!).
You just end up programming in a c**p language, with limited debugging and limited editor support.
If you need logic in your configuration the "Pythonic" approach of coding the configuration in a real language is much much better.
Solution 30 - Language Agnostic
powerbasic (www.powerbasic.com) includes the compiler directive:
# BLOAT {bloatsize}
this increases the size of the compiled executable by <bloatsize>
bytes. this was put in the compiler in case people creating the executable don't like the small size of the generated executable. it makes the EXE seem bigger to compete with bloated programming languages:)
Solution 31 - Language Agnostic
In PHP function names are not case sensitive. This might lead you to think that all identifiers in php are not case sensitive. Guess again. Variables ARE case sensitive. WTF.
function add($a, $b)
{
return $a + $b;
}
$foo = add(1, 2);
$Foo = Add(3, 4);
echo "foo is $foo"; // outputs foo is 3
echo "Foo is $Foo"; // outputs Foo is 7
Solution 32 - Language Agnostic
I've always been a huge fan of the PHP error thrown when using two colons in a row out of context:
> Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /path/to/file/error.php on line 3
The first time I encountered this I was absolutely befuddled.
Solution 33 - Language Agnostic
In C
a[i++] = i;
It compiles, but it rarely does what you think it ought to do. An optimization change leads to producing wildly different results. And it runs differently on different platforms.
Yet, the compiler's perfectly happy with it.
Solution 34 - Language Agnostic
Python 2.x
>>>True = False
>>>True
False
You can really make someone become crazy with this one.
Solution 35 - Language Agnostic
Oracle has a couple of SQL WTF issues.
-
Oracle's treatment of empty strings as null.
-
Treatment of null values in a "<>" comparison.
create table wtf (key number primary key, animal varchar2(10)); insert into wtf values (1,'dog'); insert into wtf values (2,''); insert into wtf values (3,'cat'); select * from wtf where animal <> 'cat';
The only row returned is the (1,'dog') row.
Solution 36 - Language Agnostic
Java has a whole freakin book about them.
Solution 37 - Language Agnostic
In JavaScript, void
is not a keyword, it is not a type declaration, nor is it a variable name, and it is also not a function, nor is it an object. void
is a prefix operator, similar to -
, --
, ++
, and !
. You can prefix it to any expression, and that expression will evaluate to undefined.
It is frequently used in bookmarklets, and inline event handlers, as in this somewhat frequent example:
<a href="javascript:void(0)">do nothing</a>
The way it's used in that example makes it look like a function invocation, when really it's just an overly clever way of getting the primitive undefined
value. Most people don't really understand the true nature of void
in JavaScript, and that can lead to a lot of nasty bugs and weird unexpected things happening.
Unfortunately, I think the void operator is the only truly guaranteed way to get the undefined
value in JavaScript, since undefined, as pointed out in another answer, is a variable name that can be reassigned, and {}.a
can be messed up by Object.prototype.a = 'foo'
Update: I thought of another way to generate undefined
:
(function(){}())
Eh, a bit verbose though, and it's even less clear that returning "undefined" is its purpose.
Solution 38 - Language Agnostic
Perl has the yada yada operator (...
).
> The so called “yada yada” operator of Perl 6 heritage is a shortcut to mark unimplemented code:
if ($condition) { ... }
>is the same as
if ($condition) { die "not yet implemented" }
Solution 39 - Language Agnostic
In fortran (77 for sure, maybe in 95 as well), undeclared variables and arguments beginning with I
through N
(the "in" group) will be INTEGER
, and all other undeclared variables and arguments will be REAL
(source). This, combined with "whitespace optional in certain cases" resulted in one of the most famous bugs.
As told by Fred Webb in alt.folklore.computers
in 1990:
> I worked at Nasa during the summer of 1963. The group I was working
> in was doing preliminary work on the Mission Control Center computer
> systems and programs. My office mate had the job of testing out an
> orbit computation program which had been used during the Mercury
> flights. Running some test data with known answers through it, he was
> getting answers that were close, but not accurate enough. So, he
> started looking for numerical problems in the algorithm, checking to
> make sure his tests data was really correct, etc.
>
> After a couple of weeks with no results, he came across a DO
> statement, in the form:
>
> DO 10 I=1.10
>
> This statement was interpreted by the compiler (correctly) as:
>
> DO10I = 1.10
>
> The programmer had clearly intended:
>
> DO 10 I = 1, 10
>
> After changing the .
to a ,
the program results were correct to
> the desired accuracy. Apparently, the program's answers had been
> "good enough" for the sub-orbital Mercury flights, so no one suspected
> a bug until they tried to get greater accuracy, in anticipation of
> later orbital and moon flights. As far as I know, this particular bug
> was never blamed for any actual failure of a space flight, but the
> other details here seem close enough that I'm sure this incident is the
> source of the DO
story.
I think it's a big WTF if DO 10 I
is taken as DO10I
, and that in turn, because of implicit declarations is taken to be of type REAL
. And it's a great story.
Solution 40 - Language Agnostic
My favorite little C++ syntax trick is that you can put URL's (with some restrictions) directly into the code:
int main( int argc, char *argv[] )
{
int i=10;
http://www.stackoverflow.com
return 1;
}
This compiles just fine.
Syntax highlighting kind of spoils the joke, but it's still fun.
Solution 41 - Language Agnostic
I would not dare to claim that XML is a programming language, but isn't it close to our heart? :-)
The strangest feature, to my mind, in XML is that the following is a well-formed document:
<_....>
</_....>
Here is the the lexical definition of NT-Name that allows consecutive dots.
Solution 42 - Language Agnostic
Inheriting from random class in Ruby:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(first seen at https://stackoverflow.com/questions/63998/hidden-features-of-ruby/474888#474888)
Solution 43 - Language Agnostic
I was taken by surprise that you can change a class's inheritance chain in Perl by modifying its @ISA
array.
package Employee;
our @ISA = qw(Person);
# somwhere far far away in a package long ago
@Employee::ISA = qw(Shape);
# Now all Employee objects no longer inherit from 'Person' but from 'Shape'
Solution 44 - Language Agnostic
I love the fact that this sort of thing is fine in JavaScript:
var futureDate = new Date(2010,77,154);
alert(futureDate);
and results in a date 77 months and 154 days from the 0th day of 0th month of 2010 i.e. Nov 1st 2016
Solution 45 - Language Agnostic
In ruby/python/c, you can concatenate strings just like this:
a = "foo" "bar"
print a # => "foobar"
Solution 46 - Language Agnostic
In JavaScript, undefined
is a global variable whose default value is the primitive value undefined
. You can change the value of undefined
:
var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false
Due to the mutability of undefined
, it is generally a better idea to check for undefined-ness through typeof
:
var a = {};
typeof a.b == "undefined"; // always true
Solution 47 - Language Agnostic
In Forth, anything that does not contains spaces can be an identifier (things that contain spaces take a bit of work). The parser first checks if the thing is defined, in which case it is called a word, and, if not, checks if it is a number. There are no keywords.
At any rate, this means that one can redefine a number to mean something else:
: 0 1 ;
Which creates the word 0
, composed of 1
, whatever that was at the time this was executed. In turn, it can result in the following:
0 0 + .
2 Ok
On the other hand, a definition can take over the parser itself -- something which is done by the comment words. That means a Forth program can actually become a program in a completely different language midway. And, in fact, that's the recommended way of programming in Forth: first you write the language you want to solve the problem in, then you solve the problem.
Solution 48 - Language Agnostic
I added the "format" function to Lisp in about 1977, before "printf" even existed (I was copying from the same source as Unix did: Multics). It started off innocently enough, but got laden with feature after feature. Things got out of hand when Guy Steele put in iteration and associated features, which were accepted into the Common Lisp X3J13 ANSI standard. The following example can be found at Table 22-8 in section 22.3.3 of Common Lisp the Language, 2nd Edition:
(defun print-xapping (xapping stream depth)
(declare (ignore depth))
(format stream
"~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]"
(xectorp xapping)
(do ((vp (xectorp xapping))
(sp (finite-part-is-xetp xapping))
(d (xapping-domain xapping) (cdr d))
(r (xapping-range xapping) (cdr r))
(z '() (cons (list (if vp (car r) (car d)) (or vp sp) (car r)) z)))
((null d) (reverse z)))
(and (xapping-domain xapping)
(or (xapping-exceptions xapping)
(xapping-infinite xapping)))
(xapping-exceptions xapping)
(and (xapping-exceptions xapping)
(xapping-infinite xapping))
(ecase (xapping-infinite xapping)
((nil) 0)
(:constant 1)
(:universal 2))
(xapping-default xapping)
(xectorp xapping)))
Solution 49 - Language Agnostic
MUMPS. There are lots of WTF features, I've picked one, the if
statement. (Note that I'm using a rather verbose coding style below in order to accomodate those who don't know the language; real MUMPS code is usually more inscrutable to the uninitiated.)
if x>10 do myTag(x) ; in MUMPS "tag" means procedure/function
else do otherTag(x)
This is similar to saying in Java:
if (x > 10) {
myMethod(x);
} else {
otherMethod(x);
}
Except that in MUMPS, the else
statement isn't syntactically part of the if block, it is a separate statement that works by examining the built-in variable $TEST
. Every time you execute an if
statement it sets $TEST
to the result of the if
statement. The else
statement actually means "execute the rest of line if $TEST
is false, otherwise skip to the next line".
This means that if x
was greater than 10 and thus the first line called myTag
, and myTag
contains if
statements, then the behavior of the else
depends not on the if
in the line above it but on the last if
evaluated inside of myTag
! Because of this "feature", MUMPS coders are generally taught write the above code like this to be safe:
if x>10 do myTag(x) if 1
else do otherTag(x)
The if 1
at the end of the first line ensures that $TEST
is set correctly before control proceeds to the next line. (BTW, the spacing here has to be just so, with two spaces after the else
and one space in all the other places. The spacing is odd but at least it's very orthogonal once you understand the pattern.)
Solution 50 - Language Agnostic
Tri-valued logic of nulls
in ANSI SQL.
Solution 51 - Language Agnostic
An amusing side effect of Python's everything-is-really-a-reference:
>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]
Solution 52 - Language Agnostic
In JavaScript, you can use a double bitwise negation (~~n
) as a replacement for Math.floor(n)
(if n
is a positive number) or parseInt(n, 10)
(even if n
is negative). n|n
and n&n
always yield the same results as ~~n
.
var n = Math.PI;
n; // 3.141592653589793
Math.floor(n); // 3
parseInt(n, 10); // 3
~~n; // 3
n|n; // 3
n&n; // 3
// ~~n works as a replacement for parseInt() with negative numbers…
~~(-n); // -3
(-n)|(-n); // -3
(-n)&(-n); // -3
parseInt(-n, 10); // -3
// …although it doesn’t replace Math.floor() for negative numbers
Math.floor(-n); // -4
A single bitwise negation (~
) calculates -(parseInt(n, 10) + 1)
, so two bitwise negations will return -(-(parseInt(n, 10) + 1) + 1)
.
Update: Here’s a jsPerf test case comparing the performance of these alternatives.
Solution 53 - Language Agnostic
Not so much a weird feature, but one that's really irritating from a type-safety point of view: array covariance in C#.
class Foo { }
class Bar : Foo { }
class Baz : Foo { }
Foo[] foo = new Bar[1];
foo[0] = new Baz(); // Oh snap!
This was inherited (pun intentional) from Java, I believe.
Solution 54 - Language Agnostic
My favorite weirdness in C is 5["Hello World"], but since that was already posted, my next-favorite weirdness is the Windows versioned-structure initialization hack:
void someWindowsFunction() {
BITMAPINFOHEADER header = {sizeof header};
/* do stuff with header */
}
That one, subtle line accomplishes the following:
- Declares a BITMAPINFOHEADER structure
- Concisely sets the "size" member of the structure, without hardcoding a size constant (since many Window structures, including BITMAPINFOHEADER, follow the convention of specifying the size of the structure as the first member}
- Declares the version of the structure (since many Windows structures, including BITMAPINFOHEADER, identify their version by the declared size, following the convention that structures definitions are append-only)
- Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).
Solution 55 - Language Agnostic
Java; making all object instances be mutexes.
Solution 56 - Language Agnostic
In PHP one can do:
System.out.print("hello");
Solution 57 - Language Agnostic
In JavaScript:
alert(111111111111111111111) // alerts 111111111111111110000
This was quite damaging to some 64bit keys I passed back and forth in JSON.
Solution 58 - Language Agnostic
else
in Python's for
loops.
From the Python documentation:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
Output:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Solution 59 - Language Agnostic
Some early dynamic languages (including, if I remember correctly, early versions of Perl) hadn't figured out what was good dynamism and what was bad dynamism. So some of them allowed this:
1 = 2;
After that statement, the following would be true:
if(1 + 1 == 4)
Solution 60 - Language Agnostic
In Python, the "compile time" (or declaration time) evaluation of function arguments can be confusing:
def append(v, l = []):
l.append(v)
return l
print append(1)
print append(2)
>>> [1]
>>> [1,2]
The intention might have been:
def append(v, l = None):
if l is None:
l = []
l.append(v)
return l
print append(1)
print append(2)
>>> [1]
>>> [2]
This behavior is useful for things like caching, but it can be dangerous.
A bonus feature: tuples with mutable contents:
a = (1,2,[3])
a[2][:] = [4] # OK
a[2] = [2] # crashes
Solution 61 - Language Agnostic
In PHP, a string is as good as a function pointer:
$x = "foo";
function foo(){ echo "wtf"; }
$x(); # "wtf"
Unfortunately, this doesn't work:
"foo"();
Solution 62 - Language Agnostic
In Scala, there are no operators, just methods. So a + b - c
is actually the same as a.+(b).-(c)
. In this, it is equal to Smalltalk. However, unlike Smalltalk, precedence is taken into account. The rules are based on the first character, so an hypothetical method called *+
would have precedence over one called +*
. An exception is made so that any method ending in =
will have the same precedence as ==
-- meaning !!
and !=
(non-hypothetical methods) have different precedence.
All ASCII letters have the lowest precedence, but all non-ASCII (unicode) characters have the highest precedence. So if you wrote a method is
comparing two ints, then 2 + 2 is 1 + 3
would compile and be true. Were you to write it in portuguese, é
, then 2 + 2 é 1 + 3
would result in error, as it would see that as 2 + (2 é 1) + 3
.
And, just to top off the WTF of operators in Scala, all methods ending in :
are right-associative instead of left-associative. That means that 1 :: 2 :: Nil
is equivalent to Nil.::(2).::(1)
instead of 1.::(2).::(Nil)
.
Solution 63 - Language Agnostic
In JavaScript the result of a method can depend upon the style braces are placed. This is the K&R style, where braces are placed right after the method signature and after a return statement:
var foo = function() {
return {
key: 'value'
};
}
foo() // returns an object here
Now, if I format this code to the Allman style, where braces are always placed on a new line, the result is different:
var foo = function()
{
return
{
key: 'value'
};
}
foo() // returns undefined here
How come? In JavaScript the language places automatically semicolons at the end of each line if you won't do it yourself. So what really happened in the last code fragment was this:
var foo = function()
{
return; // here's actually a semicolon, automatically set by JavaScript!
{
key: 'value'
};
}
So if you'd call foo()
, the first statement in the method would be a return statement, which would return undefined
and would not execute other following statements.
Solution 64 - Language Agnostic
Other weird things:
In C++ overriding a virtual method hides all other overloads of that method. In Java this does not happen. This is very annoying. Example: http://codepad.org/uhvl1nJp
In C++ if a base class has a public virtual method foo() and a subclass has a private method foo(), this private method overrides the other one! This way you can call what is a private method outside of the class just by casting the subclass object pointer to a superclass object pointer. This shouldn't be possible: it's a violation of encapsulation. The new method should not be treated as an override of the old one. Example: http://codepad.org/LUGSNPdh
In PHP you can define functions to accept typed parameters (e.g. objects that are subclasses of a certain interface/class), the annoying thing is that this way you cannot use NULL as the actual parameter value in this case. Example: http://codepad.org/FphVRZ3S
Solution 65 - Language Agnostic
One of my favorites in C++ is the "public abstract concrete inline destructor":
class AbstractBase {
public:
virtual ~AbstractBase() = 0 {}; // PACID!
virtual void someFunc() = 0;
virtual void anotherFunc() = 0;
};
I stole this from Scott Meyers in Effective C++. It looks a bit weird to see a method that's both pure virtual (which generally means "abstract") and implemented inline, but it's the best and most concise way I've found to ensure that an object is polymorphically destructed.
Solution 66 - Language Agnostic
Some 20 years ago, when I last dabbled in MUMPS, the implementations had some curious limitations. While hosts MUMPS was becoming ever more popular, MUMPS was traditionally a self-hosted language: computer language, operating system and database in a single package.
MUMPS was essentially about its database. Essentially, a huge multidimensional hash table, supported by a B* tree that made for very fast access. There wasn't any barrier between the language and the database either: if you wanted something to be stored there, you just prefixed the variable with a symbol indicating it was to be persisted to the backing store.
On the other hand, a filesystem was almost non-existent, and support for it even less so. About the only thing one could do was to load a program into memory from a file, and send whatever was in memory back to a file. And one had better clear the buffer before loading, otherwise it would get mixed with whatever was there first.
So, considering its self-hosting nature and the extremely hostile file system, one could wonder how these programs were edited. The editors, as a matter of fact, were written in MUMPS itself -- so how could the editor store the program in memory without written over itself?
Well, the trick was the ability to execute the contents of a variable as source code. An editor, then, loaded itself into variables, executed itself in them, cleared the memory, and then loaded, saved and edited files in memory, all the time executing from variables.
Add to that the fact that all commands could be shortened to their first letters (except the Z commands, shortened to two letters, that mostly handled the filesystem), and curiosities like the fact that IF
(I
) set a variable which was then consulted by ELSE
(E
) -- and, of course, could be overridden by any intervening I
, or by the program itself. On second thought, I think the whole language was a WTF. And, yet, it had a strange attraction.
Solution 67 - Language Agnostic
In Ruby, 0 evaluates as true in conditional expressions.
Solution 68 - Language Agnostic
The absolute worst WTF has got to be Cobol's ALTERED GOTO.
The syntax is pretty straight forward: "ALTER label1 TO GOTO label2", but the results of debugging run-time spaghetti are mind-boggling.
Solution 69 - Language Agnostic
In C-like languages (including C itself), you can use the "goes down to" operator:
for (x = 20; x --> 0;) {
print x;
}
This will print the numbers from 19 to 0.
Solution 70 - Language Agnostic
"Piet is an esoteric programming language designed by David Morgan-Mar, whose programs are bitmaps that look like abstract art."
Piet program that prints Piet
Solution 71 - Language Agnostic
Well, this one's also my all-time-favorite hard to find bug... treating integers beginning with a zero as octal numbers. This led to a bug that would only show between 8 and 10 in the morning:
Once, I helped building an automated regression test to be executed via cron at night. It worked nearly for everyone in a 20 person team - expect one developper complained every once in a while the automatic test had failed, but when run manually, everything worked fine. Not even once this could be reproduced manually.
Well, the reason was, we did some calculation (in bash) for statistics based on the output of the date command, and this failed only from 8:00 till 9:59 in the morning because we'd read the hour value as "08" (which is an illegal octal value, whereas "01" - "07" are valid octal values, and from "10" onwards everything is treated as decimal again)...
Solution 72 - Language Agnostic
JavaScript dates are full of WTF.
var d = new Date("1/1/2001");
var wtfyear = d.getYear(); // 101 (the year - 1900)
// to get the *actual* year, use d.getFullYear()
var wtfmonth = d.getMonth(); // 0
// months are 0-based!
Solution 73 - Language Agnostic
As an NHibernate enthusiast, I was thrilled when I heard about become
from Smalltalk... e.g.
a become: b
it literally changes the a object into b, which makes it trivial to write lazy-initialized proxies because all references to a will now reference b. Pretty neat!
I think it qualifies as a strange language feature in that no other language has this ability to my knowledge.
Solution 74 - Language Agnostic
In FoxPro, if I remember correctly, every command can be abbreviated to 4 characters and everything else is ignored, so READ, READY, READINESS is all the same - whatever is after the first 4 characters is ignored. The guy who explained it to me liked that feature, but I thought it was creepy.
Solution 75 - Language Agnostic
Common Lisp's format
function has an option to print numbers as Roman numerals.
In INTERCAL that is the only form of output you'll ever get.
Solution 76 - Language Agnostic
In C, the sizeof
operator does not evaluate its argument. This allows one to write code that looks wrong but is correct. For example, an idiomatic way to call malloc()
, given a type T
is:
#include <stdlib.h>
T *data = NULL;
data = malloc(sizeof *data);
Here, *data
is not evaluated when in the sizeof
operator (data
is NULL
, so if it were evaluated, Bad Things would happen!).
This allows one to write surprising code, to newcomers anyway. Note that no one in their right minds would actually do this:
#include <stdio.h>
int main()
{
int x = 1;
size_t sz = sizeof(x++);
printf("%d\n", x);
return 0;
}
This prints 1
, not 2
, because x
never gets incremented.
For some real fun/confusion with sizeof
:
#include <stdio.h>
int main(void)
{
char a[] = "Hello";
size_t s1 = sizeof a;
size_t s2 = sizeof ("Hi", a);
printf("%zu %zu\n", s1, s2);
return 0;
}
(The confusion is only if one is confused about arrays, pointers, and operators.)
Solution 77 - Language Agnostic
Might have already been said (and maybe this isn't so strange to some) but I thought this was pretty cool:
In Javascript, declaring the parameters a function accepts is only a convenience to the programmer. All variables passed through the function call are accessible by the keyword "arguments". So the following would alert "world":
<script type="text/javascript">
function blah(){
alert(arguments[1]);
}
blah("hello", "world");
</script>
Note, that while it may seem like these arguments are stored in an array (since you can access object properties in much the same way as array elements), they are not. arguments
is an Object, not an Array (so, they are Object properties stored with numeric indices), as the following example illustrates (typeOf function taken from Crockford's remedial JavaScript page):
argumentsExample = function(){
console.log(typeOf(arguments));
anArray = [];
console.log(typeOf(anArray));
anObject = {};
console.log(typeOf(anObject));
}
function typeOf(value) {
var s = typeof value;
if (s === 'object') {
if (value) {
if (typeof value.length === 'number' &&
!(value.propertyIsEnumerable('length')) &&
typeof value.splice === 'function') {
s = 'array';
}
} else {
s = 'null';
}
}
return s;
}
argumentsExample("a", "b");
Solution 78 - Language Agnostic
Java caches Integer object instances in the range from -128 to 127. If you don't know this the following might be somewhat unexpected.
Integer.valueOf(127) == Integer.valueOf(127); // true, same instance
Integer.valueOf(128) == Integer.valueOf(128); // false, two different instances
Solution 79 - Language Agnostic
Being able to cast out of range ints to enums in C# is quite weird in my opinion. Imagine this enum:
enum Colour
{
Red = 1,
Green = 2,
Blue = 3
}
Now, if you write:
Colour eco;
eco = (Colour)17;
The compiler thinks that’s fine. And the runtime, too.
See https://stackoverflow.com/questions/1758321/casting-ints-to-enums-in-c">here</a> for more details.
Solution 80 - Language Agnostic
I'm surprised no one mentioned the REALLY ugly switch-case implementation in most C-like languages
switch (someInt) {
case 1:
case 2: System.out.println("Forgot a break, idiot!");
case 3: System.out.println("Now you're doing the wrong thing and maybe need hours to find the missing break muahahahaha");
break;
default: System.out.println("This should never happen -,-");
}
The good thing is newer languages got it implemented right.
Solution 81 - Language Agnostic
x = x + 1
This was very difficult to digest when I was a beginner and now functional languages don't use it, which is even more difficult!
If you don't see how this is strange: Consider the equals sign as a statement of assertion instead of an assignment action, as you used to do in basic algebra, then this is the equivalent of saying "zero equals one".
Solution 82 - Language Agnostic
Perl:
It's possible to write a program consisting entirely of punctuation.
How does this even work?!
Solution 83 - Language Agnostic
Ok, since question will be in intermittent mode, I'll join to the "fun"
Go ( aka Issue9 ) use of upper case for visibility:
-
If you name something with uppercase it will have public access.
-
If you use lower case it will be package-protected:
Visible outside the package:
func Print(v ...) {
}
Not visible outside the package
func print( v ... ) {
}
You can find more in this original answer.
Solution 84 - Language Agnostic
Here's a good bunch of strange C features: http://www.steike.com/code/useless/evil-c/
Solution 85 - Language Agnostic
In Perl you can do:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
Is this possible in other languages?
Solution 86 - Language Agnostic
In JavaScript, seeing !!a
for the first time (as a way to convert to boolean).
Solution 87 - Language Agnostic
I like sneaking-in octal values in C:
int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };
Solution 88 - Language Agnostic
The C++ templating mechanism is Turing-complete: As long as you don't need input at run time, you can do arbitrary calculations at compile time. Arbitrary. Or you can easily write a C++ program that never compiles - but is syntactically correct.
Solution 89 - Language Agnostic
This is one of my favorites, you can do a println in Java without main().
This will compile and run, giving the println, but also an exception (java.lang.NoSuchMethodError: main)
class Test {
static {
System.out.println("I'm printing in Java without main()");
}
}
Solution 90 - Language Agnostic
This may have been already mentioned, but --
PHP's handling of octal values:
$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)
See here: http://bugs.php.net/bug.php?id=29676
Also note the comments on the bug - Derick calls it a feature (as shown by quoting "fix"), not a bug and he claims it would "slow down PHP dramatically in all cases where numbers are used inside scripts" - but then, why does PHP raise an error for 0A?
I think one could make a whole book about the weirdness of PHP...
Solution 91 - Language Agnostic
In Java you might expect
byte b = 0;
b++;
to be equal to
byte b = 0;
b = b + 1;
But it is not. In fact you get a compiler error, as the result of the addition is of type int and therefore not assignable to the byte variable b. When using the compound operator ++
The compiler automatically inserts a cast here. So
b++;
becomes
b = (byte) b + 1;
Solution 92 - Language Agnostic
VBScript's date/time literals (why is this still so rare?):
mydate = #1/2/2010 5:23 PM#
If mydate > #1/1/2010 17:00# Then ' ...
Edit: Date literals are relative (are they technically literals, then?):
mydate = #Jan 3# ' Jan 3 of the current year
VB.NET, since it is compiled, does not support relative date literals. Date only or time only literals are supported, but the missing time or date are assumed to be zero.
Edit[2]: Of course, there are some bizarre corner cases that come up with relative dates...
mydate = #Feb 29# ' executed on 2010-01-05, yields 2/1/2029
Solution 93 - Language Agnostic
Why does C#'s List<T>.AddRange() not let me Add elements of a subtype of T? List<T>.Add() does!
All it would take would be ONE extra line of code on Microsoft's part:
public void AddRange<S>(
IEnumerable<S> collection
) where S : T
Solution 94 - Language Agnostic
In C#, this should at least generate a compiler warning, but it doesn't:
public int Something
{
get { return Something; }
set { Something = value; }
}
When called, it causes your app to crash, and you don't get a good stack trace, since it's a StackOverflowException.
Solution 95 - Language Agnostic
Variable/function declarations in Javascript:
var x = 1;
function weird(){
return x;
var x = 2;
}
weird() returns undefined... x is 'taken' even though the assignment never happened.
Similarly, but not so unexpectedly
function weird2(){
var x;
return x();
function x(){ return 2 };
}
returns 2.
Solution 96 - Language Agnostic
I've written a programming language for a client (used for experimentally driving custom hardware) with some custom types (Curl, Circuit, ...) that each have only 2 values. They are implicitly convertible to boolean, but (at the request of the client) the exact boolean value of a constant of such a type can be changed at runtime.
E.g.: The type Curl has 2 possible values: CW and CCW (clockwise and counterclockwise). At runtime, you could change the boolean value by a simple assignment statement:
ccw := true
So you could change the boolean meaning of all values of those types.
Solution 97 - Language Agnostic
ActionScript 3:
When an object is used by its interface, the compiler doesn't recognize the methods inherited from Object
, hence:
IInterface interface = getInterface();
interface.toString();
gives a compilation error. The workaround is casting to Object
Object(interface).toString();
PHP:
.
and +
operators. It has its reasonable explanation, but still "a" + "5" = 5
seems awkward.
Java (and any implementation of IEEE754):
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
Outputs 0.9999999999999999
Solution 98 - Language Agnostic
Perl is full of odd but neat features.
if
may be used before or after the statement like this:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
The same is true for foreach
:
print "Hello $_!\n" foreach qw(world Dolly nurse);
Solution 99 - Language Agnostic
When I was in college, I did a little bit of work in a language called SNOBOL. The entire language, while cool, is one big WTF.
It has the weirdest syntax I've ever seen. Instead of GoTo, you use :(label). And who needs if's when you have :S(label) (goto label on success/true) and :F(label) (goto label on failure/false) and you use those functions on the line checking some condition or reading a file. So the statement:
H = INPUT :F(end)
will read the next line from a file or the console and will go to the label "end" if the read fails (because EOF is reached or any other reason).
Then there is the $ sign operator. That will use the value in a variable as a variable name. So:
ANIMAL = 'DOG'
DOG = 'BARK'
output = $ANIMAL
will put the value 'BARK' on teh console. And because that isn't weird enough:
$DOG = 'SOUND'
will create variable named BARK (see the value assigned to DOG above) and give it a value of 'SOUND'.
The more you look at it, the worse it gets. The best statement I ever found about SNOBOL (from link text) is "the power of the language and its rather idiomatic control flow features make SNOBOL4 code almost impossible to read and understand after writing it. "
Solution 100 - Language Agnostic
In PHP "true", "false" and "null" are constants which normally cannot be overridden. However, with the introduction of namespaces in PHP >=5.3, one can now redefine these constants within any namespace but the global namespace. Which can lead to the following behaviour :
namespace {
define('test\true', 42);
define('test\false', 42);
define('test\null', 42);
}
namespace test {
var_dump(true === false && false === null); // is (bool) true
}
Of course if you want your trues to be true, you can always import true from the global namespace
namespace test {
var_dump(\true === \false); // is (bool) false
}