What determines a line of code?

70 views
Skip to first unread message

SpongeJr

unread,
Jun 20, 2015, 10:40:03 PM6/20/15
to khan-academy-javas...@googlegroups.com
I've done a little Googling and spec-reading and stuff and still couldn't quite come up with an answer I'm 100% confident in.

Can anyone advise me as to what determines a "line of code" in ECMAScript/JavaScript?
What is the easiest way to state the exact rules? (how many lines of code does for(var i = 0; i < 5; i++) {} count for?)

and I'd like to be well-informed on the subject.

Also having trouble finding a good utility on the web for counting JS LOC. Figured something like that would exist, but couldn't find it.
Did find utilities like these but that's not what I want: http://cloc.sourceforge.net/ and https://www.npmjs.com/package/sloc



Bob Lyon

unread,
Jun 21, 2015, 3:59:07 PM6/21/15
to khan-academy-javas...@googlegroups.com
Line count is an absurd concept with  Lisp-like languages and Javascript is essentially Lisp with a human readable syntax.  But whatever the metric, it needs to be precisely and unambiguously stated in the contest rules.

IMHO, for the purposes of a coding competition, if you want to measure the "token complexity" of a Javascript program, then sum the total number of the following three tokens: open (or close) curly braces, semicolons and commas.  I hope the reasons for counting curly braces and semicolons are obvious to anyone that cares.  Counting commas is necessary to counteract those of us who would game the system by using using commas instead of semicolons.

Then outlaw the use of eval and Function (with a capital F).  Maybe labels should also be outlawed.

So while normal people would agree that  for (i = 0; i < a.length; i++) {  is one line of code, it gets a complexity count of 3.  The obvious translation of the for loop into a while loop also produces a complexity count of 3.

Element118

unread,
Jun 24, 2015, 8:12:56 PM6/24/15
to khan-academy-javas...@googlegroups.com
How about ternary operators? That probably should have some complexity as a ternary operator is "equivalent" to an if statement. I written some Java (not Javascript) code before that did a lot of logic with ternary operators and I only had one semicolon at the end. How about chained assignments?

Element118

unread,
Jun 24, 2015, 8:42:33 PM6/24/15
to khan-academy-javas...@googlegroups.com
Also, I looked through some of the submissions:
||, && can be used to reduce lines of code, as well as the for..in.. loop.
Maybe "Best program in 400 characters (excluding spaces) would be better" and all user-defined variables and functions count as 1 character.

primenum...@gmail.com

unread,
Oct 11, 2015, 11:04:27 AM10/11/15
to Khan Academy Javascript Technical Q&A
It's arbitrary. The best way is to assign point values to everything, for example: for/while is 4 points, brackets and parens are 1.5 points each, variables and functions are 1 point each, and so on.
Reply all
Reply to author
Forward
0 new messages