Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

"Hello, World!" in zero lines of code

NPM's sloc is a moderately popular tool for counting source lines of code in a file. The tool will attempt to strip out both single and multiline comments and count the remaining lines in order to get an estimate of the 'true' number of lines of code.

However, the parser used is based on regular expressions and is quite simple, so it can be tricked. In this challenge, you will need to construct a hello world program that sloc counts as zero lines of code.

The challenge

  1. Download sloc. If you have npm, this can be done via npm i sloc. We will use the latest version as of the time of posting, which is 0.2.1. Or, use the online testing environment linked at the bottom of this post.

  2. Select any language that sloc supports. A full list is available here.

  3. Write a full program in that language that prints the exact string Hello, World! to stdout. The output may optionally have a trailing newline, but no other format is accepted. The program may output anything to stderr. However, to be considered a valid submission, sloc must see the program as zero lines of source code. You can verify this by running sloc myprogram.ext, which should read Source : 0.

Example

The following Python program is a valid submission:

'''"""''';print("Hello, World!");"""'''"""

If I run sloc hello.py, I get:

---------- Result ------------
 Physical : 1
 Source : 0
 Comment : 1
 Single-line comment : 0
 Block comment : 1
 Mixed : 0
 Empty block comment : 0
 Empty : 0
 To Do : 0
Number of files read : 1
----------------------------

Why? Well, sloc uses ''' or """ to determine if something is a mulitline comment in Python. However, it doesn't pair them up correctly. Thus Python parses our program as:

'''"""''';print("Hello, World!");"""'''"""
\-------/ \--------------------/ \-------/
 comment active code comment

But sloc parses it as:

'''"""''';print("Hello, World!");"""'''"""
\----/\----------------------------/\----/
comment comment comment

(This is not the shortest valid Python submission, so it shouldn't stop you looking for shorter ones)

Scoring

The shortest program in bytes wins.

A note on feasibility

It might be a reasonable question to ask whether this is possible for all languages. I don't know, but all the ones I've looked at so far (Python, C, HTML) have at least one solution. So there's a very good chance it is possible in your language.

There are a number of ideas that can work across multiple languages.

Online test environment

Try it online!

You need to:

  1. Type your code into the input box
  2. Type the extension corresponding to your language into the command args (important!)

Answer*

Draft saved
Draft discarded
Cancel
2
  • 4
    \$\begingroup\$ You only need two - between the}s. \$\endgroup\$ Commented Nov 28, 2020 at 21:20
  • 1
    \$\begingroup\$ Interestingly, my tool of choice cloc is also fooled by your code, unlike most of the other answers! \$\endgroup\$ Commented Dec 11, 2020 at 10:45

AltStyle によって変換されたページ (->オリジナル) /