Is this a legal Java program? If so, what does it print?
\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020 \u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079 \u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020 \u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028 \u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020 \u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b \u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074 \u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020 \u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b \u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d
Solution 17: Huh?
Of course it's a legal Java program! Isn't it obvious? It
prints Hello world. Well, maybe it isn't so obvious. In fact, the
program is totally incomprehensible. Each time you use a Unicode escape
unnecessarily, you make your program a bit less comprehensible, and this program
takes the concept to its logical extreme. In case you are curious, here is what
the program looks like after the Unicode escapes are translated to the
characters they represent:
public class Ugly {public static void main( String[] args){ System.out .println( "Hello w"+ "orld");}}
Here is how it looks after cleaning up the formatting:
public class Ugly { public static void main(String[] args) { System.out.println("Hello w" + "orld"); } }
The lesson of this puzzle is: Just because you can doesn't mean
you should. Alternatively, If it hurts when you do it, don't do it! More
seriously, this puzzle serves to reinforce the lessons of the previous three:
Unicode escapes are essential when you need to insert
characters that can't be represented in any other way into your program. Avoid
them in all other cases. Unicode escapes reduce program clarity and
increase the potential for bugs.
For language designers, perhaps it should be illegal to use
Unicode escapes to represent ASCII characters. This would make the programs in
Puzzles 14, 15, and 17 (this puzzle) invalid, eliminating a great deal of
confusion. This restriction would cause no great hardship to programmers.
No comments:
Post a Comment
Your comments are welcome!