Strangest language feature

Viewed 496025

What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?

Please only one feature per answer.

320 Answers

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.

In JavaScript:

 '5' + 3 gives '53'

Whereas

 '5' - 3 gives 2

In JavaScript, the following construct

return
{
    id : 1234,
    title : 'Tony the Pony'
};

returns undefined is a syntax error due to the sneaky implicit semicolon insertion on the newline after 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;

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

Trigraphs in C and C++.

int main() {
   printf("LOL??!");
}

This will print LOL|, because the trigraph ??! is converted to |.

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;

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;
}

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) );

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
  • $! — like errno as a number but strerror(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.

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.

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)

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.

Duff's device in C!

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);
    }
}

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]);
}

In Python:

>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False

Not a WTF, but a useful feature.

In Java:

int[] numbers() {
  return null;
}

Can be written as:

int numbers() [] {
  return null;
}

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

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.

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 :)

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);
}

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.

I struggled a bit about this:

1;

In perl, modules need to return something true.

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

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!

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.

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: Why does 2 == [2] in JavaScript?

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.

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:)

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

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.

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.

Python 2.x

>>>True = False
>>>True
False

You can really make someone become crazy with this one.

Oracle has a couple of SQL WTF issues.

  1. Oracle's treatment of empty strings as null.

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

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.

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.

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.

Inheriting from random class in Ruby:

class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
   ...
end

(first seen at Hidden features of Ruby)

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'

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

In ruby/python/c, you can concatenate strings just like this:

a = "foo" "bar"
print a # => "foobar"

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

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.

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

Tri-valued logic of nulls in ANSI SQL.

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]]

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.

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.

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:

  1. Declares a BITMAPINFOHEADER structure
  2. 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}
  3. 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)
  4. Clears all other members of the structure (a C standard behavior when a structure is incompletely initialized).

Java; making all object instances be mutexes.

In JavaScript:

alert(111111111111111111111) // alerts 111111111111111110000

This was quite damaging to some 64bit keys I passed back and forth in JSON.

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)

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

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"();

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

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.

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

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.

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.

In Ruby, 0 evaluates as true in conditional expressions.

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.

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

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!

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.

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.

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.

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

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");

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

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 here for more details.

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.

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

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.

In Perl you can do:

my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";

print $test;

Is this possible in other languages?

In JavaScript, seeing !!a for the first time (as a way to convert to boolean).

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.

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;

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

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

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.

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.

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.

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

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);

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

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
}

In PHP:

echo 'foo' == 0;    // echos '1'
echo 'foo' == true; // echos '1'
echo 0 == true;     // echos '0'
$foo = 'foo';
echo $foo['bar']    // echos 'f'

PHP has some of the most annoying type coercion...

LOLCODE!

The whole language itself. While not exactly a WTF thing, I've never come across a language which plays out in my head in a squeeky cartoony voice. Nor have I ever looked at code before and want to exclaim "aaaawwww cuuute!"

This program displays the numbers 1–10 and terminates

HAI
CAN HAS STDIO?
IM IN YR LOOP UPPIN YR VAR TIL BOTHSAEM VAR AN 10
    VISIBLE SUM OF VAR AN 1
IM OUTTA YR LOOP
KTHXBYE

In C or C++ you can have a lot of fun with Macros. Such as

#define FOO(a,b) (a+b)/(1-a)

if FOO(bar++,4) is passed in it'll increment a twice.

Perl filehandle-style operator calls.

In the beginning, there was

print "foo", "bar", "baz"; # to stdout
print STDERR "foo", "bar", "baz";

Notice the ostentatious lack of a comma so that you know that's a filehandle to print-to, not a filehandle to print in a stringified manner. It's a dirty hack.

Language upgrade rolls around, they make proper OO filehandles and turn x FOO y, z, abc into FOO->x(y, z, abc). Kinda cute. The same print statement effectively runs

STDERR->print("foo", "bar", "baz");

Mostly you notice this when you miss a comma, or try to run something like hashof $a, $b, $c (subroutine call without parentheses) and forget to import the hashof function into your namespace from its utility package, and you get a weird error message about "Can't call method 'hashof' via package 'contents of string $a'".

In Python:

>>> a[0] = "hello"
NameError: name 'a' is not defined
>>> a[0:] = "hello"
NameError: name 'a' is not defined
>>> a = []
>>> a[0] = "hello"
IndexError: list assignment index out of range
>>> a[0:] = "hello"
>>> a
['h', 'e', 'l', 'l', 'o']

These slice assignments also give the same results:

a[:] = "hello"
a[42:] = "hello"
a[:33] = "hello"

Easy pickins, Erlang is full of them. For example, 3 forms of punctuation,

a_function(SomeVariable) ->
  statements_end_with_commas(),
  case PatternMatching of
    0 -> now_we_end_with_semicolon;
    true -> except_the_last_one
  end.

%%  Function definitions end with periods!

In JavaScript (and Java I think) you can escape funny characters like this:

var mystring = "hello \"world\"";

If you want to put a carriage return into a string though, that's not possible. You have to use \n like so:

var mystring = "hello, \nworld";

That's all normal and expected- for a programming language anyway. The weird part is that you can also escape an actual carriage return like this:

var mystring = "hello, \
world";

More of a platform feature than a language feature: on the iPhone, create an infinite loop with a few computations inside and run your program. Your phone will heat up and you can use it as a hand-warmer when it's cold outside.

In C or C++, parentheses are optional for the argument to sizeof ... provided the argument isn't a type:

void foo() {
  int int_inst;

  // usual style - brackets ...
  size_t a = sizeof(int);
  size_t b = sizeof(int_inst);
  size_t c = sizeof(99);

  // but ...
  size_t d = sizeof int_inst; // this is ok
  // size_t e = sizeof int; // this is NOT ok
  size_t f = sizeof 99; // this is also ok
}

I've never understood why this is!

Bracket identifiers in VBScript

VBScript has so-called bracket identifiers, which are identifiers defined enclosed in square backets, like this:

[Foo]

They're quite handy, actually, as they allow you to name variables and routines after reserved words, call methods of third-party objects whose names are reserved words and also use almost any Unicode characters (including whitespace and special characters) in identifiers. But this also means that you can have some fun with them:

[2*2] = 5

[Здравствуй, мир!] = [Hello, world!]

[] = "Looks like my name is an empty string, isn't that cool?"

