r/programming • • May 10 '18

Java: Executing code in comments?!

http://programming.guide/java/executing-code-in-comments.html
260 Upvotes

84 comments sorted by

View all comments

1

u/DynamicTextureModify May 11 '18

So I'm not a Java guy... can someone explain to me the rationale behind parsing unicode expressions in comments? This doesn't seem like it should reasonably exist.

3

u/drysart May 11 '18

Java effectively does a pre-pass over a source file to replace all \u0000 sequences with the actual character that the escape references to. That it's in a comment is irrelevant; the escape sequences are parsed through the entire source file independent of any other context.

It's pretty idiotic, but it's Java so some level of brain damage is to be expected.

0

u/DynamicTextureModify May 11 '18

That's ridiculous. Brain damage is right.

I don't care if it's a "pre-pass" or not, commented lines should always be ignored.1


  1. Don't get me started on nonsense like those PHP interpreting engines that use fucking docblocks programmatically for database definitions and shit. I almost had an anyuerism when I started reading about that.

-2

u/recursive May 11 '18

That's how it already works. What you probably object to is how java defines "lines".

1

u/DynamicTextureModify May 12 '18

// \u000d System.out.println("Hello World!"); Is a line. It only becomes 2 lines after Java parses the unicode in this line. The fact that it reaches the point where it could be considered 2 lines is the problem in the first place.

1

u/recursive May 12 '18

I think we're all in agreement on that.

1

u/aioobe May 11 '18

This is basically the first thing the article addresses.

1

u/DynamicTextureModify May 12 '18

It really doesn't.

You don’t even need to figure out where comments begin and end!

Is not a good thing. Why would anyone want that?

1

u/aioobe May 12 '18

You asked for the rationale behind parsing unicode expressions in comments. The rationale is that it makes it trivial to transform a program written in Unicode to ASCII.

Why anyone (except the person writing the code for transforming between encodings) would want that is not answered in the article, but that is a different question.

Also, I don't think anyone here argues that it is a good thing.

1

u/DynamicTextureModify May 12 '18

I guess you're right, I was just expecting the rational to be... well, rational. Hah.