What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
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.
printf("%c", [!!flag]"FT")
. - David R Tribble
[!!flag]"FT"
is invalid syntax. - Roger Pate
"FT"[!!flag]
-- which actually isn't so bad if you're used to C's treatment of strings as aarrays and booleans as integers. It's not nearly as bad as C++ code that uses badly designed operator overloading. - Nate C-K
do { tmp = val; val /= 10; *p++ = "9876543210123456789"[9+tmp-10*val]; } while(val); ...
- guess what its doing? - Frunsi
number[array]
format in this answer: indexing a string like that is not strange at all. - Roger Pate
"10+a" is also pointer arithmetic, is treated exactly the same way"
. If it were treated literally the same way, we would expect "10+a" to mean 'start at address 10, and move a
shorts, i.e. a*2 bytes', which would give an incorrect result. If the key is that the compiler distinguishes between the address (a
) and the index (10
) based on their datatype, then I think that point is a crucial one that needs to be stated. However this is covered in q 381542. - LarsH
lengthof
macro to quit the need for extra parentheses: #define lengthof(x) (sizeof(x) / sizeof 0[x])
. Very bad, I know. - unkulunkulu
In JavaScript:
'5' + 3 gives '53'
Whereas
'5' - 3 gives 2
+
for string concatenation is horrible - Matteo Riva
+
for concat. It's just a symbol. (It's how it behaves with mixed types I don't like). But I find string concatenation a very different operation from mathematical addition, beginning with the fact it's not commutative. - R. Martinho Fernandes
+
is not used for non-commutative operators (although I'm sure someone will give a counter-example). Even as a monoid(?) I'd prefer not to use *
as it would cause the same confusion. - Tom Hawtin - tackline
x - 0
. - David R Tribble
'a'+2+3+5+7
is 'a2347'
. Now tell me what 'a'+2+3-5+7
will be. Wisest syntax lawyers get baffled. - SF.
alert('a'+2+3-5+7)
. You need to meet some wiser language lawyers. - Roger Pate
+
for concat nor weak typing is the problem. Programmers who are unaware of the language semantics are the problem. I mean: If you think it's a good idea to subtract an int form a string, it's all your own fault. And if you're even not aware what type your variables belong to -- so much the worse. - wnrph
In JavaScript, the following construct
return
{
id : 1234,
title : 'Tony the Pony'
};
returns is a syntax error due to the sneaky implicit semicolon insertion on the newline after undefined
return
. The following works as you would expect though:
return {
id : 1234,
title : 'Tony the Pony'
};
Even worse, this one works as well (in Chrome, at least):
return /*
*/{
id : 1234,
title : 'Tony the Pony'
};
Here's a variant of the same issue that does not yield a syntax error, just silently fails:
return
2 + 2;
a = myFunc[newline](1)
is not the same as a = myFunc;(1)
. In the first case a equals the value returned by myFunc(1)
, but in the second case a equals the function myFunc
. - Tim Goodman
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
==
serve in the eyes of the language designer? - Chris S
==
had the meaning of ===
, and then there was another operator, something like ~=
that allowed type coercion. - TM.
(a != b) != c <=> a != (b != c)
(!=
is exclusive-or for booleans). - Tom Hawtin - tackline
==
without realizing it when they do getAttribute()
and get a string value but compare against an int value. - Nicole
===
operator which does have sane semantics. Other than this and a couple other WTFs, JavaScript is actually a really cool language and definitely worth learning, in my opinion. - Joey Adams
Trigraphs in C and C++.
int main() {
printf("LOL??!");
}
This will print LOL|
, because the trigraph ??!
is converted to |
.
-
and +
to produce very primitive graphical boxes. In Unicode, I believe that the 'broken bar' was introduced elsewhere (0xA6) for compatibility, just so that such graphics can still be drawn. - Euro Micelli
| ¦ |
was for drawing roads in ascii games - Scoregraphic
if(foo ??!??! bar)
instead of if(foo || bar)
. - R. Martinho Fernandes
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
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;
IntegerCache.high
, but only 1 line ahead, they decide it's better to hardcode the 128 (instead of using IntegerCache.high+1). - monokrome
128
is there to rebase the array so the pooled instance of -128 is at index 0, -127 at index 1, 0 at index 128, and so forth. Writing IntegerCache.high+1
would be a bug. - R. Martinho Fernandes
x
and it is not necessary to know what range the value of x
is within to predict the result. - Daniel Earwicker
a = 127; b = 127, a is b
will return True
. :) - mipadi
Quoting Neil Fraser [1] (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;
}
[1] http://neil.fraser.name/news/2009/10/27/Control cannot leave the body of a finally clause
- Richard Ev
return
the stack is "discarded". But you're right, finally always wins. I wonder if you can insert some clever trick here, like fooling the compiler and yet executing other code. - lorenzog
return
in finally
clause. - jfs
return
in a finally
. Well, i'll predicate that with saying that it depends on what the language tells you about what happens when you return
or throw
or any other thing. - SingleNegationElimination
finally
clause has to run, it follows that the return true
statement has not executed when return false
is encountered. The function returns at that point. A compiler might notice that a return
statement cannot raise an exception, so it is safe to rewrite it to put it after the finally
clause. - SingleNegationElimination
finally
what would the following code do: bool x = true; try { return x; } finally { x = false; }
- Chris Lutz
TestClass x = new TestClass(); x.Value = true; try { return x; } finally { x.Value = false; }
where TestClass is a reference class then the Value property will be set to false before control returns to the calling method. So it would appear that the value is copied to the stack when the return is hit, but the finally block is still called (as you'd expect). - Martin Harris
try / finally
block work. - Chris Lutz
finally
does exactly what it says. - David R Tribble
halt;
doesnt stop the madness, maybe exit;
will.. :) - Talvi Watia
APL (other than ALL of it), [1] the ability to write any program in just one line.
e.g. Conway's Game of Life in one line in APL [2]:
alt text http://catpad.net/michael/APLLife.gif [3]
If that line isn't WTF, then nothing is!
And here is a video [4]
[1] https://stackoverflow.com/questions/268751/what-ever-happened-to-aplThe weird things C++ templates can be used for, best demonstrated by "Multi-Dimensional Analog Literals" [1] 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) );
[1] http://www.xs4all.nl/~weegen/eelis/analogliterals.xhtmlassert(blue-screen-of-death)
LOL - Talvi Watia
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 isThere’s a lot more where those came from. Read the complete list here [1].
[1] http://perldoc.perl.org/perlvar.html$[
variable is the most evil of them all. - Brad Gilbert
perldoc perlvar
every five seconds. (Though I confess that half the time I check it thinking "I know there's a special variable that can do this for me, I just don't remember which one..." =P ) - Chris Lutz
use English;
is that it affects RegExp performance. I am not making this up. perldoc.perl.org/English.html#PERFORMANCE - David Webb
use English;
. I hate mixedCaseCode unconditionally as it is, but ALL_CAPS_CODE is a whole new level of loathing. - Chris Lutz
$$
is more evil than Brad's one, I'd never seen that before. - Chris S
/$foo[bar]/
, is the [bar]
part a character class or a subscript to the array @foo
? Grep perldata for the terrifying answer. - j_random_hacker
<? $__='([^/]+)@i\',$__,$___';$_=preg_match('@^(?:,)?([^/]+)@i',$__,$___); echo $_; echo $__; print_r($___); ?>
- Talvi Watia
perldoc -v $.
Also see perldoc perlvar
for complete list (also online: perldoc.perl.org/perlvar.html) - draegtun
$_
as the topic, $!
for errors and $/
for match results). None of them are global anymore. - moritz
PHP's handling of numeric values in strings. See this previous answer to a different question [1] 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.
intval()
is recommended for integer comparisons using ==
when in non-integer variable form. - Talvi Watia
intval(01e4,16);
see: us2.php.net/manual/en/function.intval.php - Talvi Watia
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 [1].
[1] https://stackoverflow.com/questions/850341/workarounds-for-javascript-parseint-octal-bugperl -E'say 010'
- Evan Carroll
parseInt('09')
should have thrown an exception. - Albert
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)
IF(
could mean either the start of one of the varieties of IF, or an assignment to the IF array. - David Thornley
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);
}
}
[1] http://en.wikipedia.org/wiki/Duff%27s_devicewhile
at the end is a (conditional) JMP
back to the do
, which explains why you can skip the do
and still end up in the loop. - wds
fallthrough
or continue
keyword). and in that world most other code using a switch statement would be shorter. however, it is too late to change that for C and C-like languages (everyone except newbies expect the default-fall-through nowadays). - Frunsi
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]);
}
def f(j : => int)
) - Dario
... template<typename T> struct by_name { virtual operator T&() = 0; }; void f(by_name<int> j) { ... } int main() { f(struct : by_name<int> { operator int&() { return a[i]; } }); }
? - Simon Buchan
x = dotproduct(a[i], b[i], i)
. - Kragen Javier Sitaker
become:
, it swaps any two objects in place, for example true become: false
. - starblue
f
would take a parameter Func<int>
, the test call would be f(() => a[i]);
and references to j
inside f
would say j()
. This makes the recalculation more obvious. - Daniel Earwicker
In Python:
>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False
Not a WTF, but a useful feature.
(10 > 5 > 1) != ((10 > 5) > 1)
in Python. - sastanin
(funct_a(5)+5 > b > funct_a(5))
only calls funct_a(5)
once. It's a GREAT feature! - Khelben
//
operator is simply amazing. - Chris Lutz
funct_a
will be called twice in that example. In b > funct_a(5) > c
it will only be called once though, as opposed to b > funct_a(5) and funct_a(5) > c
. - Baffe Boyois
((bool)(1<a)) < 3
- SF.
In Java:
int[] numbers() {
return null;
}
Can be written as:
int numbers() [] {
return null;
}
const T*
and T const*
are equivalent, it's T* const
that consts the pointer. Also, I hate sans fonts. - Simon Buchan
numbers()[2]
is a legal statement. - badp
int[] numbers() [] { return new int[1][1];}
- Bas Leijdekkers
INTERCAL [1] is probably the best compendium of strangest language features. My personal favourite is the COMEFROM [2] statement which is (almost) the opposite of GOTO.
[1] http://en.wikipedia.org/wiki/INTERCAL_programming_languageCOMEFROM 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.
PLEASE
modifier often enough! - vgru
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.
2+2
can equal 5
(for very large values of 2
of course!). - Alok Singhal
2
is defined as a constant, and as such could be sent by reference to a function making the constant 2
be any abritrary value in the actual machine code. - Earlz
2+2
would equal 5
for small values of 5
). - David Thornley
2 + 2 = 5
; that'll be a syntax error. What will be true is 2 + 2 .EQ. 5
. - David Thornley
2 + 2 .EQ. 5
is possible. Just read the other comments. Or if you don't want to, just make 5 be 4. - R. Martinho Fernandes
x
is an integer, shouldn't x+x
at least always be an even number? Or can you even make the integer constant 2
point to 2.5
? - Albert
int foo(a) {return ++a + 2;}
then foo(2)
returns 6
? - Robert
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 :)
cout << "Hello, World!"
, which many people don't realize involves templates. (Using a string instead of a quoted string would have been even more verbose.) - David Thornley
std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::vector< std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator>(std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator, std::vector<std::pair<int, std::complex>, std::allocator<std::pair<int, std::complex> > >::iterator, std::allocator<std::pair<int, std::complex> >)
- rlbond
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.
]p
and ]P
commands, which help paste code into new locations while automatically using the correct indent. - Greg Hewgill
]p
and ]P
is whether the pasted text is inserted below or above the current line. - Greg Hewgill
I struggled a bit about this:
1;
In perl, modules need to return something true.
'Cogito ergo sum';
which as everyone knows is self-evidently true in all possible universes. This ensures maximum portability." - Greg Bacon
<?=1;?>
returns 1. <?=true;?>
returns 1. <?=false;?>
returns null. - Talvi Watia
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 ...
print "Hello World!"
as its hello world program. - R. Martinho Fernandes
Hello World!
. - Tom Hawtin - tackline
'Hello World!'
is how the program would go. Must be much easier to use than this "Java" thing! - Tikhon Jelvis
Hello World!
. Just as bad sometimes for those picking up a second language are those examples that use a generic print 'Hello World!'
and lead you to believe "Oh good, it's exactly the same, this'll be easy." - bob-the-destroyer
h
? It is valid in Jon Skeet's Hello World language. :) - user142019
print "Hello World!"
exactly in PHP. No need for this public static void main(String[] args)
nonsense! - Kevin Ji
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!
While
and Whend
", since there are some people who do pronounce the word "while" with the voiceless labialised velar approximant. And of course it lines up nicer, and code that lines up is nice. - dreamlax
Wend
should have been a replacement for goto
. On Error Wend FixIt
- dreamlax
Do: Loop
breaks the debugger. - Robert
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
[1] for a long time.
Now for the "WTF feature". This is from man bc
(emphasis mine):
[1] http://www.opengroup.org/onlinepubs/000095399/utilities/bc.htmlquit: 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.
JavaScript is object oriented, right? So running methods on literal strings and numbers should work. Like "hello".toUpperCase()
and 3.toString()
. Turns out that second one is a syntax error, why? Because the parser expects a number followed by a dot to be a floating point literal. That's not the WTF, the WTF is that you only have to add another dot to make it work:
3..toString()
The reason is that the literal 3.
is interpreted as 3.0
, and 3.0.toString()
works fine.
3..__add__(4)
). Then again I think (3).__add__(4)
is a much less brain damaged way to do it :) - badp
3.0.toString()
makes my eyes itch. - Ben Blank
3 .toString()
or (3).toString()
, too. - user492203
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? [1]
[1] https://stackoverflow.com/questions/1724255/why-does-2-2-in-javascript===
instead. - Gumbo
Number(n)
to do something similar. Unfortunately in both of our solutions ===
breaks =(. - Xavi
[2]
is casted to a string, and [[["abc"]]]
is casted to a string, too. It still shows poor design, IMO... - strager
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
Option
- RobertPitt
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.
::
symbol. Weird name though. - RCIX
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.
a[i++] = i
is obviously UB, otoh a[(*p)++] = *q
is UB depending on whether p and q point to the same object; you can't tell which until you run the thing, and so the compiler can't actually do anything in the general case. another example: division is UB if the denominator is zero, but what do you expect the compiler to do with x / foo(69)
? - Nietzche-jou
i
gets incremented. It could be (as you hope) after the statement as a whole. Or it could be after evaluating the LHS of the assignment. And it's unclear if RHS or LHS is evaluated first. So there are at least four alternatives. - S.Lott
-Wall
flag, gcc warns about this warning: operation on 'i' may be undefined
- Hasturkun
x/0
, which will seriously slow down division on some computers, whatever the definition. We both know that C has a lot of UB issues, but we seem to miss the root cause. - David Thornley
x/0
. I'm not fine with something that compiles error-free and ambiguously. It's the ambiguity that kills me. - S.Lott
Python 2.x
>>>True = False
>>>True
False
You can really make someone become crazy with this one.
True,False = False,True
. What makes it doubly awful is that 1==1
afterwards still returns True. It wouldn't be such a big deal if True and False were simply consistent (global) labels - kibibu
#define true false
but only if stdbool.h is used. - user142019
if test() == True:
, but just do if test():
, which is suggested in Python (same of course for if not test():
instead of if test() == False:
) - Colin Emonds
Oracle has a couple of SQL WTF issues.
Oracle's treatment of empty strings as null.
Treatment of null values in a "<>" comparison.
create table wtf (key number primary key, animal varchar2(10));
insert into wtf values (1,'dog');
insert into wtf values (2,'');
insert into wtf values (3,'cat');
select * from wtf where animal <> 'cat';
The only row returned is the (1,'dog') row.
''
should no more be treated as NULL than should the integer 0
. - Ben Blank
NULLIF(field, '')
. It assigns NULL
if the value ''
is found. It's like a brother to ISNULL(field, '')
which assigns a value ''
if NULL
is found. - Scoregraphic
SELECT * FROM wtf WHERE ISNULL(animal, '') <> 'cat'
, the same way that is done in any RDBMS. - Esteban Küber
NULL
. In any scenario, NULL
means no value has been supplied, whereas an empty string is a value of zero length. Just like in many imperative languages, an array reference could be null
or a reference to an array of zero elements. This difference can sometimes be important. If a data member has zero length, then its length and content are known. If it is NULL
, its length and content are unknown until it is retrieved/calculated/prompted for/whatever. - P Daddy
Java has a whole freakin book about them.
book http://www.javapuzzlers.com/lg-puzzlers-cropped.jpg [1]
Java Puzzlers [2]
[1] http://www.javapuzzlers.com/lg-puzzlers-cropped.jpgIn 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.
cursor:pointer
(or something like that) - hasen
(function(){})()
, with the parentheses in different places - Eric
<a href="" onClick="return undefined">do nothing</a>
same thing, and why would you need it? - Talvi Watia
tabindex=0
to those elements, otherwise they won't be keyboard-accessible. - Kornel
(function($, undefined){})(jQuery);
- Adam Lassek
var foo;
Variables are assigned the default value of undefined
, regardless of what the variable "undefined" is. The same goes for function arguments, as already noted. - Pauan
(function(){var v; return v})()
. - ELLIOTTCABLE
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
[1]). 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 theDO
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.
IMPLICIT NONE
statement. Also, the implicit typing applies to function names as well; I spent a particularly unpleasant evening trying to figure out why an INTEGRATE
function I had written for a numerical methods course would always return 0. - Pillsy
implicit none
is backported to a number of fortran 77 compilers too. - SingleNegationElimination
DO10I=1.10
and D O 1 0 I = 1 . 1 0
are treated the same! - Kevin Panko
Perl has the
yada yada operator
[1] (...
).
The so called “yada yada” operator of Perl 6 heritage is a shortcut to mark unimplemented code:
if ($condition) { ... }
is the same as
if ($condition) { die "not yet implemented" }
[1] http://blogs.activestate.com/2010/04/whats-new-in-activeperl-rollout-of-features-from-perl-5-12/ack '\.{3}'
- Kent Fredric
My favorite little C++ syntax trick is that you can put URL's (with some restrictions) directly into the code:
int main( int argc, char *argv[] )
{
int i=10;
http://www.stackoverflow.com
return 1;
}
This compiles just fine.
Syntax highlighting kind of spoils the joke, but it's still fun.
http:
is a GOTO label, it defines a position in the current scope you can jump to by calling goto http;
. - user244343
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 [1] that allows consecutive dots.
[1] http://www.w3.org/TR/REC-xml/#NT-Name<:-D>..</:-D>
as tag. Great, I'm going to abuse this immediately! - Esko
C
) to the smiley face it should be valid? <o_o>OMG</o_o>
is directly valid tho' - Esko
<
(which I assume is supposed to be text content) is illegal. How about <_><_·.>:/:>_.·</_·.></_>
- LarsH
Inheriting from random class in Ruby:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(first seen at Hidden features of Ruby [1])
[1] https://stackoverflow.com/questions/63998/hidden-features-of-ruby/474888#474888class C ( random.choice([A, B]) ):
;-) - Jürgen A. Erhard
package C; use base (qw/A B/)[ int(rand(2)) ];
- draegtun
I was taken by surprise that you can change a class's inheritance chain in Perl by modifying its
@ISA
[1] 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'
[1] http://docstore.mik.ua/orelly/perl/prog3/ch12_05.htmisa
/ class_pointer
is changed to the newly created subclass. - dreamlax
for
loop. Of course, most languages don't expose the object-oriented aspects quite as much (see the Common Lisp Meta-Object Protocol). - David Thornley
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"
@""
), you don't need any extra @s. For example, @"foo" "bar"
. This can be really useful for breaking strings across lines... - jtbandes
"foo" bar
where bar is a variable containing a string won't work. - Juliano
CFSTR
macro that builds compile-time static CFStringRef
instances ensures that it is only used with constant strings by being defined like this: #define CFSTR(x) __some_magic_builtin_that_i_cant_remember("" x "")
- dreamlax
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
(function (undefined) { ... })();
. This way you have a guaranteed undefined value in your scope (since omitted parameters are undefined always). And it's compressed better than typeof checks. - Sedat Kapanoglu
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.
0BRANCH
. As for taking over the parser, that's pretty common, as the language is pretty much geared towards building DSLs. - Daniel C. Sobral
I added the "format" function to Lisp in about 1977, before "printf" even existed (I was copying from the same source as Unix did: Multics). It started off innocently enough, but got laden with feature after feature. Things got out of hand when Guy Steele put in iteration and associated features, which were accepted into the Common Lisp X3J13 ANSI standard. The following example can be found at Table 22-8 in section 22.3.3 of Common Lisp the Language, 2nd Edition [1]:
(defun print-xapping (xapping stream depth)
(declare (ignore depth))
(format stream
"~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]"
(xectorp xapping)
(do ((vp (xectorp xapping))
(sp (finite-part-is-xetp xapping))
(d (xapping-domain xapping) (cdr d))
(r (xapping-range xapping) (cdr r))
(z '() (cons (list (if vp (car r) (car d)) (or vp sp) (car r)) z)))
((null d) (reverse z)))
(and (xapping-domain xapping)
(or (xapping-exceptions xapping)
(xapping-infinite xapping)))
(xapping-exceptions xapping)
(and (xapping-exceptions xapping)
(xapping-infinite xapping))
(ecase (xapping-infinite xapping)
((nil) 0)
(:constant 1)
(:universal 2))
(xapping-default xapping)
(xectorp xapping)))
[1] http://www.cs.cmu.edu/afs/cs.cmu.edu/project/ai-repository/ai/html/cltl/clm/node200.htmlMUMPS. 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]]
a
, you could do [a for i in range(7)]
. - asmeurer
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 [1].
[1] http://jsperf.com/rounding-numbers-down/4parseInt(x, 10)
is about 3 times slower than Math.floor()
. - Harmen
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.
foo
is an array containing Foo
objects... Why should it complain when you add a foo object? It seems like this is rational behavior to me. - TM.
new Baz()
to foo
; rather, it shouldn't have let me coerce the array into type Foo[]
in the first place. An array of Foo
makes two guarantees: 1) that any element retrieved from it will have type Foo
, and 2) that any object of type Foo
may be assigned to the array. This obviously isn't the case if the underlying type of the array is actually Bar
. - Will Vousden
Object
: void sort(Object[] a, Comparator cmp) { ... }
. Covariance of arrays was needed so that arrays of arbitrary reference types could be passed to this sort
method." - cdmckay
Bar[]
is not a subtype of Foo[]
, so what do inheritance and polymorphism have to do with it? The reason that this kind of type variance shouldn't be allowed is that an array allows both read and write operations on its underlying type. A read-only type may be type covariant, while a write-only type may be type contravariant, but a read/write type may not be type variant at all. - Will Vousden
Array
type). - Will Vousden
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:
Java; making all object instances be mutexes.
synchronized
statement would be a lot harder to use if that wasn't the case. - David R Tribble
java.util.concurrent.locks.ReentrantLock
but it could have been a separate class from the beginning. Most objects don't need built-in locks, especially immutable objects. - finnw
In PHP one can do:
System.out.print("hello");
UndefinedConstant
as "UndefinedConstant"
) - user395760
"System" . "Out" . print("hello");
, which concatenates those strings with the output of print (whatever it is), and then does nothing with it. Meanwhile, the "print" function sends whatever you passed it to the output. - Adam Bard
In JavaScript:
alert(111111111111111111111) // alerts 111111111111111110000
This was quite damaging to some 64bit keys I passed back and forth in JSON.
Nubmber
s which is an IEEE float. - LiraNuna
else
in Python's for
loops.
From the Python documentation:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
Output:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
else
is only executed if the for
loop does not exit because of a break
statement. - Adam Paynter
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)
2 = 2.5;
then if(2 + 2 == 5)
- GameFreak
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
return l.append(1)
. You have to l.append(1); return l
because list.append
returns nothing. - Chris Lutz
a
. Every element of a tuple has to be hashable in order for you to hash it. I mention this because hashability is the main reason for immutable types like tuples. - asmeurer
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"();
function elem($array, $key) { return $array[$key]; }
to get round this and then say elem(someFn(), $index);
without PHP complaining. I've got a whole library of this stuff: github.com/olliesaunders/fluidics - Ollie Saunders
"
for function calls in PHP. - Talvi Watia
#
instead of //
for comments. - Talvi Watia
In
Scala
[1], 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)
.
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
null
, then it means that the value can be either null
or an instance of the specified class. - Ignas R
NULL
in an object means empty, and unknown if undefined. A child element normally should not exist. In the example at http://codepad.org/FphVRZ3S
, there is a child element being called from $obj
... return $obj->eat();
... below when you call foo(null);
, you should actually instead call foo();
, rather foo($var_obj);
by creating it first. Inserting NULL
as the function parameter just won't work because it doesn't exist (yet). see: http://php.net/manual/en/language.types.object.php
If the value was NULL
, the new instance will be empty. - Talvi Watia
null
is set as the default value may be an acceptable trade off. - Andrea Zilio
function foo(Eatable $obj=null)
as your function declaration. - Aether
In JavaScript the result of a method can depend upon the style braces are placed. This is the K&R style [1], 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 [2], 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.
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.
if
-else
, they've proposed adding a then
statement that would preserve the value of $TEST and restore it at the beginning of the next line, thus protecting you from the hazard of having it change on you; but I don't even know if the proposed future standard will ever happen. - Nate C-K
%
. - Nate C-K
In Ruby, 0 evaluates as true in conditional expressions.
0 == true
is a good thing. I'm not trolling - actually interested... Does any integer evaluate as false? - nickf
false
and nil
are false. I suppose it avoids using a magic number for false, if for instance 0 is a valid result for a function it could still return nil
on a error and be used in an if statement. - Scott Wales
nil
) is "false" and everything else is true. - lfborjas
nil
/false
if not found so you can simply write puts "Found" if "abba".find "ab"
instead puts "Found" unless "abba".find("ab") == nil
- Hauleth
The absolute worst WTF has got to be Cobol's ALTERED GOTO.
The syntax is pretty straight forward: "ALTER label1 TO GOTO label2", but the results of debugging run-time spaghetti are mind-boggling.
Label:
<long initialization>
ALTER Label TO GOTO InitializedLabel
InitializedLabel:
<Other stuff>
. - configurator
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.
"Piet is an esoteric programming language designed by David Morgan-Mar, whose programs are bitmaps that look like abstract art."
Piet program that prints Piet
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)...
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.
x --> 0
is parsed as x-- > 0
. - mipadi
-->=
,--<=
,--<
,++>
,++<
,++>=
,++<=
- gion_13
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.
become:
for Python: wargle.blogspot.com/2009/07/smalltalks-become-in-python.html - outis
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.
FORMAT
, which has two options for printing numbers as Roman numerals: one prints 4 as IV
; the other prints 4 as IIII
. - Pillsy
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.)
int
, you should use %zu
to print out size_t
types. - Chris Lutz
s1
and s2
are small enough to fit in int
. I did the cast because %zu
is C99 only. - Alok Singhal
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
[1]):
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");
[1] http://javascript.crockford.com/remedial.htmlfunc_get_args()
function, except you'd actually have to type out $arguments = func_get_args();
. And if you really don't like cluttering up function declarations with parameters and leaving everyone who reuses your code hating you, you can do this: function do_something() {list($var1, $var2, $var3) = func_get_args();}
- bob-the-destroyer
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 [1] for more details.
[1] https://stackoverflow.com/questions/1758321/casting-ints-to-enums-in-cJava 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
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".
x = x + 1
in a functional language if you wish. codepad.org/7n69C5KC - Josh Lee
$x++;
or $x.=1;
in PHP. (not $x.='1'
BTW, completely different statement!) - Talvi Watia
LET X=X+1
- Gerry Coll
<-
for assignment and =
for constant declarations and equality, which I think is much more clear. Pascal's :=
irked me at first too, until I tried teaching C# to beginners and discovered that the =
not being equality was extremely frustrating to them. - Rei Miyasaka
Perl:
It's possible to write a program consisting entirely of punctuation [1].
How does this even work?!
[1] http://99-bottles-of-beer.net/language-perl-737.htmlI'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.
break
there out of habit I get the bug I could never find looking at the code. - vava
continue
has useful behaviour inside a switch
(as does break
), so overloading either of those words to mean either "fall through" or "leave the switch
" is, in my opinion, a bad idea. - C. K. Young
switch
statement, with a few improvements (fall-through disallowed) but still using that horrible syntax. The one thing I prefer about VB.NET over C# is its superior Select Case
statement where each case block is a true block of code, not just the span of lines between a label and a break
statement. - Nate C-K
{statement;statement;}
was never enough and break
was needed.. - Talvi Watia
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. [1]
[1] https://stackoverflow.com/questions/1712172/whats-your-take-on-the-programming-language-go/1716703#1716703Here's a good bunch of strange C features: http://www.steike.com/code/useless/evil-c/
In JavaScript, seeing !!a
for the first time (as a way to convert to boolean).
~~a
for int coercion then. - Karl Guertin
!!
is not that useful in Perl. - Andrey Shchekin
I like sneaking-in octal values in C:
int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };
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 Perl you can do:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
Is this possible in other languages?
Goodbye World
I assume? - Tyler
test[0,5] = 'Goodbye'
. You could roll your own in C++ (and presumably many other languages supporting OOP). - outis
Mid(test, 0, 5) = "Goodbye"
- SLaks
test =~ s/^...../Goodbye/;
I know the comparison is kinda moot, but in essence, this would yield the same result, although using a completely different approach and technique. I use Regexp before any other replacement methods. - polemon
This is one of my favorites, you can do a println in Java without main().
This will compile and run, giving the println, but also an exception (java.lang.NoSuchMethodError: main)
class Test {
static {
System.out.println("I'm printing in Java without main()");
}
}
main(String[] args)
- that's why it's definitely clear why it fails with NoSuchMethodError
. This is very similar to Turbo C++ programming techniques that allowed to specify some before-main priority using some special #pragma
directives, if I'm not wrong. - Lyubomyr Shaydariv
This may have been already mentioned, but --
PHP's handling of octal values:
$a = 07; // 7 (as it should be)
$b = 08; // 0 (would be an error in any sensible language)
$c = 018; // 1 (again, should have been an error)
$d = 0A; // error (as it should be)
See here: http://bugs.php.net/bug.php?id=29676
Also note the comments on the bug - Derick calls it a feature (as shown by quoting "fix"), not a bug and he claims it would "slow down PHP dramatically in all cases where numbers are used inside scripts" - but then, why does PHP raise an error for 0A?
I think one could make a whole book about the weirdness of PHP...
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()
[1] not let me Add elements of a subtype of T?
List<T>.Add()
[2] 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
[1] http://msdn.microsoft.com/en-us/library/z883w3dc.aspxAddRange<S>()
. Only with the introduction of type inference (alongside .NET 3.5) were you able to omit the type parameter and so the syntactic baggage was no longer a consideration. But by then it was too late. A neat trick nonetheless. - Allon Guralnek
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.
:this()
). - Gabe
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.
ccw
to be defined based on what is in actual data), but only if all such assignments are the same. Differences should be flagged as conflicts, unless there's some awesome way for the compiler to translate between modules. It does seem pretty out there at first glance, I'll agree, but it's a neat idea if you're dealing with input data whose sense you have no control over. - Mike D.
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
->
for qualifying objects instead of the prettier .
. - cdmckay
+
operator for both string concatenation as well as addition. In PHP, if you see +
you know you're talking about adding numbers. ...(unless you've got arrays...) - nickf
'a'.'5'.'...'
looks pretty in my editor because of the markup colors... ->
gets so annoying because it appears similar to HTML tags. Which is why ::
seems to work better for that. - Talvi Watia
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 [1]) 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. "
[1] http://cgibin.erols.com/ziring/cgi-bin/cep/cep.pl?_key=SNOBOL$animal[$dog][$bark]=$sound;
or class $animal->dog->bark=$sound;
- Talvi Watia
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
}
define("TRUE",false,false);define("true",true,false);
would be echo(TRUE==false);//echos true
and echo(true==false);//echos false
... TRUE
would then mean not really true, unless its lowercase. - Talvi Watia
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.
hoisting
of the variable & function declarations - gion_13
In Haskell:
let 2 + 2 = 5 in 2 + 2
yields 5.
let 2 + 2 = 5 in 2 + 3
would yield a pattern matching failure. - fuz
let (+) = \2 2 -> 5 in (+) 2 2
- user954298
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);
if
modifier as it makes the programs more expressive and readable. it lets you place the more important part of the statement (the condition or the action) before the other so it is prominent. Like any feature, it is useful if used judiciously and not abused. - Prakash K
last unless defined $row
- Evan Carroll
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.
#define while if
(who needs loops?) #define void int
("Why is the compiler complaining about no explicit return from my void
functions?") #define main(argv, argc) (main)(argc, argv)
(switch argv and argc for no apparent reason) - Chris Lutz
#define private public
. - luiscubal
#define void int
is quite odd. - wheaties
void
at first; void
only really appeared with standard C. That meant there were a heck of a lot of people with old compilers who wanted to run modern C code, and #define void int
worked well enough to run some C90 code in K&R compilers. - David Thornley
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
'".
IO::Handle
were created to make them more sensible -- except these objects don't work everywhere a real filehandle would. It's a good thing programmers don't need to work with files much. - j_random_hacker
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...
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";
"\r\n"
) newlines. - David R Tribble
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!
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
[1]'s blog:
C/C++:
The Fast Inverse Square Root [1] algorithm takes advantage of the IEEE floating-point representation (code copied from Wikipedia):
float InvSqrt(float x)
{
union {
float f;
int i;
} tmp;
tmp.f = x;
tmp.i = 0x5f3759df - (tmp.i >> 1);
float y = tmp.f;
return y * (1.5f - 0.5f * x * y * y);
}
[1] http://en.wikipedia.org/wiki/Fast_inverse_square_rootIn 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.
"
=UTF-8 vs '
=ASCII. $i
will render as an integer/byte - Talvi Watia
In PowerShell, you can rename variables:
> $a = "some value"
> $b = "a"
> $c = "d"
> Rename-Item variable:$b $c
> $d
some value
Indirect indirection! Take that, PHP [1]!
Literals work, too:
> Rename-Item variable:d e
> $e
some value
[1] http://php.net/manual/en/language.variables.variable.phpThe 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
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
Ruby
Time.parse
often pretends that the parsing did not fail, returns now
instead
require 'time'
Time.parse '2000-01-01 12:00:00'
# -> 2000-01-01 12:00:00 +0100
Time.parse '2000-99-01 00:00:00'
# -> ArgumentError: argument out of range ...
Time.parse 'now'
# -> 2010-08-13 21:26:13 +0200
Time.parse 'yesterday'
# -> 2010-08-13 21:26:18 +0200
Time.parse 'billion years ago'
# -> 2010-08-13 21:26:37 +0200
Early FORTRAN where whitespace was not significant. (The anti-Python!)
DO 20 I = 1, 10
Meaning: loop from here to line 20 varying I from 1 to 10.
DO 20 I = 1. 10
Meaning: Assign 1.10 to the variable named DO20I.
Rumors are that this bug crashed a space probe.
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.
foo()
protected in the base class (and rename it foo_core()
or something). Then define a public non-virtual foo()
in the base class that calls the protected virtual one. - munificent
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.
please
as a shell alias for sudo
. - dan04
READ OUT
and WRITE IN
instructions. - polemon
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.
define
isn't a keyword in PHP, it's a function. Sure, it's an incomprisable primitive too but it's still a function. - Ollie Saunders
define
. I just keep a globals.php
file. Keep it $variables
! - Talvi Watia
isLeapYear()
function. Rather than being an error, it made the function always return true. - dan04
Found while learning PowerShell:
Try to guess what the resulted array look like:
$a = 1, 2
$b = 1, 2+3
$c = 1, 2*3
Answers:
1, 2
1, 2, 3
1, 2, 1, 2, 1, 2
Ouch! It shakes my faith in PowerShell and people behind it.
$b = 1, 2+3
is the same as $b = (1, 2)+3
. You were possibly expecting: $b = 1, (2+3)
- Eclipse
In JavaScript this:
var something = 12;
function nicelyCraftedFunction()
{
something = 13;
// ... some other code
// ... and in Galaxy far, far away this:
if( false ) // so the block never executes:
{
var something;
}
}
nicelyCraftedFunction(); // call of the function
Normally you would expect that something
variable will get value of 13.
But not in JavaScript - variables there have function scope so later declaration affects everything up-stream.
In languages that use C/C++/Java notation (like JS) you would expect variables having block scope, not like this ...
So dead block of code that compiler can even remove from final generated bytecode still have side effects in the rest of code that executes normally.
Therefore something
will be still 12
- not change after invocation of the function.
something
actually gets, leaving the reader to wonder what is the strange result of this language feature. - John K
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
==
in SQL. - Talvi Watia
NULL
make sense if NULL
is interpreted as unknown. But SQL isn't consistent about that. (If it were, SUM
would return NULL
if any summed value is NULL
.) - dan04
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 [1]. 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 [2] can be found.
[1] http://inform-fiction.org/I7/Welcome.htmlC++1x Lambda's:
[] (int x) { std::cout << x << std::endl; } ();
These can be abused for some odd syntax:
[](){}();[]{[]{}();}();
This is completely valid C++1x.
:(){ :|:& };:
- new123456
By far the strangest feature I've ever encountered was a "RETURN n" statement in a dialect of BASIC (don't remember which one, this was about 28 years ago). "n" was optional and defaulted to 1. It could be a positive or negative number that indicated which line relative to the invoking GOSUB is the next to get executed.
For example the following would output "30":
10 GOSUB 200
20 PRINT "20"
30 PRINT "30"
100 END
200 RETURN +2
I encountered this when I had to translate a program written in this bizarre BASIC to FORTRAN. The BASIC program used this feature quite a bit to return to different statements based on various conditions and it took me a while to understand the logic flow. Once I understood it, I was able to write a much simpler version of the program. Needless to say, the simpler FORTRAN version had fewer bugs than the original BASIC program.
In PHP:
for ($s="a";$s<="z";$s++) echo $s.' ';
This will write:
a b c d e .. .w x y z aa ab ac ad .. ay az ba bb bc ... by bz ca cb ... yz za zb ... zx zy zz
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?
<?=!(0);?>
returns 1 - Talvi Watia
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.
/*
inside a comment as a warning. It was a good warning to pay attention to. - David Thornley
// comment... */
instead of */
being on its own line, as the close-comment can skip if its commented. another nice-to-have. - Talvi Watia
This is a lack of a feature which is weird: Python
has no
switch
statement
[1] (although workarounds exist).
switch
(perldoc.perl.org/Switch.html). However this was a "source filter" and with 5.10 the new given/when (borrowed from Perl6) were added (perldoc.perl.org/5.10.0/perlsyn.html#Switch-statements) - draegtun
In javaScript, NaN is a global variable.
The most weird feature I know of is from C++ world : SFINAE [1].
The worst is that it happens to actually be very usefull, extensive use of SFINAE in BOOST is proof enough for me.
[1] https://stackoverflow.com/questions/982808/c-sfinae-examplesJava 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.
IList<T>
won't be one of the changes, because of the problem migsho showed. But IEnumerable<object> foo = new List<string>()
will be legal, yes. - R. Martinho Fernandes
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.
writeonly
for the process which declared it so - just an annotation for the compiler to check it. It could be read-only or read/write for others. And, of course, it was a global variable, shared between compilation units. - Mawg
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
}
PHP
From the online doc:
string implode ( string $glue , array $pieces )
— Join array elements with a string
Note: implode() can, for historical reasons, accept its parameters in either order.
So this works: implode($someArray, $glue)
Hope they kill these historical quirks in PHP 6.
mysql_db_query
depreciated to mysql_query
but flipped the arguments... - Talvi Watia
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".
${array()}
actually becomes $Array
(because converting an array to string always returns Array
). So you cannot use actual arrays here. - grawity_u1686
.js
, ${===}='==';
, then ==
would finally be correct. - Talvi Watia
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.
if (x!=x) ...
my first thought is "WTF", so I'd say it qualifies for this topic :) By the way, the Eclipse compiler agrees this is a WTF - it generates a warning for it. - Oak
javascript:
parseInt('06'); // 6
parseInt('08'); // 0
python -c 'a = "05"; b = int(a); print b';
ruby -e'a='05'; puts a.to_i'
.. All return 5. - Evan Carroll
The entirety of the Malbolge programming language: http://en.wikipedia.org/wiki/Malbolge
Commodore BASIC's command shortcuts. Basically most commands had an abbreviated form which was usually the first letter + (shift+2nd letter). But because the character set on a C64 was by default in all uppercase, these commands would look like bizarre symbols. Here's a short hello world example:
Maybe someone has a better example actually with more meat to it, but for long programs this looked completely ridiculous.
Here is a list of abbreviations: http://www.c64-wiki.com/index.php/BASIC_keyword_abbreviation
FORI=1TO15
is valid. Shouldn't take too much imagination to come up with more interesting examples. The one everyone experienced though is that if you put the cursor on the READY.
line and hit ENTER, the BASIC parser interprets it the same as READ Y
. - tenfour
LIST
command) was surprised for the first time. - Lyubomyr Shaydariv
Looking for a function? Why not a language?
I love PHP but it always seems to be built like this "Oh s***t! I forgot this! Let's just add another argument to the function" which result in this :
str_replace($search, $replace, $subject, ...)
strstr($subject, $search, ...)
Notice the extra underscore and the different order for the arguments.
Here is something else
$a = array( 'a', 'b', 'c', 'd');
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
unset($a[2]); //Destroys the element 2 of the list
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 3 => 'd');
strstr
is like that to copy its behaviour in C. And php's arrays are stored associative. If you don't want indexes to break, don't use unset
but actual array functions like array_splice
. - poke
In Perl (without "use strict" or "use warnings"):
if(true==undef)
{
print "True\n";
}
else{
print "False\n";
}
if(undef)
{
print "True\n";
}
else{
print "False\n";
}
if(true)
{
print "True\n";
}
else{
print "False\n";
}
Prints:
True
False
True
In JavaScript:
1 / 0; // Infinity
1 / -0; // -Infinity
C#'s default inheritance model wins my vote:
public class Animal
{
public string Speak() { return "unknown sound" ; }
}
public class Dog : Animal
{
public string Speak() { return "Woof!" ; }
}
class Program
{
static void Main( string[] args )
{
Dog aDog = new Dog() ;
Animal anAnimal = (Animal) aDog ;
Console.WriteLine( "Dog sez '{0}'" , aDog.Speak() ) ;
Console.WriteLine( "Animal sez '{0}'" , anAnimal.Speak() ) ;
return ;
}
}
Running the program give the following as a result:
Dog says 'Woof!' Animal says 'unknown sound'
Getting that sort of behavior should require the programmer to go out of the programmer's way. The subclass instance doesn't stop being what it is because it's been upcast to its supertype. Instead you have to explicitly request the expected (and almost always desired) result:
public class Animal
{
public virtual string Speak() { return "unknown sound" ; }
}
public class Dog : Animal
{
public override string Speak() { return "Woof!" ; }
}
C++'s most vexing parse:
struct S
{
S() {} //default constructor
};
int main() {
S s(); // this is not a default construction, it declares a function named s that takes no arguments and returns S.
}
s
->Go Right-> is a function
->Go Left-> returning S
->Go Right-> ';'. - Zabba
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.";
a["if"]
works, but a.if
doesn't. This is fixed in ECMAScript 5. Also, you can do things like this: a["foo.bar.qux"]
which is not the same as a.foo.bar.qux
. In fact, object keys can be anything that's a valid string: a["_%foo@@*(bar)"]
- Pauan
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'
}
var
statement per function, usually at the top of the function. Only functions create a new "scope" in Javascript, and not things like for loops and if statements or {}
's. - strager
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!
TINYINT(1)
, BIT
or (perhaps best of all) BOOLEAN
rather than ENUM('0','1')
? - outis
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 c#
Math.Round(2.5)==2
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
).
Atari BASIC:
You can fill a string with a character without writing a loop:
10 DIM A$(100)
20 A$(1)=" ":A$(100)=" ":A$(2)=A$
NSIS
[1] (the Nullsoft Scriptable Install System) has the
StrCmp
[2] instruction:
StrCmp str1 str2 jump_if_equal [jump_if_not_equal]
Compares (case insensitively) str1 to str2. If str1 and str2 are equal, Gotos jump_if_equal, otherwise Gotos jump_if_not_equal.
StrCmp $0 "a string" 0 +3 DetailPrint '$$0 == "a string"' Goto +2 DetailPrint '$$0 != "a string"'
The icing on the cake: jump_if_equal
and jump_if_not_equal
can be negative, too. But I guess you already figured that out from the +
symbol in front of positive numbers. I don't remember whether it's mandatory, or just a horrible convention.
This basically combines the worst of BASIC and the worst of Assembler.
[1] http://nsis.sourceforge.net/Main_PageIn Java,
String s = null;
System.out.println(s + "hello");
This outputs "nullhello".
+
operations are automatically compiled to StringBuilder
flows so that a+b+c
(where all variables are Strings
) becomes new StringBuilder().append(a).append(b).append(c).toString()
and as we can see from StringBuilder.append(Object o)
's javadoc, nulls are handled by printing null
instead of throwing an exception. Tl;dr: Compiler magic with syntactic sugar. - Esko
+
is really a whole issue on its own. - Esko
Python 2.x demonstrates a poor list comprehension realization:
z = 4
s = [z*z for z in range(255)]
print z
This code returns 254. The list comprehension's variable collides with an upper defined.
Python 3.x had disposed of this feature, but closures are still using dynamic linking for external variables and brings many WTFs in the functional style python programmer
def mapper(x):
return x*x
continuations = [lambda: mapper(x) for x in range(5)]
print( [c() for c in continuations])
This code returns obviously [16,16,16,16,16]
.
The following is similar to this answer [1] which is about arrays.
In Powershell, like other dynamic languages, strings and numbers are somewhat interchangeable. However, Powershell can't make up its mind.
PS> $a = "4" # string
PS> $a * 3 # Python can do this, too
444
PS> 3 * $a # Python doesn't do it this way, string repetition is commutative
12
PS> $a + 3 # Python gives a mismatched types error
43
PS> 3 + $a # Python would give an error here, too
7
If the variable is an integer instead of a string, then the operations are commutative.
PS> $a = 4 # integer
PS> $a * 3
12
PS> 3 * $a
12
PS> $a + 3
7
PS> 3 + $a
7
When in doubt, do a cast:
PS> $a = "4"
PS> $b = 3
PS> [int] $a * [int] $b
12
You could also use [float]
.
*
and +
operators: the left operand defines the type of the operation. It happens to differ from Python. Note that there is no "official" requirement for string repetition being commutative, unlike there is for numeric addition. Then, numeric operations work as expected. If you cast everything to a single type, everything works as expected too. So, what's strange? - R. Martinho Fernandes
$a = "4"; 0 + $a * 3
So the *
has higher precedence than the +
and since $a
is a string, we get 0 + 444
which results in 444
. I call that a "gotcha" (aka "strange"). - Dennis Williamson
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:
[1] http://msdn.microsoft.com/en-us/library/cb8x3kfz.aspx
AndAlso
(MSDN) [1]
OrElse
(MSDN) [2]
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
{local $[=1; ...}
- slebetman
$]
is always implicitly lexically scoped. - tchrist
$[
, and local doesn't lexically scope anything.. It simply stores the global value in a private register, and restores it at the end of the lexical scope. However, the variable is still global in nature. - Evan Carroll
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 bless
ed 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!
void*
and then cast it to a pointer to a different class. - R. Martinho Fernandes
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
$array = array('id','field','value');
and... $x == 'value'; // true
. Great for queries! - Talvi Watia
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!
x = 4
actually is a reference to an already existing number, whereas y is simply to big to be stored by default, so both constants create new objects. - poke
is
and you are fine. - poke
In php:
easter_date — Get Unix timestamp for midnight on Easter of a given year
int easter_date ([ int $year ] )
In Java (Actually, I have wrote this on different SO post recently) :
int x = 1 + + + + + + + + + + + + + 1;
System.out.println(x);
SQLite lets you declare columns with whatever data type you want. It looks for a few particular substrings ("INT", "REAL", "TEXT", etc.) to determine the affinity.
This makes it possible to lie in your type declarations:
CREATE TABLE Quirks (
X FLOATING POINT, -- = INTEGER affinity because of the "INT"
Y STRING, -- = NUMERIC affinity
);
This old PHP favorite isn't all that WTFish on its own, but a scope resolution error is one of those things that gets seen by so many developers that it's worth giving some WTF love:
$class = new StdClass();
$class::test();
PHP Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM on line 3
Fortran's special meaning of different columns. (Probably completely natural if you grew up with punchcards.)
One side effect of this is that e.g. variable names are truncated after column 72. Combined with IMPLICIT NONE
this then silently introduces a new variable when such a variable name is started close to column 72.
You'll need
to be aware of this
an editor which highlights the comment part (after column 72) in a different color than the part before...
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
++
and --
are notable counterexamples. - Peter Olson
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'; }
[1] http://php.net/manual/en/language.variables.variable.phpuse strict
(or specifically use strict 'refs'
). But I prefer symbol table games: $a = 1; *b = \$a; $b = 2;
Now guess what $a is? yep! (serves you right for using package globals!) - user240438
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.
operator+(const char *, std::string)
function, which will allow "foo" + my_str
. In an operator overload, you need at least one user-defined type (class, enum, etc.), but it needn't be the first one. Operator overloading is usually not a good idea, but it can be very useful on occasion. - David Thornley
char* dir=...; std::ifstream in((std::string(dir) + "/" + file).c_str());
steals it though - Stefan Dragnev
In ColdFusion arrays start at 1.
$[
-- default 0, but it's variable. - ephemient
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.
i
was already taken, so they used j
instead. I couldn't find the comp.lang.python postings, but see, for example, this python-tutor thread. - beerbajay
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).
var_export('false' == 0); // true
var_export('false' == true); // true
var_export('false' == false); // false
EDIT
As @Kobi mentioned, it could happen because language interpret any value as "TRUE" except "FALSE", but not in case of PHP, where things are even more strange than you thought!
This case is fully documented in chapter "String conversion to numbers" of a PHP manual, which says:
If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero).
Here is example:
print (int) 'zero'; // 0
print (int) 'false'; // 0
// but
print (int) '1 - one'; // 1
P.S. I see more harm than usefulness of such implicit type conversions.
false
(some languages behave that way) - 0
is true, and 'any string'
is true. - Kobi
1 == 2
would also be true. The test isn't ('false' && 0)
, it's ('false' == 0)
. PHP is just crazy. - Nicholas Knight
:P
, but the direction is right - show some more asserts and see if it makes sense, draw a whole picture. - Kobi
In Ruby, you can do some weird things with heredocs. Consider:
a = <<ONE
This is one. #{<<TWO}
This is two. #{<<THREE}
This is three.
THREE
TWO
ONE
p a # => "This is one. This is two. This is three.\n\n\n"
Ruby Flip-Flops. "..." and ".." in conditional statements are not always range operators:
(0..20).each do |x|
if ((x%10) == 5)..((x%10) == 5)
print "#{x} "
end
end
(0..20).each do |x|
if ((x%10) == 5)...((x%10) == 5)
print "#{x} "
end
end
This will output:
5 15
5 6 7 8 9 10 11 12 13 14 15
.. checks both statements on each pass, ... only checks the "on" or "off" statement in each pass (depending on the flip-flop state). They are stolen from awk and sed.
Matz writes in "The Ruby Programming Language": "Flip-flops are a fairly obscure feature of Ruby and probably best avoided..."
How about the neat system-dependent overflows causing year rollovers in (MRI/C) Ruby and MacRuby (but not in JRuby) followed by localtime errors for a larger number. Not a common issue, but it is strange:
$ ruby -version
ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
$ irb
>> Time.at(67767976233550799)
=> Tue Dec 31 23:59:59 -0500 2147483647
>> Time.at(67767976233550800)
=> Wed Jan 01 00:00:00 -0500 -2147483648
>> Time.at(67768036191694799)
=> Wed Dec 31 23:59:59 -0500 -2147481749
>> Time.at(67768036191694800)
ArgumentError: localtime error
...
Maybe IRB bug!!
This may be specific to 64-bit environments, though.
Since I haven't seen anyone mention it... RPG 2 or 3 (Report Program Generator... aka Rocket Propelled Garbage) is by far the screwyest language I've ever used. It combines almost no control over program flow (Enter at the top of the file, Exit at the bottom) and programming statements are defined based on characters defined in specific columns using a fixed font (think PUNCH CARDS!!).
To be really FUBAR you have to attempt to program in DYL-280. It combined RPG flow and logic with COBOL syntax.
Look here for RPG: wikipedia.org /wiki/IBM_RPG
An example of DYL-280: http://99-bottles-of-beer.net/language-dyl-280-224.html
For those who didn't know, PostScript is actually a programming language. I've gotten a bit insane with it -- I wrote a PostScript program that computes a Mandelbrot fractal to a very high level of detail. It's really printable PostScript, though it will crash a lot of print drivers...
Anyway, where to start with PostScript... Here's one: You can actually create a variable whose identifier is.... nothing.
() cvn 5 def % Assign the number 5 to... nothing
PostScript is a stack-based language. () puts an empty string on the stack. cvn converts it to a name ("/" if you print it, because all names in PS are preceded by a slash). Then 5 def assigns the value 5 to it. (% is the comment character)
You can't directly get it back, e.g. if I say "/ print", this will not print the number 5. But you can get it back indirectly:
() cvn load print % this will print the number 5
What else... PostScript has dictionaries as a native type, and you can use an array reference as a key to the dictionary... but it is the REFERENCE that is the key, not the array. So:
/myDict 100 dict def
[0] dup myDict exch 42 put myDict exch get == % prints 42
myDict [1] 42 put myDict [1] get % throws an undefined error
Edit: Oh yeah, one more fun thing... Try the following at a Ghostscript prompt:
1 array dup dup 0 exch put ==
D'oh!
Here's some messing around in the Perl debugger:
DB<1> sub foo { +(1..20) }
DB<2> @bar = foo(); # list of 1, 2, 3, 4...20
DB<3> x scalar @bar # size of list
0 20
DB<4> x scalar foo();
0 ''
That's right. When you call the method like that, the scalar context from scalar
propagates down into the subroutine call, turning the innocuous-looking ..
into an entirely different operator. (That's the "flip-flop" operator, instead of the range operator).
One of my C++ favorites:
#include <iostream>
using namespace std;
int main()
{
cout << 3 << 5 << endl;
cout << (3 << 5) << endl;
return 0;
}
Of course, this is easily explainable, but it has beginning programming students scratching their heads!
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
IF(12.GT.11)THEN
and I F ( 1 2 . G T . 1 1 ) T H E N
are identical. - D.Shawley
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.
) ) ) ) ) ) )
- Talvi Watia
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.
null
. Except you can't just return null;
since that's not a reference to anything. This meant I had to define a variable, set it to null and return that. Of course, the variable was named $thisIsAStupidFxxxingVariableIHadToDefineBecauseOtherwiseThisFunctionShitsItself
. (yes these were early days in my PHP development career) - nickf
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
var
keyword before the declaration. Python also does this, and there's no way around it AFAIK... really screws you up when you thought you redefined the variable later in a different scope, but didn't, and its still using the same value from a previous loop...confusing as hell! - mpen
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 PHP, the following:
<?php $foo = 'abc'; echo "{$foo";
is a syntax error.
If you actually wanted {
, followed by the contents of $foo
, you'd have to use .
:
<?php $foo = 'abc'; echo '{' . $foo;
Processing (processing.org) is a language based on Java. In simple terms, processing compiler is Java preprocessor that translates Processing-specific syntax into Java.
Due to the language's design, it has a few surprises:
Processing's class are compiled into Java inner class, this causes some annoyance, like private variables that isn't really private
class Foo {
private int var = 0; // compiles fine
}
void setup() {
Foo f = new Foo();
print(f.var); // but does not causes compile error
}
also missing draw() function causes event handlers to not be called:
// void draw() {} // if you forgot to include this
void mousePressed() {
print("this is never called");
}
In C++, the ability to create a protected abstract virtual base pure virtual private destructor.
This is a pure virtual private destructor that is inherited from a protected abstract virtual base.
IOW, a destructor that can only be called by members or friends of the class (private), and which is assigned a 0 (pure virtual) in the base class (abstract base) that declares it, and which will be defined later/overriden in a derived class that shares the multiple-inherited base (virtual base) in a protected way.
Not technically a language WTF, but an architecture one.
http://www.6502.org/tutorials/6502opcodes.html#JMP
6502 assembly, indirect JMP:
Note that there is no carry associated with the indirect jump so:
AN INDIRECT JUMP MUST NEVER USE A
VECTOR BEGINNING ON THE LAST BYTE
OF A PAGE
For example if address $3000 contains $40, $30FF contains $80, and $3100 contains $50, the result of JMP ($30FF) will be a transfer of control to $4080 rather than $5080 as you intended i.e. the 6502 took the low byte of the address from $30FF and the high byte from $3000.
So adding a single byte to your code could make your indirect jump go wildly off target.
Perl can automatically convert base 26 into base 10, if you can live with yourself in the morning...
$ perl -E "say lc (@a='a'..'asdf')"
30530
The rest of these have nothing on the astounding Ruby Flip-Flop Operator:
p = proc {|a,b| a..b ? "yes" : "no" }
p[false,false] => "no"
p[true,false] => "yes"
p[false,false] => "yes" # ???
p[false,true] => "yes"
p[false,false] => "no"
Yes, program state stored in the interpreter's parse tree. Things like this are why it takes forever to make a compliant Ruby implementation. But I forgive you, Ruby <3
s a="a=""a=""""a"""",@a=""""2N"""",a=""""c=""""_(""""22""""?@a),@a"",@a,a=""a"",a(c)=""S+""_c,e=$T(@@a@(c))",@a
this is a nice one-liner in COS (cache objectscript). The funny thing to note here are 5 different modes of code-indirection *G
At Ohio State University they teach programming using a bastard C++ language called Resolve/C++. Resolve/C++ uses a design-by-contract methodology to everything. It requires you to mathematically model out components and methods within comments that get compiled so that it forces you to maintain a requires/ensures relationship between methods and objects.
In retrospect, FORTRAN's computed goto is pretty odd. Wikipedia tells me [1] some BASICs outdo it.
Another famous favourite is Algol 60's call by name [2] parameter passing.
[1] http://en.wikipedia.org/wiki/Goto#Computed_GOTOI'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...
C# yield statement, not weird but pretty useful.
http://msdn.microsoft.com/en-us/library/9k7k7cf0(VS.80).aspx
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.
In SQL Server you may end up with a nasty surprise if you use select *
in your production code. Using select *
is not considered as a good practice anyway but it is good to know of some of the interesting behaviour.
See question “select * from table” vs “select colA,colB,etc from table” interesting behaviour in SqlServer2005 [1] for more details
[1] https://stackoverflow.com/questions/321468/select-from-table-vs-select-cola-colb-etc-from-table-interesting-behaviour$x=mysql_num_rows($result);
over 4 billion queries served! - Talvi Watia
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.
Foo
to refuse arguments of type Bar
. Just
is a function a -> Maybe a
, Nothing
is a Maybe a
, fromJust
is a function Maybe a -> a
. Hope this helps. - R. Martinho Fernandes
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.
Labeled break and continue statements in Java. [1].
They allow you to break out of multiple control-blocks with a single break.
[1] http://java.sun.com/docs/books/tutorial/java/nutsandbolts/branch.htmlC# 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: 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 [1]:
def show_square(string):
try:
n = int(string, 10)
except ValueError:
print "I can't do that, Dave."
else:
print n * n
[1] https://stackoverflow.com/questions/1744070/why-should-exceptions-be-used-conservatively/1744176#1744176print n * n
after the n = int(string, 10)
- RCIX
else
block is only executed if an exception is raised but isn't of type (in this case) ValueError
. - Chinmay Kanchi
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]]
.
a[b]==b[a]
transformation twice. Could be useful in the real world. - MSalters
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()
{
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_
_-_-_-_
}
_-
is expanded to --F
. In ANSI C, it is two separate tokens, so expands to -
followed by -F
. The output is therefore not the value of pi as the author intended. See stackoverflow.com/questions/841646/… for more. - Alok Singhal
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.
((char[]) String.class.getDeclaredField("value").get("niko"))[3] = 'i'
- akuhn
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 [1], 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
[1] http://www.jsoftware.comIn
J
[1], 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.
[1] http://www.jsoftware.comIn 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)
5 5 != 25
. In Python, two adjacent strings are combined to make a string - this is taken directly from C. - Alok Singhal
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(); });
new Action(delegate { })
stuff automatically. - Rei Miyasaka
A Java source file can end with the character \u001a
(control-Z).
\x1A
would mark "end of file"... I can't imagine it being used on a modern system though. Anyway, Python (v3 at least) also treats this as end of file. - grawity_u1686
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
c == d || c.equals(d)
. But you must call equals()
, or intern both strings, to get the right result. This differs from C# behavior, BTW. - David R Tribble
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)
((ƛ (ƛ) (ƛ ƛ)) (ƛ (ƛ) (ƛ ƛ)))
is the simple version that requires an applicative-order compiler, and ((ƛ (ƛ) (ƛ ƛ)) (ƛ (ƛƛ) (ƛ (ƛ) ((ƛƛ ƛƛ) ƛ))))
works on standard compilers IIRC. - configurator
Well the first thing that came to my mind was 'noop', my brain did the same thing when I first came across it!
I don't know if it's still true, but we discovered by accident that VS FORTRAN(66 or 77) will not support recursion. The recursion was accidental and our default F77 supported it beautifully, but when we took the source to an IBM - Whatta Mess.
"dynamic" in C#.
Ruins the day for everyone who has to work together with a RAD or python victim because Intellisense, type safety and determinism die instantly with the first use of the "dynamic" keyword.
In ColdFusion text values are converted to various data types automatically for various purposes. I hit an odd problem where "00A" and "000" were being return as equal. It turned out that ColdFusion was interpreting "00A" as a time, converting to some sort of numeric time format, and converting it to 0. "000" was being converted to 0. So they were both considered equivalent. That is when I learned about the compare function for strings.
I built a language with a BUT clause once, a long time ago.
expr1 || expr2 || expr3
. The execution stops at the first expression that evaluates to true. - David R Tribble
And again Haskell:
In Haskell you can handle an arbitary size file, as if it is a simple String
. The file will be only read, if the string is actually used. Because of the incredible laziness of Haskell, a program like this will run in constant space, regardless of the file's size:
main = interact (>>= \x -> if x == '\n' then "\r\n" else [x])
(This program convert's a file from stdin to stdout and replace LF by CRLF, the interact
function input's the whole stdin to a function and moves the output to stdout.)
This laziness may also cause problems, because if you close a file handle, you cannot be completely shure, whether lazy Haskell has already parsed all the data from it.
I came across this one while trying to figure out a MACRO that made absolutely no sense but worked anyway. This is true for objective-c but might be also true for other flavors of C (or at least the gcc compiler)
NSString *oneString = @"This " @"is " @"just " @"one " @"normal " @" string";
equals
NSString *oneString = @"This is just one normal string";
It's also true for C style strings
char* str = "this " "also " "works";
Go's [1] pseudo-constant Iota:
type ByteSize float64
const (
_ = iota; // ignore first value by assigning to blank identifier
KB ByteSize = 1<<(10*iota); MB; GB; TB; PB; YB;
)
[1] http://golang.org/Tcl's virtualize the sense of time hooks in the interpreter are pretty weird: http://www.tcl.tk/cgi-bin/tct/tip/233.html
Basically it allows you to make the interpreter use some other source of time data, e.g. to run hardware tests in a simulator first and than later just replace the timer functions and run the identical tests against the real thing.
Call/cc. Call/cc passes a function representing the rest of the program to its body.
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.
float
, you get 0.9 while if you use double
you get 0.8999999999999999. Since double
is Java's default floating point type and float is rarely used in practice, it's pretty safe to say that this does occur in Java too. - Esko
0.9
or 9/10
to this query). - configurator
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;;;;;;;;;;;;;;;;;;;;;;;
}
#define EVER ;;
. You know.... for(EVER){ ... }
. i run away and hides now - user240438
#define ever (;;)
so I can say for ever { ... }
. I also like #define forever while(1)
so I could make the same statement work as both forever { ... }
and do { ... } forever;
- Chris Lutz
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.
in Java
String("aaa")==String("aaa") //false
//you need to use
String("aaa").equals(String("aaa")) // true
+
for String types, coming from a C background this is completely expected behaviour. - Joe D
==
works on strings as the equality operator? Why is there strcmp
then? - R. Martinho Fernandes
C++
was what I meant` java2s.com/Tutorial/Cpp/0300__string/Stringequals.htm - Shervin Asgari
In C,
int x = 1;
int y = x++ + ++x;
printf("%d", y);
Is ambiguous, what gets printed depends on the compiler. The compiler could store the new value of x++ before ++x is evaluated, or at the end of the statement.
Delphi don't care about typecast like "word" and will read outside of array arr[0..65535] where pos = 65535:
arr[word(pos + 10)]
I know what I do
and generates a wraparound. If you just had written arr[pos + 10] you would get the range error you probably expect. (Tested with D2007 and $RANGECHECKS ON.) - Uli Gerhardt
In PHP:
<?php
$o = new stdClass;
echo count($o);
?>
..prints 1. Never figured out why.
PHP (again?)
First: (unset)
type casting.
$a = 1;
$b = (unset)$a;
var_dump($a); // 1
var_dump($b); // NULL
Usage: http://www.php.net/manual/en/language.types.type-juggling.php#89637
Second: difference between = NULL
and the unset()
function.
$c = 10;
$d = &$c;
$c = NULL;
var_dump($c); // NULL
var_dump($d); // NULL
$e = 10;
$f = &$e;
unset($e);
var_dump($e); // NULL
var_dump($f); // 10 - WTF?
var_dump
s on the first example backwards? Also, the difference between = NULL
and unset
is interesting would it makes sense. I woulnt say it's a WTF. - Nicole
(unset)
cast really is completely useless. In the example you linked to, the only reason why it really saves one line is kind of a unique case anyway. - Nicole
This C program prints a different result on x86 vs. x86-64:
#include <stdio.h>
int main (void)
{
long a = -1;
unsigned b = 1;
printf ("%d\n", a > b);
return 0;
}
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
pic xxx
), it turns out that COBOL is full of XXX Pic
's. - Joe D
С#:
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 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.
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
);
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.
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 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)
In C:
int main() {
int i = 0;
int array[] = {1,2};
return (i[array] + 1 == array[i]);
}
This program will return 1 (true).
array[i]
and i[array]
are both identical to *(array+i)
. - David R Tribble
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
[1] and
Py 3x
[2].
In Python:
i = 1
++i
print i
prints '1'. The line '++i' evaluates to +(+i) (Python doesn't support increment operators)
In C# you can use the new operator on an interface.
Weak typing, in general.
C:
printf("%c\n", 'a' + 3);
PHP:
echo 5 + "3";
And far too many other languages.
'a'
and 3
are both int
s with values of 97 and 3, respectively, and adding them produces an int
of 100, which is passed to printf()
for the argument %c
(which takes an argument of type char
which int
is easily convertible to) which prints "d" (ASCII value 100). Any confusion is from a misunderstanding of the C language. If "a" + 3
worked that way in C, it would be a valid complaint about typing, but that does something totally different. - Chris Lutz
printf()
is a variadic function, which means that nothing is passed as a char or a
short` or a float
. This means that 'a'
is an int
, 3
is an int
, 'a' + 3
is an int
, and it's passed as an int
to where %c
expects an int
and prints it out as a character value. There are no type conversions whatsoever in that example. - David Thornley
'a' + 3
may or may not be equal to 'd'
. - Alok Singhal
%c
converts an int
to unsigned char
for printing, but it is passed as an int
(depending on your architecture, of course). @Alok - This is true, but from a practical perspective even in EBCDIC 'a' + 3 == 'd'
(you only get problems around 'l'
and 'm'
in EBCDIC). - Chris Lutz
COMEFROM [1] 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 [2].
[1] http://en.wikipedia.org/wiki/COMEFROM(a<b) and (b<c)
? ( a<b<c
is a ternary operator in Python, anyone who writes the ternary operator needs to get out more ) - Pete Kirkham
Modula-2 doesn't have elseif
or else if
; it has elsif
elif
instead (I think Python is one). - R. Martinho Fernandes
if ... elif ... else ... fi
. And case ... esac
. - David R Tribble
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".
a = b + c
and it's c
that's causing the trouble, it will show that expression and point to c
. - Daniel C. Sobral
NullReferenceException
, which is probably the most embarassing an pointless exception you can have. - R. Martinho Fernandes
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.
now.GetYear()
pretty much sums up your whole post. - mpen
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.
In MOD_REWRITE
RewriteRule ^([a-zA-Z0-9_-]+)\.php$ $1/ [R,NC]
RewriteRule ^([a-zA-Z0-9_-]+)/$ $1\.php [NC,L]
Will cause:
"file.php > file > file.php > file > file.php > file > ..."
And finally:
Error 500 Too Many Redirects
(In general I find editing .htaccess files to be about as tedious as constructing a properly functioning regular expression.)
HTTP Error 500 Internal server error
. - Talvi Watia
L
? Is that the bit you find confusing? - mpen
L
caused that. I know better now, of course. ;) - Talvi Watia
PHP
$ php -r '::'
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM
WTF? http://en.wikipedia.org/wiki/Scope_resolution_operator
Why not say unexpected T_SCOPE_RESOLUTION_OPERATOR
?
ERROR_REPORTING=OFF
, which BTW is also a feature... - Talvi Watia
In C#: a = cond ? b : c; If 'b' & 'c' are "assign incompatible", you'll never get result, even if 'a' is object. It's frequently used and most idiotically implemented operator from MS. For comparison see implementation in D language (note on type inference).
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"
}
Objective-C's use of @ for strings. Example: @"This is a string."
T"string"
to distinguish between the library's string object and a null-terminated char array. I guess it's a common shorthand feature to C-based languages/libraries. - Rei Miyasaka
in C.
int a;
(&a)[0] = 10; /* will assign value 10 to a */
(&a)[0] is equivalent to *(&a +0), which gives us *(&a), which is nothing but a.
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)
Nat
as a type isomorphic with the natural numbers: it's the set of values that are either zero (Z
) or the successor of a Nat
(S Nat
). For example 1 would be S Z
, 2 S (S Z)
, etc. The rest is just sugar to be able to print Nat
values (deriving Show
), and functions that convert actual numbers to Nat
and Nat
values to actual numbers, using recursion. Oh, by the way, ifs in Haskell work the same way as the conditional operator in Java: result = if condition then 42 else 23
. - R. Martinho Fernandes
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.
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: http:/stackoverflow.com/questions/1724255/why-does-2-2-in-javascript [1]
[1] http://http:/stackoverflow.com/questions/1724255/why-does-2-2-in-javascriptJavaScript:
( {} == {} ) == false
(function is a function) is false
? - Esko
{}
, you create a new object pointer. Two new object pointers aren't the same because they aren't at the same location. - Delan Azabani
( object() == object() ) == False
in Python - mykhal
{}
is not a pointer. It's a new empty object that can be referenced throught either a regular reference variable. Moreover, pointers can be "same" in any language, and point the same objects many times. - Lyubomyr Shaydariv
a = {}; b = a;
we're really setting a pointer because they point to the same object, not a copy. Try it by setting a.c
and seeing that b.c
is also set. - Delan Azabani
pointer
is not so applicable for JS 'cause it's ok to say reference. All JavaScript objects are passed by reference - an implicit pointer saying in C++ terminology. You said, Every time you {}, you create a new object pointer. It's not true because you can write e.g. function(){/*NO REF*/new CustomObject();}
(I choosed CustomObject because its constructor could e.g. display something, but a simple {} might be ommitted by JS optimizer) where a newly created object is NOT referenced at all. - Lyubomyr Shaydariv
here is my 2 cents. In c++:
int* t = new int(15);
delete t;
int(15)
indeed casts 15 to an integer, new int(15)
will create a dynamically allocated integer and assign the value 15 to it. t
will be a pointer to an integer containing 15 - Pieter
The concatenation in Tcl is adding two strings it become one string:
set s1 "prime"
set s2 "number"
set s3 $s1$s2
puts s3
The output is
primenumber
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){
}
}
}
class YourBoss : Exception
in C#, either. - SLaks