What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
What is, in your opinion, the most surprising, weird, strange or really "WTF" language feature you have encountered?
Please only one feature per answer.
I struggled a bit about this:
1;
In perl, modules need to return something true.
Being able to cast out of range ints to enums in C# is quite weird in my opinion. Imagine this enum:
enum Colour
{
Red = 1,
Green = 2,
Blue = 3
}
Now, if you write:
Colour eco;
eco = (Colour)17;
The compiler thinks that’s fine. And the runtime, too.
See here for more details.
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.
Trigraphs in C and C++.
int main() {
printf("LOL??!");
}
This will print LOL|
, because the trigraph ??!
is converted to |
.
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.
In ruby/python/c, you can concatenate strings just like this:
a = "foo" "bar"
print a # => "foobar"
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.
I would definitely give Perl the honor of having multiple horrible examples:
if(!$#var)
or
if($mystring =~ m/(\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
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]);
}
PHP's handling of numeric values in strings. See this previous answer to a different question for full details but, in short:
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.
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.
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.
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.
Weak typing, in general.
C:
printf("%c\n", 'a' + 3);
PHP:
echo 5 + "3";
And far too many other languages.
INTERCAL is probably the best compendium of strangest language features. My personal favourite is the COMEFROM statement which is (almost) the opposite of GOTO.
COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.
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.
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.
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 :)
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.
For those who don't know, bc
is an "arbitrary precision calculator language", and I use it quite often for quick calculations, particularly when the numbers involved are large ($
is the prompt):
$ bc -lq
12^345
20774466823273785598434446955827049735727869127052322369317059031795\
19704325276892191015329301807037794598378537132233994613616420526484\
93077727371807711237016056649272805971389591721704273857856298577322\
13812114239610682963085721433938547031679267799296826048444696211521\
30457090778409728703018428147734622401526422774317612081074841839507\
864189781700150115308454681772032
bc
has been a standard Unix command for a long time.
Now for the "WTF feature". This is from man bc
(emphasis mine):
quit: When the quit statement is read, the bc processor is terminated, regardless of where the quit statement is found. For example, "if (0 == 1) quit" will cause bc to terminate.
halt: The halt statement (an extension) is an executed statement that causes the bc processor to quit only when it is executed. For example, "if (0 == 1) halt" will not cause bc to terminate because the halt is not executed.
In 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 Python:
>>> x=5
>>> 1<x<10
True
>>> 1<x<3
False
Not a WTF, but a useful feature.
The bigest collection (today 1313) of decent and weird programming languages I know, you will find here: http://99-bottles-of-beer.net/ be prepared to see real weird stuff ;-) Everybody should make his one choice
Perl's many special variables
$. $_ $_# $$ $[ @_
They're definitely shorter than the equivalent, but if you subscribe the school of thought that you write code for humans rather than trying to impress the computer then it makes reading the code for a non-Perl veteran a lot harder.
Read the complete list here
I would not dare to claim that XML is a programming language, but isn't it close to our heart? :-)
The strangest feature, to my mind, in XML is that the following is a well-formed document:
<_....>
</_....>
Here is the the lexical definition of NT-Name that allows consecutive dots.
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.
Here's a good bunch of strange C features: http://www.steike.com/code/useless/evil-c/
I once wrote a programming language that had a "strfry" operator:
"hello world"?
# => "wdo rlholle"
Useful, eh?
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.
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
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 config in a real language is much much better.
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)
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
As an NHibernate enthusiast, I was thrilled when I heard about become
from Smalltalk... e.g.
a become: b
it literally changes the a object into b, which makes it trivial to write lazy-initialized proxies because all references to a will now reference b. Pretty neat!
I think it qualifies as a strange language feature in that no other language has this ability to my knowledge.
In 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
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 ...
In Java:
int[] numbers() {
return null;
}
Can be written as:
int numbers() [] {
return null;
}
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
COMEFROM is the weirdest, and probably most useless, language feature I ever saw.
Runner-up would be the ternary operator, because it violates rule #1 of optimization. And it does more harm than it solves problems. It does more harm because it makes code less readable.
Not really a language feature, but interesting/awesome use of features is Duff's device.
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 retrospect, FORTRAN's computed goto is pretty odd. Wikipedia tells me some BASICs outdo it.
Another famous favourite is Algol 60's call by name parameter passing.
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);
In Perl you can do:
my $test = "Hello World";
substr($test, 0, 5) = "Goodbye";
print $test;
Is this possible in other languages?
I was taken by surprise that you can change a class's inheritance chain in Perl by modifying its @ISA
array.
package Employee;
our @ISA = qw(Person);
# somwhere far far away in a package long ago
@Employee::ISA = qw(Shape);
# Now all Employee objects no longer inherit from 'Person' but from 'Shape'
I'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.
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
Perl has lots of strange quirky features, loved by some, hated by others. There are a few I find particularly WTF-ish:
automatic list flattening, i.e., how (1, 2, (3, 4))
is the same as (1, 2, 3, 4)
;
how every operator or function in the language can behave in six different ways depending on the context;
the more than a hundred operators and their complex precedence rules. Fortunately Mark Lentczner came up with The Periodic Table of the Operators (Perl 6);
the multiple variable namespaces:
$var
is a scalar named var
;$var[41]
is the 42nd element of an array named @var
;$var{'foo'}
is the element with key 'foo' in the hash named %var
;Hi, check out this link for some fun: http://computersight.com/programming/five-strangest-programming-languages/
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!
The many name spaces of C:
typedef int i;
void foo()
{
struct i {i i;} i;
i: i.i = 3;
printf( "%d\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);
}
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
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.
Fun with auto boxing and the integer cache in Java.
Integer foo = 1000;
Integer bar = 1000;
foo <= bar; // true
foo >= bar; // true
foo == bar; // false
//However, if the values of foo and bar are between 127 and -128 (inclusive)
//the behaviour changes:
Integer foo = 42;
Integer bar = 42;
foo <= bar; // true
foo >= bar; // true
foo == bar; // true
Explanation
A quick peek at the java source code will turn up the following:
/**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}
Note: IntegerCache.high defaults to 127 unless set by a property.
What happens with auto boxing is that both foo and bar the same integer object retrieved from the cache unless explicitly created: e.g. foo = new Integer(42), thus when comparing reference equality, they will be true rather than false. The proper way of comparing Integer value is using .equals;
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.
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:
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.";
In fortran (77 for sure, maybe in 95 as well), undeclared variables and arguments beginning with I
through N
(the "in" group) will be INTEGER
, and all other undeclared variables and arguments will be REAL
(source). This, combined with "whitespace optional in certain cases" resulted in one of the most famous bugs.
As told by Fred Webb in alt.folklore.computers
in 1990:
I worked at Nasa during the summer of 1963. The group I was working in was doing preliminary work on the Mission Control Center computer systems and programs. My office mate had the job of testing out an orbit computation program which had been used during the Mercury flights. Running some test data with known answers through it, he was getting answers that were close, but not accurate enough. So, he started looking for numerical problems in the algorithm, checking to make sure his tests data was really correct, etc.
After a couple of weeks with no results, he came across a
DO
statement, in the form:DO 10 I=1.10
This statement was interpreted by the compiler (correctly) as:
DO10I = 1.10
The programmer had clearly intended:
DO 10 I = 1, 10
After changing the
.
to a,
the program results were correct to the desired accuracy. Apparently, the program's answers had been "good enough" for the sub-orbital Mercury flights, so no one suspected a bug until they tried to get greater accuracy, in anticipation of later orbital and moon flights. As far as I know, this particular bug was never blamed for any actual failure of a space flight, but the other details here seem close enough that I'm sure this incident is the source of 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.
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.
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;;;;;;;;;;;;;;;;;;;;;;;
}
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'; }
I'm surprised no one mentioned the REALLY ugly switch-case implementation im 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.
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...
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.
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.
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;
Some 20 years ago, when I last dabbled in MUMPS, the implementations had some curious limitations. While hosts MUMPS was becoming ever more popular, MUMPS was traditionally a self-hosted language: computer language, operating system and database in a single package.
MUMPS was essentially about its database. Essentially, a huge multidimensional hash table, supported by a B* tree that made for very fast access. There wasn't any barrier between the language and the database either: if you wanted something to be stored there, you just prefixed the variable with a symbol indicating it was to be persisted to the backing store.
On the other hand, a filesystem was almost non-existent, and support for it even less so. About the only thing one could do was to load a program into memory from a file, and send whatever was in memory back to a file. And one had better clear the buffer before loading, otherwise it would get mixed with whatever was there first.
So, considering its self-hosting nature and the extremely hostile file system, one could wonder how these programs were edited. The editors, as a matter of fact, were written in MUMPS itself -- so how could the editor store the program in memory without written over itself?
Well, the trick was the ability to execute the contents of a variable as source code. An editor, then, loaded itself into variables, executed itself in them, cleared the memory, and then loaded, saved and edited files in memory, all the time executing from variables.
Add to that the fact that all commands could be shortened to their first letters (except the Z commands, shortened to two letters, that mostly handled the filesystem), and curiosities like the fact that IF
(I
) set a variable which was then consulted by ELSE
(E
) -- and, of course, could be overridden by any intervening I
, or by the program itself. On second thought, I think the whole language was a WTF. And, yet, it had a strange attraction.
In 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.
In JavaScript, seeing !!a
for the first time (as a way to convert to boolean).
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' on via package 'contents of string $a'".
In JavaScript:
alert(111111111111111111111) // alerts 111111111111111110000
This was quite damaging to some 64bit keys I passed back and forth in JSON.
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
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
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"
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'
}
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
This is a lack of a feature which is weird: Python has no switch
statement (although workarounds exist).
APL (other than ALL of it), the ability to write any program in just one line.
e.g. Conway's Game of Life in one line in APL:
If that line isn't WTF, then nothing is!
And here is a video
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.
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!
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!
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.
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)
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 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
Why does C#'s List<T>.AddRange() not let me Add elements of a subtype of T? List<T>.Add() does!
All it would take would be ONE extra line of code on Microsoft's part:
public void AddRange<S>(
IEnumerable<S> collection
) where S : T
When I was in college, I did a little bit of work in a language called SNOBOL. The entire language, while cool, is one big WTF.
It has the weirdest syntax I've ever seen. Instead of GoTo, you use :(label). And who needs if's when you have :S(label) (goto label on success/true) and :F(label) (goto label on failure/false) and you use those functions on the line checking some condition or reading a file. So the statement:
H = INPUT :F(end)
will read the next line from a file or the console and will go to the label "end" if the read fails (because EOF is reached or any other reason).
Then there is the $ sign operator. That will use the value in a variable as a variable name. So:
ANIMAL = 'DOG'
DOG = 'BARK'
output = $ANIMAL
will put the value 'BARK' on teh console. And because that isn't weird enough:
$DOG = 'SOUND'
will create variable named BARK (see the value assigned to DOG above) and give it a value of 'SOUND'.
The more you look at it, the worse it gets. The best statement I ever found about SNOBOL (from link text) is "the power of the language and its rather idiomatic control flow features make SNOBOL4 code almost impossible to read and understand after writing it. "
In 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.
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 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 for more details
Quoting Neil Fraser (look at the end of that page),
try {
return true;
} finally {
return false;
}
(in Java, but behaviour is apparently the same in JavaScript and Python). The result is left as an exercise to the reader.
EDITED: As long as we are on the subject consider also this:
try {
throw new AssertionError();
} finally {
return false;
}
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".
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:
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){
}
}
}
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 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
Anything will autometic pluralizes or singularizes any class and member names.
Linq-to-Sql, for example
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#, 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.
Java's Integer class's base-conversion static methods. :P Very few languages have this functionality built right in, it seems.
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
Perl's $[
, this was mentioned in another earlier post about generic perl variables, but it deserves specific mention with better explanation. It isn't used in the wild but what it does is change the starting index of an array and here is the clincher it works globally, such that $[
will modify the whole running instance of perl. 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
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.
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, 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
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)
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 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".
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!
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";
I've always been a huge fan of the PHP error thrown when using two colons in a row out of context:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /path/to/file/error.php on line 3
The first time I encountered this I was absolutely befuddled.
In 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.).
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 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 invokation, 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.
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();
}
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.
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.
MUMPS. There are lots of WTF features, I've picked one, the if
statement. (Note that I'm using a rather verbose coding style below in order to accomodate those who don't know the language; real MUMPS code is usually more inscrutable to the uninitiated.)
if x>10 do myTag(x) ; in MUMPS "tag" means procedure/function
else do otherTag(x)
This is similar to saying in Java:
if (x > 10) {
myMethod(x);
} else {
otherMethod(x);
}
Except that in MUMPS, the else
statement isn't syntactically part of the if block, it is a separate statement that works by examining the built-in variable $TEST
. Every time you execute an if
statement it sets $TEST
to the result of the if
statement. The else
statement actually means "execute the rest of line if $TEST
is false, otherwise skip to the next line".
This means that if x
was greater than 10 and thus the first line called myTag
, and myTag
contains if
statements, then the behavior of the else
depends not on the if
in the line above it but on the last if
evaluated inside of myTag
! Because of this "feature", MUMPS coders are generally taught write the above code like this to be safe:
if x>10 do myTag(x) if 1
else do otherTag(x)
The if 1
at the end of the first line ensures that $TEST
is set correctly before control proceeds to the next line. (BTW, the spacing here has to be just so, with two spaces after the else
and one space in all the other places. The spacing is odd but at least it's very orthogonal once you understand the pattern.)
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.
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);
}
}
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]]
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.
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:)
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.
Some early dynamic languages (including, if I remember correctly, early versions of Perl) hadn't figured out what was good dynamism and what was bad dynamism. So some of them allowed this:
1 = 2;
After that statement, the following would be true:
if(1 + 1 == 4)
In 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.)
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".
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.
In Lisp you can copy a list, and you can copy a vector, and you can copy a struct, and you can copy a CLOS object...
... but you cannot copy an array or a hash table.
JCL Conditional execution.
//STEP02 EXEC PGM=PROG02,COND=(4,GT,STEP01) .
This features allows you to run or not run a step depending on the return code from previous steps. Quite a nice feature really.
Except for a couple of small features which turn the logic inside out and backwards.
First the step does NOT run if the condition is true.
Secondly the 4,GT,STEP01 actually means "if the return code from STEP01 is greater than 4"
So the whole thing means "Do not run this step if the return code from STEP01 is greater than 4". Which is the almost but not quite the same as a naive interpretation "Run the step if 4 is greater than the return code from STEP01".
Given that only time you ever look at these things seriously is about 2.30 am with a frantic nightshift operator at the other end of the line this double ambiguity leads to serious headaches.
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 PHP, a string is as good as a function pointer:
$x = "foo";
function foo(){ echo "wtf"; }
$x(); # "wtf"
Unfortunately, this doesn't work:
"foo"();
C++:
void f(int bitand i){ //WTF
i++;
}
int main(){
int i = 0;
f(i);
cout << i << endl; //1
return 0;
}
In PHP "true", "false" and "null" are constants which normally cannot be overridden. However, with the introduction of namespaces in PHP >=5.3, one can now redefine these constants within any namespace but the global namespace. Which can lead to the following behaviour :
namespace {
define('test\true', 42);
define('test\false', 42);
define('test\null', 42);
}
namespace test {
var_dump(true === false && false === null); // is (bool) true
}
Of course if you want your trues to be true, you can always import true from the global namespace
namespace test {
var_dump(\true === \false); // is (bool) false
}
In 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...
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!
Python 2.x
>>>True = False
>>>True
False
You can really make someone become crazy with this one.
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.
Labeled break and continue statements in Java..
They allow you to break out of multiple control-blocks with a single break.
C# has a feature called "extension methods", which are roughly analogous to Ruby mix-ins - Essentially, you can add a method to any pre-existing class definition (for instance, you oould add "reverse()" to String if you were so inclined). That alone is fine- The "Weird" part is that you can add these extension methods, with a method body and everything, to an interface. On the one hand, this can be handy as a way to add a single method to a whole swath of classes which aren't part of the same inheritance tree. On the other, you're adding fleshed out methods to interfaces, essentially breaking the very definition of an interface.
try: 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.
def show_square(string):
try:
n = int(string, 10)
except ValueError:
print "I can't do that, Dave."
else:
print n * n
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.
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
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).
Java Generics Are a WTF:
List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2
2: Is illegal (???) this is puzzling but you have to thing 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.
In Scala, there are no operators, just methods. So a + b - c
is actually the same as a.+(b).-(c)
. In this, it is equal to Smalltalk. However, unlike Smalltalk, precedence is taken into account. The rules are based on the first character, so an hypothetical method called *+
would have precedence over one called +*
. An exception is made so that any method ending in =
will have the same precedence as ==
-- meaning !!
and !=
(non-hypothetical methods) have different precedence.
All ASCII letters have the lowest precedence, but all non-ASCII (unicode) characters have the highest precedence. So if you wrote a method is
comparing two ints, then 2 + 2 is 1 + 3
would compile and be true. Were you to write it in portuguese, é
, then 2 + 2 é 1 + 3
would result in error, as it would see that as 2 + (2 é 1) + 3
.
And, just to top off the WTF of operators in Scala, all methods ending in :
are right-associative instead of left-associative. That means that 1 :: 2 :: Nil
is equivalent to Nil.::(2).::(1)
instead of 1.::(2).::(Nil)
.
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.
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.
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>
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.
In Java, if the value of x is NaN then x == x
returns false and x != x
returns true.
The weird things C++ templates can be used for, best demonstrated by "Multi-Dimensional Analog Literals" which uses templates to compute the area of "drawn" shapes. The following code is valid C++ for a 3x3 rectangle
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
Or, another example with a 3D cube:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
Inform 7. An example of a valid program:
Chomsky is a room. A thought is a kind of thing. Color is a kind of value. The colors are red, green and blue. A thought has a color. It is usually Green. A thought can be colorful or colorless. It is usually colorless. An idea is a thought in Chomsky with description "Colorless green ideas sleep furiously." A manner is a kind of thing. Furiously is a manner. Sleeping relates one thought to one manner. The verb to sleep (he sleeps, they sleep, he slept, it is slept, he is sleeping) implies the sleeping relation. Colorless green ideas sleep furiously.
Other silliness like this Turing machine simulator can be found.
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.
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...
Inheriting from random class in Ruby:
class RandomSubclass < [Array, Hash, String, Fixnum, Float, TrueClass].sample
...
end
(first seen at http://stackoverflow.com/questions/63998/hidden-features-of-ruby/474888#474888)
this made me stunning
#define _ -F<00||--F-OO--;
int F=00,OO=00;main(){F_OO();printf("%1.3f\n",4.*-F/OO/OO);}F_OO()
{
_-_-_-_
_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_-_-_-_-_
_-_-_-_-_-_-_-_
_-_-_-_
}
Smalltalk:
Have a class method in a class Test, that returns a constant string:
method1
^ 'niko'
You should expect that this method constantly returns the string 'niko' whatever happens. But that is not the case.
s := Test method1
(Set s to 'niko'.)
s at: 4 put: $i.
(Set s to 'niki'.)
s := Test method1
(Set s to 'niki' again.)
So, what happens is that the second line of code permanently changed method1 to return 'niki' rather than 'niko', even though the source code of the method was not updated.
In C++, the following expression returns true:
1 == 2??!??! 'A' == 'B'?! false: true
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.
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 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 ) )
PHP's list construct:
$array = array(0,1,2);
list (,,$x) = $array;
$x == 2; // true
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.
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.
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!
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).
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.
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.
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.
c#.net
add textbox1 to the form
int x = 5;
textbox1.Text = x;
its work!!!
In J, most primitives (a.k.a. functions) are monadic (one argument) or dyadic (two arguments, one to the left, one to the right). But the amend primitive takes 3 (I think it's the only one, besides foreigns). It's understandable that it would take 3, but it just looks... wrong at first.
vector =: i. 10 NB. Vector will be 0 1 2 3 4 5 6 7 8 9
(10) (0) } vector NB. Will yield 10 1 2 3 4 5 6 7 8 9
In J, foreigns (!:
) are various functions bunched together. The left argument is a category, where as the right are often (but not always) incremental values for different... stuff. For example:
2!:55 NB. Close console 9!:10 NB. Set print precision 6!:0 NB. Actual time 6!:2 NB. Execution time 4!:3 NB. Loaded scripts
Of course, the smart thing is to wrap them, but some you just commit to memory. BTW, all of those are, come to think of it, triadic, with 2 arguments to the right and one to the left. None of the above will work unless you give them a final valid argument.
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
Python (specifically, CPython)
>>> x = 0
>>> x is 0
True
>>> x = 256
>>> x is 256
True
>>> x = 257
>>> x is 257
False
>>> x = -5
>>> x is -5
True
>>> x = -6
>>> x is -6
False
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.
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 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, 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 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)
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
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 JavaScript the result of a method can depend upon the style braces are placed. This is the K&R style, where braces are placed right after the method signature and after a return statement:
var foo = function() {
return {
key: 'value'
};
}
foo() // returns an object here
Now, if I format this code to the Allman style, where braces are always placed on a new line, the result is different:
var foo = function()
{
return
{
key: 'value'
};
}
foo() // returns undefined here
How come? In JavaScript the language places automatically semicolons at the end of each line if you won't do it yourself. So what really happened in the last code fragment was this:
var foo = function()
{
return; // here's actually a semicolon, automatically set by JavaScript!
{
key: 'value'
};
}
So if you'd call foo()
, the first statement in the method would be a return statement, which would return undefined
and would not execute other following statements.
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! :)
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"
}
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.
Two words: Lambda expressions Or phrased differently: Functional programming This style is a "write-only" language feature.
in Ruby ...
i=true
while(i)
i=false
a=2
end
puts defined?(a) // returns true
The following C# code throws NullReferenceException rather than print 1:
static void SomeMethod(string format, params object[] args)
{
Console.WriteLine(args.Length);
}
static void Main(string[] args)
{
SomeMethod("blabla", null, "Ok here"); // print 2
SomeMethod("blabla", null); // exception
}
In 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(); });
In Python:
i = 1
++i
print i
prints '1'. The line '++i' evaluates to +(+i) (Python doesn't support increment operators)
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
95 WTF moments in Java: http://www.javapuzzlers.com/
For example,
System.out.println(12345 + 5432l);
prints 17777.
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
}
};
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
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 Python:
>>> x = 4
>>> y = 1000000
>>> x is 4
True
>>> y is 1000000
False
>>>
Just try it if you don´t believe me!
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.
VBScript has so-called bracket identifiers, which are identifiers defined enclosed in square backets, like this:
[Foo]
They're quite handy, actually, as they allow you to name variables and routines after reserved words, call methods of third-party objects whose names are reserved words and also use almost any Unicode characters (including whitespace and special characters) in identifiers. But this also means that you can have some fun with them:
[2*2] = 5
[Здравствуй, мир!] = [Hello, world!]
[] = "Looks like my name is an empty string, isn't that cool?"
For[For[i=0]=[0]To[To[To[0]
[Next[To]([For[i=0])=[For[i=0]
Next
On the other hand, bracket identifiers can be a gotcha in case you forget the quotes in a statement like this:
If MyString = "[Something]" Then
because If MyString = [Something] Then
is a perfectly legal syntax. (And that's why an IDE with syntax highlighting is a must!)
More info on bracket identifiers in Eric Lippert's blog:
In Java, there is some inconsistency as to how Strings handle the == operator depending on how it was constructed.
String a = "Hello";
String b = "Hello";
System.out.println(a == b ); // prints true.
String c = new String("Hello");
String d = new String("Hello");
System.out.println(c == d ); // prints false
Here is one about python:
>>> print 07
7
>>> print 08
File "<stdin>", line 1
print 08
^
SyntaxError: invalid token
Ain't that a beauty?
Especially unthoughtful when you think of how human write dates, which has the following effect:
datetime.date(2010,02,07) # ok
datetime.date(2010,02,08) # error!
(the reason is that 0x
is interpreted as octal, so print 010
prints 8
!)
In 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
).
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)
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.
Well the first thing that came to my mind was 'noop', my brain did the same thing when I first came across it!
In php:
easter_date — Get Unix timestamp for midnight on Easter of a given year
int easter_date ([ int $year ] )
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.
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;
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.
C++1x Lambda's:
[] (int x) { std::cout << x << std::endl; } ();
These can be abused for some odd syntax:
[](){}();[]{[]{}();}();
This is completely valid C++1x.
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");
}
Perl has the yada yada operator (...
).
The so called “yada yada” operator of Perl 6 heritage is a shortcut to mark unimplemented code:
if ($condition) { ... }
is the same as
if ($condition) { die "not yet implemented" }
In Java (Actually, I have wrote this on different SO post recently) :
int x = 1 + + + + + + + + + + + + + 1;
System.out.println(x);
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.
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.
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.)
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.
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
);
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$
Perl:
It's possible to write a program consisting entirely of punctuation.
How does this even work?!
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.
"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.
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.
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()");
}
}
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.
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.
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
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"
In C#, there is the Managed Extensibility Framework in .Net 4 - It offers a container for IoC-DI but it is spookysaurus: http://mef.codeplex.com
NSIS (the Nullsoft Scriptable Install System) has the StrCmp
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.
The entirety of the Malbolge programming language: http://en.wikipedia.org/wiki/Malbolge
In Ruby, default function arguments are working in mysterious ways.. (or at least for someone familiar with Python):
def f(a="a", b="b")
return [a, b]
end
f() # -> ["a", "b"]
f(a="A") # -> ["A", "b"]
f(b="B") # -> ["B", "b"] # WTF? expected ["a", "B"]
Perl
if you try break the line with backslash, like
my $x = 1 + \
1;
print $x;
you don't get 2
, but rather something like 313781057
, and the result is always different.
don't use backslash for this in Perl, it is used to get the references (something like pointers)
In Java,
String s = null;
System.out.println(s + "hello");
This outputs "nullhello".
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
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
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.
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
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.
Ruby
Time.parse
often pretends that the parsing did not failed, 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
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
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..."
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');
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]
.
In C:
int main() {
int i = 0;
int array[] = {1,2};
return (i[array] + 1 == array[i]);
}
This program will return 1 (true).
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...
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" ?
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.
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
I like sneaking-in octal values in C:
int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };
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.
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!!
in Java
String("aaa")==String("aaa") //false
//you need to use
String("aaa").equals(String("aaa")) // true
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).
In Perl:
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 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.
I added the "format" function to Lisp in about 1977, before "printf" even existed (I was copying from the same source as Unix did: Multics). It started off innocently enough, but got laden with feature after feature. Things got out of hand when Guy Steele put in iteration and associated features, which were accepted into the Common Lisp X3J13 ANSI standard. The following example can be found at Table 22-8 in section 22.3.3 of Common Lisp the Language, 2nd Edition:
(defun print-xapping (xapping stream depth)
(declare (ignore depth))
(format stream
"~:[{~;[~]~:{~S~:[->~S~;~*~]~:^ ~}~:[~; ~]~ ~{~S->~^ ~}~:[~; ~]~[~*~;->~S~;->~*~]~:[}~;]~]"
(xectorp xapping)
(do ((vp (xectorp xapping))
(sp (finite-part-is-xetp xapping))
(d (xapping-domain xapping) (cdr d))
(r (xapping-range xapping) (cdr r))
(z '() (cons (list (if vp (car r) (car d)) (or vp sp) (car r)) z)))
((null d) (reverse z)))
(and (xapping-domain xapping)
(or (xapping-exceptions xapping)
(xapping-infinite xapping)))
(xapping-exceptions xapping)
(and (xapping-exceptions xapping)
(xapping-infinite xapping))
(ecase (xapping-infinite xapping)
((nil) 0)
(:constant 1)
(:universal 2))
(xapping-default xapping)
(xectorp xapping)))
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
Delphi don't care about typecast like "word" and will read outside of array arr[0..65535] where pos = 65535:
arr[word(pos + 10)]
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
C/C++:
The Fast Inverse Square Root 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);
}
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).
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...
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";