1
\$\begingroup\$

I'm using javascript as a golfing language. I've just noticed that the number of characters used in a program may differ from the number of bytes that program takes (due to text encoding?). For example, TIO says that the number of characters in this answer is 101 whereas the number of bytes is 116. I'm fairly new to code golfing so I don't know what to do in this case. Should I just include the number of bytes and ignore the number of characters? Or should I include both?

Including both seems reasonable to me as it shows both how short and light a program is.

\$\endgroup\$

2 Answers 2

7
\$\begingroup\$

The standard for most challenges on this site is to measure code in bytes, so the length of the code in bytes should be included in the answer. Any other metrics are unnecessary, but not forbidden, so feel free to include them if you think they are relevant or interesting.

The answer you linked has more bytes than characters because it is encoded in UTF-8, which uses between one and four bytes per character, due to the need to encode more than 256 characters.

You might also find this helpful: How to count bytes FAQ

\$\endgroup\$
1
\$\begingroup\$

The header for an answer needs to include its score, as that's necessary for people to compare the answers. For most answers, this will be the length in bytes.

It's also helpful to include anything else in the header that might be useful to someone trying to assess the score themself. For example, if the byte count is higher than the character count due to multibyte characters, something like "6 bytes, 4 characters" or "6 bytes of UTF-8" would help to explain to people reading the code why the byte count was correct. (Sometimes it's helpful to give an explanatory note even when the byte count equals the character count; things like "10 bytes in Jelly's codepage" or "4 bytes of ASCII" are helpful to people's understanding in languages which use many non-ASCII characters, or languages which are normally encoded using sub-byte encodings but in this case are using a single-byte character set instead.)

For a you'd normally include the score in the header rather than a byte count, but code challenges frequently have tie-breaks, and program length is a common tie break. So including that in the header is helpful if it seems like there's any chance that there might be a tie. (For example, if you've maxed out the primary score criterion, answers from then on are going to be competing on the secondary score criterion, so you'd want to list the length in much the same way as you would for ; the challenge has effectively turned into "golf this program while maximising the primary score".)

This doesn't apply when an answer has no readily-available score (e.g. because it's an uncracked submission and the secondary scoring criterion is something that can't be revealed until the answer becomes safe, or a submission whose score may change from run to run).

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .