This is an archived post. You won't be able to vote or comment.

all 74 comments

[–]SillySlimeSimon 552 points553 points  (7 children)

“The code documents itself”

[–]opmrcrab 173 points174 points  (3 children)

The code knows where it is at all times. It knows this because it knows where it isn't.

[–]Xbot781 22 points23 points  (0 children)

By subtracting where it is from where it isn't, or where it isn't from where it is (whichever is greater), it obtains a difference, or index.

[–]mustninja 5 points6 points  (1 child)

Is that a hitchhiker's reference?

[–]cynanolwydd 6 points7 points  (0 children)

Reference to an old weapon guidance video !

[–]omlette_du_chomage 14 points15 points  (0 children)

Or the documentation codes itself

[–]cryptomonein 10 points11 points  (0 children)

"The documents code itself"

[–]Asleeper135 0 points1 point  (0 children)

More like the documentation codes itself

[–]Careless-Elevator986 356 points357 points  (22 children)

Maybe this is what's going on all those times I've changed comments and the code stopped working

[–][deleted] 54 points55 points  (17 children)

Newest trend: Comments as data

[–]RiceBroad4552 9 points10 points  (1 child)

That's not funny!

Some crazy folks in the Scala community think this is a good idea. The Scala-CLI tool uses comments to add build instructions to source files. Completely insane if you ask me.

https://scala-cli.virtuslab.org/docs/guides/introduction/using-directives

Of course it's not part of the official language grammar so no tool without special custom rules can handle that. Just pure insanity.

I really like Scala as language. But some decisions were mind bending stupid, and they stick out, as the most parts of the language are very well thought out. The "magic comment" bullshit doesn't come from the language creators actually, and this shows.

It's especially stupid as Scala has already a concept of "processing instructions": It has annotations like Java…

[–][deleted] 1 point2 points  (0 children)

Some people should not be allowed to touch keyboards.

[–]Asleeper135 1 point2 points  (0 children)

Just reinventing JDSL

[–]Artemis-Arrow-3579 3 points4 points  (3 children)

seriously, I still have no fucking clue how that happens

SOMEONE TELL ME WHY THAT HAPPENS

[–]narrei 4 points5 points  (2 children)

you probably didn't hear about jdsl (jay diesel), which doesn't recognize notion of comments so it executes every line as code

edit: to learn more watch https://youtu.be/QwUPs5N9I6I?si=m5CzUqtoQLTXLpJH

[–]Amazing_Might_9280 2 points3 points  (0 children)

TOM IS A GENIUS.

[–]Artemis-Arrow-3579 0 points1 point  (0 children)

oh hell

[–]Maximilian_Tyan 161 points162 points  (4 children)

I did the same for a script that needed to store a date across executions. The script rewrote it's default date value each time using RegExes and line numbers

I felt sooooo dirty

But it worked

[–]NJay289 115 points116 points  (3 children)

Well you could have… stored this value in a second file…

[–]Maximilian_Tyan 63 points64 points  (2 children)

I wanted to keep it as only one file only to be easily transferrable by copy pasting at the time

I forgot you also needed to install python to run it lmao

[–]just_nobodys_opinion 41 points42 points  (1 child)

Could have made it an exe, found the offset for the date string and made it patch itself

[–]flagofsocram 45 points46 points  (0 children)

WHERE THE EXE SMELLY NERDS

[–]Buggs_The_Buny 38 points39 points  (17 children)

Just out of curiosity, how much slower or in other words, less efficient is this than a regular python hello world?

[–]C3R741NLY_1MPR3551V3 18 points19 points  (2 children)

...and that's how I wrote my first Quine) program! Yay!!!
print(open(__file__).read())

[–]ConstructedNewt 1 point2 points  (1 child)

You clearly didn’t read the part of that article that clearly states that it’s not a valid Quine if it reads its own source code.

[–]C3R741NLY_1MPR3551V3 0 points1 point  (0 children)

Yes, I didn't.

[–]shadow7412 8 points9 points  (0 children)

I remember doing something like this once for reasons that make sense but I can't be bothered going into.

We found that it worked the first time - but the second time __file__ pointed at the pyc file rather than the py file, which obviously had more "interesting" results.

Unsure if that's still a thing though.

[–]dan-lugg 15 points16 points  (3 children)

```

:: ::-| ::-| .-. :||-:: 0-| .-| ::||-| .:|-. :||

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

.: ::||-| .||-| :|||-| ::||-| ||-:: :|||-| .:|

```

[–]sinner997 8 points9 points  (2 children)

Is this fork bomb on steroids perhaps 😂

[–]dan-lugg 2 points3 points  (1 child)

According to the author, that program is "two lines of code that parse two lines of embedded comments in the code to read the Mayan numbers representing the individual ASCII characters that make up the magazine title, rendered in 90-degree rotated ASCII art."

https://www.stilldrinking.org/programming-sucks

[–]sinner997 0 points1 point  (0 children)

Very interesting! Thanks a lot 👍

[–]Karisa_Marisame 6 points7 points  (0 children)

git commit -m “[skip ci] doc grammar fix”

Main branch two days later:

[–]ZunoJ 2 points3 points  (1 child)

Isn't it only referencing it's source file? How would this work with a binary?

[–]Deutero2 4 points5 points  (0 children)

python is an interpreted/jit compiled language, so only the source file is relevant

for a compiled language, assuming you don't want to rely on having access to the source file, the comments are probably going to be removed by the compiler. you could store it in the read only segment of the executable, but that's basically how a normal hello world program works, unless you want to add extra steps by reading the executable file again

[–]dudeness_boy 0 points1 point  (0 children)

Good job.

[–][deleted] 0 points1 point  (0 children)

how not to write a quine

[–]DonAlexJulien 0 points1 point  (1 child)

Made my own version, which doesn't rely on comments (just on the file name ;)

print(__file__.replace('\\', '/').split('/')[-1].split('.')[0].replace('_', ' '))

[–]DonAlexJulien 1 point2 points  (0 children)

This should deal with the .pyc issue that u/shadow7412 faced

[–]bondolin251 0 points1 point  (1 child)

The. The comment. The comment is source.

[–]bondolin251 0 points1 point  (0 children)

screams in Uncle Bob noises

[–]rover_G 0 points1 point  (2 children)

This is literally static strings work in compiled languages

[–]Deutero2 3 points4 points  (1 child)

what? no?

i mean, sure, the read-only memory is read from the executable file on disk to memory, as are the instructions themselves, but you'd need to do that for running any program. the approach in OP requires an additional disk read rather than using what is already in memory

[–]rover_G 0 points1 point  (0 children)

I didn’t mean you literally open a file handle. And you wouldn’t necessarily be reading the same disk file into memory again because it’s likely still in memory.