For[For[i=0]=[0]To[To[To[0]
  [Next[To]([For[i=0])=[For[i=0]
Next

On the other hand, bracket identifiers can be a gotcha in case you forget the quotes in a statement like this:

If MyString = "[Something]" Then

because If MyString = [Something] Then is a perfectly legal syntax. (And that's why an IDE with syntax highlighting is a must!)


More info on bracket identifiers in Eric Lippert's blog:

In earlier version of Visual Basic, functions without a "Return" statement just "Return None", without any kind of compiler warning (or error).

This lead to the most crazy debugging sessions back when I had to deal with this language on a daily basis.

in PHP the strings letters cannot be used like in C, you need to use ord() and chr() in order to convert from number to char and vica versa: "a" != 97, but ord("a") == 97.

Although, there is one exception:

for ($i = 'a'; $i < 'z'; $i++) {
    print "$i ";
}

will print letters a to y. just like you would expect as if it was C style datatypes.

however if the test condition is changed to <= it will not print a to z as you would think, but a to yz! (total 676 items printed)

also if you change the 'z' to 'aa' which came out next after 'z' in the 676 items list, and change test condition to < again, you will see only "a" being printed out! not a to z as you would expect.

And if you change the incrementor to $i+=2 it will print only "a" again! only way to do that is to use $i++, $i++ in sequence, and now it works like expected.

Nevertheless, this is a nice way in PHP to output combinations of letters a-z, although its very hard to actually use it.

String math in Perl is pretty weird.

$ perl -E '$string = "a"; $string++; say $string'
b

$ perl -E '$string = "abc"; $string++; say $string'
abd

$ perl -E '$string = "money"; $string++; say $string'
monez

$ perl -E '$string = "money"; $string--; say $string'
-1

The bigest collection (today 1313) of decent and weird programming languages I know, you will find here: http://99-bottles-of-beer.net/ be prepared to see real weird stuff ;-) Everybody should make his one choice

In my opinion this should not be allowed in C++:

class A {
public:
  virtual string foo(){return "A::foo";}
};

class B : public A {
public:
  virtual string foo(){return "B::foo";}
};

int main () {
  B* b = new B();
  // In my opinion the following should not be allowed
  cout << b->A::foo() << endl;  // Will print "A::foo"
}

This may seem right, but this means that you cannot override a method without allowing users of the subclass to call the original method instead of the new one.

Just think about a subclass of a collection where you want to increment the number of elements when adding an element to the collection itself.

A logical solution would be to override the add() method to increase the counter before adding the element, but a user of the new collection could add an element to it using the old method so bypassing your increment and resulting in your elements-counter disagree with the actual number of elements of the collection.

This is not possible in Java.

Java's access modifiers are a recent WTF to me (as I had to learn a bit of it).

Apparently packages are more intimate than class hierarchies. I can't define methods and attributes that are visible to sub-classes but not to other classes in the package. And why would I want to share the insides of a class to other classes?

But I can define attributes and methods that are visible to every class inside the package, but not to subclasses outside the package.

No matter how hard I think about this, I still can't see the logic. Switch over the access modifiers and make protected act like it works in C++ and keep the package private modifier as it is and it would make sense. Now it doesn't.

In C:

warning C4013: 'myfunc' undefined; assuming extern returning int

I remember for some reason not seeing warnings (too much of them in some legacy code?) and puzzling over why conversion from int causes compiler error where non int-returning function is used.

Compiler assuming such stuff was quite unexpected.

Reading a line from a text file in Java.

BufferedReader in = null;
try {
   in = new BufferedReader(new FileReader("filename"));
   String str;
   str = in.readLine();
   if (str != null) {
      ...
   } 
} catch (IOException e) {
   ...
} finally {
   try {
      if (in != null) {
         in.close();
      }
   } catch (IOException e) {}
}

Ugh. Although I admit it is not strange...just evil. :-)

A shorter, more idiomatic version:

try {
   BufferedReader in = new BufferedReader(new FileReader("filename"));
   try {
       String str = in.readLine();
       while (str != null) {
          str = in.readLine();
       } 
    } finally {
        in.close();
    }
} catch (IOException e) {
    e.printStackTrace();
}

For me it's definitely the PLEASE modifier in INTERCAL. If PLEASE does not appear often enough, the program is considered insufficiently polite, and the error message says this; if too often, the program could be rejected as excessively polite.

PHP as an entire language is mostly WTF.

The langauge definition is defined,(see www.php.org) not by a grammar, or a standard, but by a bunch of "you can write this example" sections (can you write anything else, sure, just guess at the generalization), with honest-to-god user contributions saying "but it does this wacko thing ...".

I periodically encounter glitches with a PHP parser we built. Here's the latest:

 "abc$A[define]def"

Now, PHP is a (truly bad) copy of PERL, and so it allows strings to be constructed with implicit substition of variables. $X in the string says "plug the value of $X into the string", equivalent to "abc" . $X . "def" where "." is PHP's string-concatenate operator.

$A[7] in a string says, "plug the value of the seventh slot of array $A into the string",equivalent to "abc" . $A[7] . "def".

Now, the language (website) clearly says "define" is a keyword, and you can't use it whereever you'd find an expression. So the above gem containing "define" does what? Throw a syntax error? Nah, that would make sense.

No, what it actually means is:

 "abc" . $A["define"] . "def"

It does this ONLY if you write an thing that looks like an identifier (keyword or not!) in an simple array access in a string. Nowhere else in the language does this behaviour occur. What, writing "abc$A["define"]def" was unreasonable so the PHP inventors had to throw this in? Give me a break. (To compound the felony, there's "complex array access in a string" and of course it works differently. Check out "abc{$A[define]}def"; that is illegal according to the PHP website.

(Turns out PHP arrays are associate hashes, so looking up an array (well, hash table) member by name isn't a terrible idea).

The language is full of gotchas like this. If you like "gee, look what squirmy thing I found under my subroutine today", you should switch to PHP.

Unary operators in INTERCAL (AND, OR and XOR).

In MUMPS you can have a GOTO with offset. If you have (my MUMPS is rusty...)

some_label if x=1 do_something
           else  do_something_else

Then the code

           goto some_label+1

Will jump to the ELSE statement...

I'm fond of the lack of operator precedence in Smalltalk

2 * 3 + 4 * 5 = 6 + 4 * 5 = 10 * 5 = 50

instead of

2 * 3 + 4 * 5 = 6 + 4 * 5 = 6 + 20 = 26

This is due to the object nature of smalltalk and the fact that messages are passed left to right. If the message * is sent to the 2 with the number 3 as a parameter, the response of that message is 6. Pretty awesome, you can even monkey patch it if you're feeling evil.

In SQL

NULL is not equal to NULL

So you can't do:

WHERE myValue == NULL

This will always return false.

NULL != NULL

Forth has some strange things about its control structures. First, because it is a reverse polish notation language, the condition precedes the IF, as in:

x 0 = IF

Now, to close the conditional block, one uses the keyword THEN:

x 0 = IF ." Equals zero!" THEN

Now the real WTF begins. What IF does is compile a conditional forward jump, and place on a stack the address of the jump offset. When THEN is found, it pops that address from the stack, computes the actual offset, and then compile that. The ELSE, on the other hand, compiles an inconditional forward jump, pops an address from the stack, pushes a new address on the stack, computes the offset for the popped address, and then compiles that offset. Meaning the syntax is this:

x 0 = IF ." Equals zero!" ELSE ." Not equal to zero!" THEN

The first and second statements are compiled like this:

x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ)
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ) BRANCH LITERAL offset SLITERAL" Not equal to zero!" (DOTQ)

To compound the weirdness, that behavior is not hidden. It is part of the ANSI specification of the language, and can be freely be taken advantage of, either by constructing custom flow control structures or by combining them in interesting ways. For example, take Forth's WHILE loop:

BEGIN x 10 < WHILE x 1+ to x REPEAT

The part between BEGIN and WHILE is arbitrary code, so you can actually have code execute before and after the conditional test in a single control structure. That's by design, but the following, though allowed, is not:

BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN 

Which takes advantage of how each control flow word works to combine two WHILE statements, and, to boot, add a different post-loop code for each exit. And just to show I'm not kidding, I just copied that small snippet from a code on the Internet, with minor modifications to simplify it.

In MAXScript, all operators are treated equal. So, a = b + c sets a equal to b, then calculates the sum a+c, and discards the result.

Inform 7. An example of a valid program:

    Chomsky is a room. 
    A thought is a kind of thing. 
    Color is a kind of value. 
    The colors are red, green and blue. 
    A thought has a color. It is usually Green. 
    A thought can be colorful or colorless. It is usually colorless. 
    An idea is a thought in Chomsky with description "Colorless green ideas sleep furiously." 
    A manner is a kind of thing. 
    Furiously is a manner. 
    Sleeping relates one thought to one manner. 
    The verb to sleep (he sleeps, they sleep, he slept, it is slept, he is sleeping) implies the sleeping relation. 
    Colorless green ideas sleep furiously. 

Other silliness like this Turing machine simulator can be found.

The designers of VB.NET did several really dumb things to maintain backwards compatibility with Visual Basic 6.0. Of course, not enough that it actually was compatible, just enough to make things more counter-intuitive. But the worst of them was the fact that you don't have to initialize variables because they already are, except on those rare occasions when they are not.

    For i As Integer = 1 To 3
        Try
            Dim k As Integer
            k += 1
            MsgBox(k)
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    Next

This will print 1 2 3.

Having a feature you can't trust 100% of the time is not a feature, it's a bug. Saying it's as designed just makes it a design bug, not an implementation bug.

I once wrote a programming language that had a "strfry" operator:

"hello world"?
# => "wdo rlholle"

Useful, eh?

Another C-ism.

int i= 0;
while( i != 12 ) {
    /* Some comment 
    i += 1;
    /* Another comment */
}

Why doesn't it work? Lint will tell you. The C compiler, however, usually passes over this blithely. As did I.

That was a real WTF moment when I figured out what was wrong.

In javaScript, NaN is a global variable.

The most weird feature I know of is from C++ world : SFINAE.

The worst is that it happens to actually be very usefull, extensive use of SFINAE in BOOST is proof enough for me.

Java Generics Are a WTF:

List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2

2: Is illegal (???) this is puzzling but you have to think what could happen next:

lo.add(new Object());
String s = ls.get(0);

We would be assigning an Object to a String reference, oh noes! And like this there a lots of gotchas around them.

About 20 years ago I worked with a compiler for a language called Coral which allowed me to declare writeonly variables!

It made sense, though, as they were global and used as a signalling mechanism. One process would write a value and another would read it.

The following C# code throws NullReferenceException rather than print 1:

    static void SomeMethod(string format, params object[] args)
    {
        Console.WriteLine(args.Length);
    }

    static void Main(string[] args)
    {
        SomeMethod("blabla", null, "Ok here"); // print 2
        SomeMethod("blabla", null); // exception
    }

In Java,

int x = 010;

This assigns x to have the value 8.

Any integer preceded with a zero in Java is presumed octal.

In PHP, you can reference variables using a sigil and a string literal or variable containing the name of the variable, for example:

${'foo'} = 'test';
echo $foo;

This will print "test". The strange thing about this behavior is that you can also use non-strings as variable names, for example:

${array()} = 'test';
echo ${array()};
${NULL} = 'test';
echo ${NULL};

Now we have variables named array() and even NULL! All containing the string "test".

C++:

void f(int bitand i){ //WTF
    i++;
}
int main(){
    int i = 0;
    f(i);
    cout << i << endl; //1
    return 0;
}

In Java, if the value of x is NaN then x == x returns false and x != x returns true.

javascript:

parseInt('06'); // 6
parseInt('08'); // 0

PL/SQL allows to declare variables and function names that are keywords. The following is compilable PL/SQL:

create or replace 
  function function 
  return number  as
  return number;
begin 
  function.return := 4;
  return   return;
end function;
/

This created a function named function. Later:

SQL> select function from dual;

  FUNCTION
----------
         4

One unexpected feature was the trailing commas in enum def lists and array initialization lists in C, C#, Ruby, etc.

string[] foods = { "tofu", "grits", "cabbage", }

public enum ArtPeriod {
  Modern,
  Romantic,
  Dada,
}

In Javascript, I believe the following are equivalent:

a['title'] = "Syntactic sugar is good for yr teeth.";
a.title = "Syntactic sugar is good for yr teeth.";

VBScript's With blocks:

With xml.appendChild(xml.createElement("category"))
  .setAttribute("id",id)
  .setAttribute("keywords",keywords)
  With .appendChild(xml.createElement("item"))
    .setAttribute("count",count)
    .setAttribute("tip",tip)
    .appendChild(xml.createTextNode(text))
  End With
End With

Dozens of things in Javascript can make your eyes water.

The scoping of local variables, as just one simple example:

function foo(obj)
{
  for (var n = 0; n < 10; n++)
  {
    var t;        // Here is a 't'
    ...
  }
  t = "okay";     // And here's the same 't'
}

MySQL enums, specifically their ability to confuse the living hell out of unprepared coworkers.

CREATE TABLE foo (
    ....
    dispatched ENUM('0','1') NOT NULL DEFAULT '0',
)

Then:

UPDATE TABLE foo SET ..., dispatched = 1;

Oops, dispatched was set to ZERO instead, because the 1 wasn't quoted. This really annoyed someone who worked on my code; I use plain old INTs now.

On a related note, even if you add an empty string option to your enum, e.g.

blah ENUM('','A','B') NOT NULL,

If you assign an invalid value to blah, MySQL will use a secret hidden empty string value to represent the invalid value, which will be difficult to distinguish from the one you added yourself. Yay!

in X++ (Microsoft Dynamics AX):

1) the need of a semi-colon (;) on a separate line to separate variable declaration from statements (at least up to version 4.0)

    int i;
    int myArray[5];
    ;
    i = 1;


2) array indexes are 1-based, so you are not allowed to read from an array using index 0 (zero) like in

    int myArray[5];
    ;
    print myArray[0];    // runtime error

this is not strange, but you are allowed to use the zero index on the left hand side of an assigment, like in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

what happens? The array gets initialized to it's default value, no matter what value was used in the assignment. The above code outputs 0 (zero)!

In MATLAB (interactive array-oriented language, currently TIOBE 20) there is a keyword end to denote the last element of array (it corresponds to NumPy -1). So this is a well known MATLAB syntax:

myVar = myArray(end)

To get an element from the middle of array one would usually write:

myVar = myArray( ceil( length(myArray)/2 ) )

Surprisingly the keyword end is not a keyword at all but is a kind of variable:

myVar = myArray( ceil( end/2 ) )

Variable assignment in JavaScript can create global variables. If a variable is a assigned a value within a function and it is not declared as var in the same scope it is implicitly declared global.

function foo() {
  x = "juhu";  // creates a global variable x!
  var y = "kinners"
}

foo();
alert(x); // alerts "juhu"
alert(y); // alerts undefined

Note that the var statement can also be used after a value has been assigned to the variable:

function foo() {
  x = 12;
  var x; // x is now local
  return x;
}

alert(foo()); // will alert 12;
alert(x); // will alert undefined

In Matlab, the following may be surprising, especially if you are used to Python:

>> not true

ans =

     0     0     0     0
>> not false

ans =

     0     0     0     0     0

There are two weird features here. The first one is that a b is interpreted as a('b'), so not true is interpreted as not('true'). The second weird feature is that not of any character returns 0 (presumably because there is no false or true in matlab, only 0 or 1).

Not sure whether someone mentioned it.

In Java, in finally block it can return a value. It will stop the propagation of an exception and override the normal return statement.

In Visual Basic 7 and above I found the implementation of short-circuit logical evaluation to maintain compatibility with legacy Visual Basic <=6 code a bit of a WTF:

AndAlso (MSDN)
OrElse (MSDN)

Perl's $[ (deprecated), this was mentioned in another earlier post about generic perl variables, but it deserves specific mention with better explanation. Changes to $[ are limited to current scope. More information and a quick writeup of how you can use this and its implications ;) can be found in $[ is under respected at http://www.perlmonks.org/index.pl/?node_id=480333

Another vote for JavaScript:

parseInt('08') == 0

because anything with a leading 0 is interpreted as octal (weird), and invalid octal numbers evaluate to zero (BAD). I discovered this one August when code I hadn't touched in months broke on its own. It would have fixed itself in October, as it turns out.

Octal support has apparently been deprecated, so future generations of JavaScripters will not have this rite of passage.

In Perl, objects are just blessed refs, so changing the class of an object at run time is a piece of cake:

package Foo;
sub new { bless {}, $_[0] }
package Bar;
package main;
my $foo = Foo->new;
ref($foo); # => "Foo"
bless $foo, 'Bar';
ref($foo); # => "Bar"

I was surprised that other languages can't do this. What a useful feature!

Digraphs and alternative tokens

C (ISO/IEC 9899:1999, 6.4.6/3) and C++ (ISO/IEC 14882:2003, 2.5) have a feature that is rarely used, called "digraphs" by C and "alternative tokens" by C++. These differ from trigraphs mainly because string literals containing them will never be interpreted differently.

%:include <stdio.h>

int main() <%
    int a<:10:> = <%0%>;
    printf("Here's the 5th element of 'a': %d\n", a<:4:>);
    puts("Evil, eh? %:>");
    return 0;
%>

C++ has many more, including and, or, and not which are required to behave as &&, ||, and !. C has these too, but requires that <iso646.h> be included to use them, treating them as macros rather than tokens. The C++ header <ciso646> is literally an empty file.

It's worth noting that GCC implements support for this weird language feature, but lots of other compilers choke and die when trying to compile the above segment of code.

In C++ you can call static methods from null pointers - behold!

class Foo {
  public:
    static void bar() {
      std::cout << "WTF!?" << std::endl;
    }
};

int main(void) {
  Foo * foo = NULL;
  foo->bar(); //=> WTF!?
  return 0; // Ok!
}

That one caught me by surprise...

PHP's list construct:

$array = array(0,1,2);
list (,,$x) = $array;
$x == 2; // true

Forth can change the base of the numbers at any time:

HEX 10 DECIMAL 16 - .
0 Ok

It need not be one pre-defined one either:

36 BASE ! 1Z DECIMAL .
71 Ok

Variables everywhere are taken as globals in Coldfusion, no matter where they are placed.

<cffunction name="one" returntype="void">
    <cfset var wtf="coldfusion">
    <cfinvoke method="second">
</cffunction>

<cffunction name="two" returntype="void">
    <cfoutput>#wtf#</cfoutput>
</cffunction>

In Python:

>>> x = 4
>>> y = 1000000
>>> x is 4
True
>>> y is 1000000
False
>>>

Just try it if you don´t believe me!

In php:

easter_date — Get Unix timestamp for midnight on Easter of a given year

int easter_date ([ int $year ] )

In two words: multiple inheritance. It makes no sense, and creates nothing but trouble.

Edit - I am referring to MI in C++, not mixins and the like in Java and other languages.

To alternate between things in many languages:

boolean b = true;
for(int i = 0; i < 10; i++)
  if(b = !b)
    print i;

on first glance: how can b really not be equal to itself!? This acctually would print odd numbers only

Variable variables in PHP

An odd feature in PHP which allows you to create and assign variables from the content of other variables (warning, untested code):

$a = 'Juliet';
$$a = 'awesome'; // assigns a variable named $Juliet with value 'awesome'

echo '$a';       // prints Juliet
echo '${$a}';    // prints awesome
echo '$Juliet';  // prints awesome

Alright, let's say we have something like this:

$bob = 'I\'m bob';
$joe = 'I\'m joe';
$someVarName = 'bob';
$$someVarName = 'Variable \'bob\' changed';

How about some fun with all kinds of indirection:

$juliet = 'Juliet is awesome!';
$func = 'getVarName'

echo '${$func()}'; // prints 'Juliet is awesome!'

function getVarName() { return 'juliet'; }

Perl's sub not having a real parameter list, just the @_ array. Also, sub's auto-flattening the parameters that are passed into it.

I don't understand why this is a persistent feature; this reflects what I had to do as a kludge on my TI-86 BASIC years ago because the language wasn't featured enough.

C#, namespace reslove order

for example.

namespace foo.bar.xyz{
  public class Foo{
    Exception e;   // you'll get compile time error here....
  }
}

Because

namespace foo.bar.Exception{
  class HowDoMyWayException : ApplicationException {
   // because someone did this
  } 
}

In C++, you can do:

std::string my_str;
std::string my_str_concat = my_str + "foo";

But you can't do:

std::string my_str_concat = "foo" + my_str;

Operator overloading is generally subject to WTF.

In ColdFusion arrays start at 1.

not that this is heavily used, but syntax of C++'s "return reference to static-size array" is weird:

struct SuperFoo {
  int (&getFoo() const)[10] {
    static int foo[10];
    return foo;
  }
}

ofc, in above case method can be declared as static const

In Python:

abs((10+5j)-(25+-5j))

Returns ~18.03, which is the distance between the points (10,5) and (25,5) by the Pythagoras theorem. This fact happens because Python has native language support to complex numbers in the form of 2+2j for example. Since the absolute value of a complex number in form of a+bj = sqrt(a^2+b^2), we get the distance while subtracting one complex number from another and then apply the abs (absolute) function over it.

Feature: Bash, the Korn shell (ksh93) and the Z shell each allow subscripting arrays with variables with or without a dollar sign:

array[index]=$value
array[$index]=$value

This, with the dollar sign, will produce the expected value of 10000:

unset array
for i in {1..10000}
do
    ((array[$RANDOM%6+1]++))
done
unset total
for count in ${array[@]}
do
    ((total += count))
done
echo $total

Strangeness: If you remove the dollar sign from RANDOM, the total will vary randomly, even to be greater than 10000.

Similarly, this produces 3 instead of 2:

a=1; ((r[a++]++)); echo $a

And you can't use a dollar sign there because it's an assignment (a is on the lhs), although you could do it if you were using indirection, but the double evaluation still occurs.

The Reason: With the dollar sign, the variable expansion is performed before the arithmetic evaluation so only gets done once. Without the dollar sign, it's performed twice, once to calculate the index for the lookup and again to calculate the index for the assignment (so, in effect, an assignment at one step in the loop might look like array[4] = $array[6] + 1 which totally scrambles the array).

PHP backticks

From http://www.php.net/manual/en/language.operators.execution.php

PHP supports one execution operator: backticks (``). Note that these are not single-quotes! PHP will attempt to execute the contents of the backticks as a shell command; the output will be returned (i.e., it won't simply be dumped to output; it can be assigned to a variable).

$output = `ls -al`;
echo "<pre>$output</pre>";

Well it's "quite easy" to spot ` instead of ' in the code.

This is funny, too:

After much trouble, I have concluded that the backtick operator (and shell_exec) have a limited buffer for the return. My problem was that I was grepping a file with over 500,000 lines, receiving a response with well over 100,000 lines. After a short pause, I was flooded with errors from grep about the pipe being closed.

RSL programming language is used in one strange banking system. There is built-in class TArray for arrays. But if you inherit from it every instance variable become an element of the array.

class (TArray) DerivedArray
  var someField = 56;
end

var a = DerivedArray();
PrintLn(a.Size);     // => 1
PrintLn(a[0]);       // => 56 

FORTRAN isn't a really WTF moment but rather it's more a "Why do I need to type all this garbage moment"

IF(12 .gt. 11) THEN
 // Do some magic
ENDIF

The ".gt." threw me off when I was playing with the language for a bit until I realized it was the ">" symbol. Oh how I love not being a biology major and having to dabble in this crap day to day

In Bash, variables can appear to be both scalars and arrays:

$ a=3
$ echo $a
3
$ echo ${a[@]}    # treat it like an array
3
$ declare -p a    # but it's not
declare -- a="3"
$ a[1]=4          # treat it like an array
$ echo $a         # acts like it's scalar
3
$ echo ${a[@]}    # but it's not
3 4
$ declare -p a
declare -a a='([0]="3" [1]="4")'
$ a=5             # treat it like a scalar
$ echo $a         # acts like it's scalar
5
$ echo ${a[@]}    # but it's not
5 4
$ declare -p a
declare -a a='([0]="5" [1]="4")'

ksh does the same things, but uses typeset instead of declare.

When you do this in zsh, you get substring assignment instead of arrays:

$ a=3
$ a[2]=4          # zsh is one-indexed by default
$ echo $a
34
$ a[3]=567
$ echo $a
34567
$ a[3]=9
$ echo $a
34967
$ a[3]=123         # here it overwrites the first character, but inserts the others
$ echo $a
3412367
$ a=(1 2 3)
$ echo $a
1 2 3              # it's an array without needing to use ${a[@]} (but it will work)
$ a[2]=99          # what about assignments?
$ echo $a
1 99 3

In Common Lisp, arrays with zero dimensions are strange, and naturally, they have read syntax.

? (aref #0A5)
5

A Fortran compiler that I used years ago had the interesting feature that: (a) Numbers were stored high-byte first; (b) Numbers were passed to subroutines by address; (c) There was no compile-time checking of length.

So you could write a program like this: (Excuse me if I mess up the syntax. It's been a long time since I've written Fortran.)

INTEGER*2 FUNCTION TIMESTWO (INTEGER*2 N)
RETURN N*2

... THEN CALL THIS SOMEWHERE WITH A LONG INTEGER ...

INTEGER*4 I, J

I=42
J=TIMESTWO(I)

The final value of J is ... zero !

Why? Because the passed in value is 4 bytes, but the called function looks at only the first two bytes. As the first two are zero, it doubles the zero and returns it. This return value is then converted back to four bytes.

This was very mysterious when I first encountered it. Almost every number I passed in to certain functions got interpreted as zero!

Perl's CORE::open and standard library having elements of object orientation masked with a procedural interface: open ( my $fh, '>', 'foobar' ); open is a constructor that operates on the reference returned by my(), and takes the arguments '>', and 'foobar'. Moreover, that being an object that is a blessed typeglob (meaning it can't hold state inside the object).

More information on my perlmonks post IO::File vs CORE::open here: http://www.perlmonks.org/?node_id=763565

I think this one isn't actually a "language feature" (C) and I'm quite possibly being widely ignorant in posting it, but I couldn't figure why this happens, so I'll ask. If it turns out to be related to some odd language feature.. well, it really made me "WTF", so it's worth this place.

int a = 0;
int *p = &a;

printf("%d, %d, %d.\n", *p, (*p)++, *p); // Outputs "1, 0, 0.\n" on MinGW's GCC 4.4.1

Why?

-- edit

Just got it, and it's not big deal. I can sense the C++ gurus laughing at me now. I guess the order in which function parameters are evaluated is unspecified, so compilers are free to call them as they wish (and I think I've read that one somewhere in boost's documentation). In this case, the argument statements were evaluated backwards, probably reflecting the calling convention of the function.

In Lisp you can copy a list, and you can copy a vector, and you can copy a struct, and you can copy a CLOS object...

... but you cannot copy an array or a hash table.

JCL Conditional execution.

//STEP02 EXEC PGM=PROG02,COND=(4,GT,STEP01) .

This features allows you to run or not run a step depending on the return code from previous steps. Quite a nice feature really.

Except for a couple of small features which turn the logic inside out and backwards.

First the step does NOT run if the condition is true.

Secondly the 4,GT,STEP01 actually means "if the return code from STEP01 is greater than 4"

So the whole thing means "Do not run this step if the return code from STEP01 is greater than 4". Which is the almost but not quite the same as a naive interpretation "Run the step if 4 is greater than the return code from STEP01".

Given that only time you ever look at these things seriously is about 2.30 am with a frantic nightshift operator at the other end of the line this double ambiguity leads to serious headaches.

Reverse Polish Notation (RPN). That means the arguments precede the function. Or, in other words, you add two and two by writing 2 2 +.

Languages featuring that WTF include Forth, Postscript (yes, of laser printers) and Factor.

Something bizarre -- VBScript having both a Null keyword and a Nothing keyword (Null is missing data and Nothing is a missing object). Why not just have one keyword...? Most other languages seem to do fine with one!

Visual Basic 6.0 and of course "Classic ASP" code (because it uses VBScript) have the same bizarrity. And in Visual Basic old and new we also have DBNull.

The situation is improving however, as in Visual Basic.NET Null has at last gone away so that Null is unused and only Nothing and DBNull are used.

A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.

I can't believe this one isn't on here yet: JSF property access.

In a JSF UI tag, you would put the value of a property from the server-side object into the interface by referencing it thusly:

<h:inputText value="#{myObject.property}></h:inputText>

The thing is that Java doesn't support properties, so you have to write methods starting with get and set in order to link the UI object to the "property" on the server.

public void setProperty(String property){...}
public String getProperty(){...}

This confused me when I first learned JSF and I still consider it WTF-worthy... even though there's really no other way to do it until Java implements support for C#-style properties.

In SQL server (MS at least):

This will always evaluate to false:

IF @someint <> NULL

Given:

DECLARE @int INT

SET @int = 6

IF @int <> NULL
BEGIN
    Print '@int is not null'
END
ELSE
BEGIN
    Print '@int is evaluating to null'
END

The output will be:

@int is evaluating to null

It must be written:

IF @someint IS NOT NULL
BEGIN
END

Who put English majors on the SQL Team! :)

in Ruby ...

i=true
while(i)
   i=false
   a=2
end
puts defined?(a) // returns true

This is not a strange feature, in fact it makes total sense if you think about it, but gave me a WTF moment nevertheless.

In C++(and in C#), subclasses of a base cannot access private and protected members on the instance of the base.

class Base {
protected:
 m_fooBar;
};

class Derived: public Base {
public:
 void Test(Base& baseInstance) {
  m_fooBar=1; //OK
  baseInstance.m_fooBar = 1; //Badness
  //This, however is OK:
  ((Derived&)baseInstance).m_fooBar = 1; //OK
 }
};

Subjunctive case in English.

Oh wait, did you mean programming languages? Then using (macro) in C to bypass the preprocessor #define of macro(). E.g., if someone has #define free(...), (free)(...) will not be the same as free(...).

In retrospect, FORTRAN's computed goto is pretty odd. Wikipedia tells me some BASICs outdo it.

Another famous favourite is Algol 60's call by name parameter passing.

I've always wondered about the purpose of this function in the Math class of the Java Core library:

static double expm1(double x);  // Returns e^x - 1.

In C#, why is this not legal?

public class MyClass<T>
    where T: Enum
{

}

It'd be pretty cool to be able to add extension methods on Enum's along with Func<T> where the T would be the enum you're extending so that you can get type inference on that enum.

Re the comment: Yes, you can extend an actual enum, but here's the difference:

You CAN do this:

public static void DoSomethingWithEnum(this Enum e)
{
   //do whatever
}

but what if you want to take a Func with your method that would be the same type as your enum:

public static void DoSomethingWithEnum<T>(this T e, Func<T,bool> func )
   where T: Enum
{
   //do whatever
}

That way, you can call your method like so:

DayOfWeek today = DayOfWeek.Monday;
today.DoSomethingWithEnum(e => e != DayOfWeek.Sunday);

or something like that. You get the idea... THAT'S not possible, and I'm not sure why...

This is nothing strange or surprising, but it is something that made me always say WTF:

Case sensitivity in syntax, or in identifier names.

Most languages that have it just seem to have it because C has it. There is no good reason for it.

Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.

-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able
>  let x :: Maybe Int; x = 5;
<interactive>:1:24:
    No instance for (Num (Maybe Int))
      arising from the literal `5' at <interactive>:1:24
    Possible fix: add an instance declaration for (Num (Maybe Int))
    In the expression: 5
    In the definition of `x': x = 5

>  Just 5  
Just 5
it :: Maybe Integer

    -- Create a function x which takes an Int
>  let x :: Int -> Int; x _ = 0;
x :: Int -> Int
-- Try to give it a Just Int
>  x $ Just 5                   

<interactive>:1:4:
    Couldn't match expected type `Int' against inferred type `Maybe t'
    In the second argument of `($)', namely `Just 5'
    In the expression: x $ Just 5
    In the definition of `it': it = x $ Just 5

Good luck reading this, I hope its right.

What datatype is foo?

SELECT TOP 1
   NULL AS foo
INTO
   dbo.bar
FROM
   sys.columns --trivial

Why does everything go to zero?

SELECT CAST('' AS int), CAST('' AS datetime), CAST('' AS float)

...except this

SELECT CAST('' AS decimal)

In MySQL string comparisons are case-insensitive.

> SELECT * FROM blah WHERE foo = 'BAR';
> SELECT * FROM blah WHERE foo = 'Bar';
> SELECT * FROM blah WHERE foo = 'bAr';

Are all equivelent. Not only that they will match any value of foo that looks like 'bar' (e.g. if foo = 'bar' it will match for BAR, baR, bAR, etc.).

Not so long ago, when I first descoverd the C Language in my CS class, it was very strange to see the way pointers behaived. we just wrote programs and guess what it would do, until they get the right behavior

The C preprocessor and its usages. Specifically preprocessor metaprogramming and using the preprocessor to generate portable code -- total mindfcuk.

C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.

Try, except, else

try:     pass
except:  pass
else:    pass
finally: pass

If no exception was caught the else part is executed.

Makes sense, but at first I really hadn't any clue what it does.

Example:

def show_square(string):
  try:
    n = int(string, 10)
  except ValueError:
    print "I can't do that, Dave."
  else:
    print n * n

Python's ternary operator

In Python, the C ternary operator (C++ example: bool isNegative = i < 0 ? true : false;) is available as syntactic sugar:

>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'

It's not really strange but a feature. The odd thing is the changed order (A if CONDITION else B) in comparison to the (IMO more logical) order in C (CONDITION ? A : B).

In C, a[b][c] is exactly the same thing as c[b[a]].

In PHP, you have to explicitly reference globals and explicitly use this-> for class variables. Makes refactoring fun. You cannot promote a variable/argument to a global or a class member without finding all points of usage.

Perl

my %h1 = map { $_ => 1 } qw/foo bar baz/;    // construct an 'is_member' type lookup table
my %h2 = map { "$_" => 1 } qw/foo bar baz/;

the second line is a syntax error even though to even an experienced perl programmer it looks like it would be identical. The downside to perl always trying to do what you mean, not what you said.

this made me stunning

#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
            _-_-_-_
       _-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
 _-_-_-_-_-_-_-_-_-_-_-_-_-_-_
  _-_-_-_-_-_-_-_-_-_-_-_-_-_
    _-_-_-_-_-_-_-_-_-_-_-_
       _-_-_-_-_-_-_-_
           _-_-_-_
}

Smalltalk:

Have a class method in a class Test, that returns a constant string:

method1
    ^ 'niko'

You should expect that this method constantly returns the string 'niko' whatever happens. But that is not the case.

s := Test method1 

(Set s to 'niko'.)

s at: 4 put: $i.

(Set s to 'niki'.)

s := Test method1

(Set s to 'niki' again.)

So, what happens is that the second line of code permanently changed method1 to return 'niki' rather than 'niko', even though the source code of the method was not updated.

shift;

sometimes you see it in the very first line of a perl method to get read of self pointer

In ColdFusion

Struct (aka Java HashMap) is passed by reference.

You'd have thought other data type behaves like Java...

Array is passed by value, wtf!

List is just a plain old comma-separated string!

In Unity,

GameObject.Find("MyObject")

will return your object normally. However, if you do something like this:

GameObject.Find("MyObject").active = false;
//do other stuff
if (GameObject.Find("MyObject").active)
{
    //do stuff
}

Then you will get a null reference. In Unity iPhone, this code will often work fine in the editor but will cause a SIGBUS when running from the iPhone. The problem is that GameObject.Find() will only locate active objects, so even if you're just checking to see if it's active, you are effectively calling if (null.active) .

To make it work right, you've got to store it prior to making it inactive.

GameObject obj = GameObject.Find("MyObject");
obj.active = false;
//do other stuff
if (obj.active)
{
    //do stuff
}

Arguably that's better practice anyway, but the way Unity treats inactive objects in general is quite weird. It appears to unload a large portion of the inactive object (textures, etc.) but not all of it, so inactive objects can still eat up a lot of memory.

In J, most primitives (a.k.a. functions) are monadic (one argument) or dyadic (two arguments, one to the left, one to the right). But the amend primitive takes 3 (I think it's the only one, besides foreigns). It's understandable that it would take 3, but it just looks... wrong at first.

vector =: i. 10   NB. Vector will be 0 1 2 3 4 5 6 7 8 9
(10) (0) } vector NB. Will yield 10 1 2 3 4 5 6 7 8 9

In J, foreigns (!:) are various functions bunched together. The left argument is a category, where as the right are often (but not always) incremental values for different... stuff. For example:

    2!:55 NB. Close console
    9!:10 NB. Set print precision
    6!:0  NB. Actual time
    6!:2  NB. Execution time
    4!:3  NB. Loaded scripts

Of course, the smart thing is to wrap them, but some you just commit to memory. BTW, all of those are, come to think of it, triadic, with 2 arguments to the right and one to the left. None of the above will work unless you give them a final valid argument.

In awk, arrays start at index 1, which is confusing the least.

In Python, at least for me, this was very wft! the first time I saw it:

>>> "ja " * 5
'ja ja ja ja ja '

You can multiply strings! WTF??

PS: I think this is because x * n means: n times x so, 5 times "ja " is "ja ""ja ""ja ""ja ""ja " and because you can concatenate strings like this:

>>> "ja ""ja ""ja ""ja ""ja "
'ja ja ja ja ja '

That two codes have the same result (and maybe are just the same)

The implict variables\constants and mutable constants in ruby

In C#, the following code generates compiler error "Cannot convert from method group to Delegate". Though the logic behind is reasonable, it still feels strange to me.

control.BeginInvoke(delegate { DoSomething(); });

A Java source file can end with the character \u001a (control-Z).

In Java, there is some inconsistency as to how Strings handle the == operator depending on how it was constructed.

String a = "Hello";
String b = "Hello";
System.out.println(a == b ); // prints true.
String c = new String("Hello");
String d = new String("Hello"); 
System.out.println(c == d ); // prints false

Here is one about python:

>>> print 07
7
>>> print 08
  File "<stdin>", line 1
    print 08
           ^
SyntaxError: invalid token

Ain't that a beauty?

Especially unthoughtful when you think of how human write dates, which has the following effect:

datetime.date(2010,02,07) # ok
datetime.date(2010,02,08) # error!

(the reason is that 0x is interpreted as octal, so print 010 prints 8!)

In Scheme there are no reserved identifiers. So, the following expression evaluates to 1:

((lambda (lambda) lambda) (let ((let 1)) let))

Note that there is a restriction on definitions within a given scope: no definition may redefine an identifier used to define identifiers within that scope, so the following is a syntax error:

(begin (define define 1) define)

Well the first thing that came to my mind was 'noop', my brain did the same thing when I first came across it!

In JavaScript, 2.0 - 1.1 = 0.8999999999999999. This is a result of the implementation of floats in the specification, so it will always be like this.

Here's one I thought was weird:

In C/C++ you can have as many semicolons as you want at least in the MS C++:

int main(void)
{
 cout<<"Hello World";;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;
 return 0;;;;;;;;;;;;;;;;;;;;;;;
}

In C++, I find it strange and distasteful that "virtual" MI (multiple inheritance) allows the "diamond-shape" class hierarchy to "work"

A : Base class, e.g. "Object"
B, C: both derive (virtually or not) from Object and 
D: derives from both B and C

Problem: "normal" inheritance causes D to be 2 ambiguous kinds of A. "virtual" MI collapses B's A and C's A to a single shared base A object.

So, even if your Wheel is an Object and your Left Front Wheel is a Wheel and your Car inherits four kinds of Wheel, your Car is still only one kind of Object with virtual MI. Otherwise, your Car is not an Object, but 4 Wheel-y Objects.

This is one language feature that rewards poor class design, punishes compiler writers, and leaves you wondering at run-time where the heck the Object really is - and if any virtual MI baggage was misplaced.

If you really need the diamond pattern in your class hierarchy, it can be accomplished with regular MI and an "AProxy" that delegates to the single A base.

 A : Base class, e.g. "Object"
AProxy: Base class, constructs with other A to bind to
B : derives from A
C : derives from AProxy
D : derives from both B and C (passing B's A to C's AProxy at construction)

This requires a little more work for those that really like diamond MI and leaves the rest of us in peace with a more tractable set of language features.

For those who never worked with COBOL, this is a common line of code but it doesn't do what you might be thinking about

PIC XXX

С#:

var a = Double.Parse("10.0", CultureInfo.InvariantCulture); // returns 10
var b = Double.Parse("10,0", CultureInfo.InvariantCulture); // returns 100

In invariant culture comma is not decimal point symbol, but group separator.

As I know, it's common mistake for novice programmers from some locales.

PHP

PHP has inconsistent handling of overloading for instance variables and methods. Consider:

class Foo
{
    private $var = 'avalue';

    private function doStuff()
    {
        return "Stuff";
    }

    public function __get($var)
    {
        return $this->$var;
    }

    public function __call($func, array $args = array())
    {
        return call_user_func_array(array($this, $func), $args);
    }
}

$foo = new Foo;
var_dump($foo->var);
var_dump($foo->doStuff());

The dump of $var works. Even though $var is private, __get() is invoked for any member which doesn’t exist or is inaccessable, and it returns the correct value. This is not the case for doStuff(), which fails with:

Fatal error: Call to private method Foo::doStuff() from context ”.”

I think a lot of these work in C-style languages, but I’m not sure.

  1. Pass a here document as a function argument:

    function foo($message)
    {
        echo $message . "\n";
    }
    
    foo(<<<EOF
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc
        blandit sem eleifend libero rhoncus iaculis. Nullam eget nisi at
        purus vestibulum tristique eu sit amet lorem.
    EOF
        );
    
  2. You can assign a variable in an argument list.

    foo($message = "Hello");
    echo $message;
    

    This works because an assignment is an expression which returns the assigned value. It’s the cause of one of the most common C-style bugs, performing an assignment instead of a comparison.

Python

In Python, mutable default function arguments cause unexpected results:

def append(thing, collection=[]):
    collection.append(thing)
    return collection

print append("foo")
# -> ['foo']
print append("bar")
# -> ['foo', 'bar']
print append("baz", [])
# -> ['baz']
print append("quux")
# -> ['foo', 'bar', 'quux']

The empty list is initialized at function definition time, not call time, so any changes to it persist across function invocations.

MySQL Case Sensitivity

MySQL has really unusual case sensitivity rules: Tables are case sensitive, column names – and string values aren't:

mysql> CREATE TEMPORARY TABLE Foo (name varchar(128) NOT NULL);
DESCRIBE foo;
ERROR 1146 (42S02): Table 'foo' doesn't exist
mysql> DESCRIBE Foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(128) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.06 sec)
mysql> INSERT INTO Foo (`name`) VALUES ('bar'), ('baz');
Query OK, 2 row affected (0.05 sec)

mysql> SELECT * FROM Foo WHERE name = 'BAR';
+------+
| name |
+------+
| bar  |
+------+
1 row in set (0.12 sec)

mysql> SELECT * FROM Foo WHERE name = 'bAr';
+------+
| name |
+------+
| bar  |
+------+
1 row in set (0.05 sec)

Anything will autometic pluralizes or singularizes any class and member names.

Linq-to-Sql, for example

Don't know if it is a feature or not. For some, yes, but for others it might be an annoying behavior. Anyway, I think it's worth mentioning.

In Python, the builtin function round() behaves a bit differently between Python 2x and Python 3x.

For Py 2x,

>>> round(0.4)
0.0
>>> round(0.5)
1.0
>>> round(0.51)
1.0
>>> round(1.5)
2.0

For Py 3x,

>>> round(0.4)
0
>>> round(0.5)
0
>>> round(0.51)
1
>>> round(1.5)
2

I'm just not familiar with the way round() in Py 3x works with 0.

Docs for round() in Py 2x and Py 3x.

In Python:

i = 1
++i
print i

prints '1'. The line '++i' evaluates to +(+i) (Python doesn't support increment operators)

Weak typing, in general.

C:

printf("%c\n", 'a' + 3);

PHP:

echo 5 + "3";

And far too many other languages.

COMEFROM is the weirdest, and probably most useless, language feature I ever saw.

Runner-up would be the ternary operator, because it violates rule #1 of optimization. And it does more harm than it solves problems. It does more harm because it makes code less readable.

Not really a language feature, but interesting/awesome use of features is Duff's device.

Modula-2 doesn't have elseif or else if; it has elsif

Java's Integer class's base-conversion static methods. :P Very few languages have this functionality built right in, it seems.

In all languages today:

TypeA a = (TypeA)some_operation_returning_TypeB(1,2,3); // TypeB is not inheriting TypeA

fails on runtime with "Cast to TypeA failed exception"-message (or similar). What this tells us is just how lazy programmers really are. There's no way for them to produce message "Failed to assign variable 'a' of TypeA with a value 'some_operation_returning_TypeB(1,2,3)' of TypeB". Noooo.. their motto is "thy who makes mistakes must suffer".

I find Javascript Date Object's love for the year 110 delightful.. Try it.

<Script language ="JavaScript">
<!--
var now = new Date()
var dia = now.getDay()
var mes = now.getMonth()
var fecha

//Day of the week
if(dia==0){
 fecha="Domingo, ";
}else if(dia==1){
 fecha="Lunes, ";
}else if(dia==2){
 fecha="Martes, ";
}else if(dia==3){
 fecha="Miércoles, ";
}else if(dia==4){
 fecha="Jueves, ";
}else if(dia==5){
 fecha="Viernes, ";
}else{
 fecha="Sábado, ";
}

fecha = fecha + now.getDate() + " de "
//Which month is it?
if(mes==0){
 fecha=fecha + "Enero"
}else if(mes==1){
 fecha=fecha + "Febrero"
}else if(mes==2){
 fecha=fecha + "Marzo"
}else if(mes==3){
 fecha=fecha + "Abril"
}else if(mes==4){
 fecha=fecha + "Mayo"
}else if(mes==5){
 fecha=fecha + "Junio"
}else if(mes==6){
 fecha=fecha + "Julio"
}else if(mes==7){
 fecha=fecha + "Agosto"
}else if(mes==8){
 fecha=fecha + "Septiembre"
}else if(mes==9){
 fecha=fecha + "Octubre"
}else if(mes==10){
 fecha=fecha + "Noviembre"
}else{
 fecha=fecha + "Diciembre"
}

//Year
fecha = fecha + " del " + now.getYear()

document.write(fecha);
//-->
</Script>

Script is in Spanish - sorry if you don't understand the code.. The idea is for you to see the year 110 result.

In Python, every string contains the empty string.

answer = input("Enter 'Y[es] or N[o]:")
if answer in 'YyNn':        # verify input
    process(answer) 

Just hitting return at the above query will set answer to the null string, pass the if answer in ... test, and be processed as a correct answer. To put it more succinctly:

>>> "ABCDEFGHIJ".__contains__("")
True

As usual, Python's behavior here is mathematically and logically impeccable. As I recall from a long ago class in set theory: The empty set is a member of every set.

It's still surprising on the few occasions when I've been bitten by it, but I wouldn't have it any other way.

Perl.

print "Foo\n" unless $foo;

Best of show entry in the Perl Journal's Obfuscated Perl Contest in 2000:

#:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||
open(Q,$0);while(<Q>){if(/^#(.*)$/){for(split('-',$1)){$q=0;for(split){s/\|
/:.:/xg;s/:/../g;$Q=$_?length:$_;$q+=$q?$Q:$Q*20;}print chr($q);}}}print"\n";
#.: ::||-| .||-| :|||-| ::||-| ||-:: :|||-| .:|

Code fully explained by its author at http://mysite.verizon.net/les.peters/id2.html

The fact that there is no encapsulation in C++ (or Java). Any object can violate the encapsulation of any other object, mess around with its private data, as long as it's the same type. For example:

#include <iostream>
using namespace std;

class X
{
  public:
    // Construct by passing internal value
    X (int i) : i (i) {}

    // This breaks encapsulation
    void violate (X & other)
    {
        other.i += i;
    }

    int get () { return i; }

  private:
    int i;
};

int main (int ac, char * av[])
{
    X a(1), b(2), c(3);

    a.violate (c);
    b.violate (c);
    cout << c.get() << endl;    // "6"
}

either in java (this is an if statement that results in assignment)

result = (Boolean condition) ? (if Boolean is true) : (if Boolean is false);

or

data Nat = Z|S Nat deriving Show
nattoInt Z = 0
nattoInt (S a) = 1 + nattoInt a

buildNat 0 = Z
buildNat a  =  S (buildNat (a - 1))

in Haskell... I still don't quite get how this defines the natural numbers (I understand the THEORY perfectly :-p)

I would definitely give Perl the honor of having multiple horrible examples:

if(!$#var)

or

if($mystring =~ m/(\d+)/) {

In c++

const char* wtf()
{
    char buf[100];
    return buf;
}
string s = ... + wtf() + ...;

creates interesting values in s. Partly string, partly stack contents, mixed with zeroes so that s.length()!=strlen(s.c_str()). The strangest thing is that compiler has absolutely no problems with returning pointers to stack - compiler programmers's hand would probably fall off if he would have added a warning there.

here is my 2 cents. In c++:

int* t = new int(15);
delete t;

You can throw anything throwable in Java.

class YourBoss extends Throwable {
}
public class Main{
  public void main(String[] s) throws YourBoss {
   try{
    throw new YourBoss();
   }catch(Exception e){
   }catch(Error e){
   }
  }
}
Related