6 ms·
I haven't made it through the whole thing yet, but I do want to register a vote in favor of using Lines Of Code count as a rough measure of program complexity.
by ufmace 7y ago
I haven't made it through the whole thing yet, but I do want to register a vote in favor of using Lines Of Code count as a rough measure of program complexity. I think it's a perfectly valid things to do, provided that it isn't used as an evaluation metric and nobody is gaming it, and everyone is a reasonably good programmer, not doing crazy things like trying to stuff a massive algorithm onto one line to be clever, or copy-pasting the same code in 20 places because they don't understand functions and classes.
- chimpburger 7y agoWhen comparing different languages it becomes more of a measure of the relative abstraction levels of languages. The exact same program will have hundreds (or thousands) of times more LOC in Assembly than in Python.
- applecrazy 7y agoNot only that, the LOC heuristic depends on the verbosity of the language. Java, for example, is more verbose, than say, Python, even though you’re close in abstraction level.
- gpm 7y agoAnd just syntax decision. if (x) { y } else { z } if x: y else: z Assuming x and y are 1 line but long enough expressions you don't want to use a trinary operator (available in both) the python is 2/3rds the length of the c because of bracketing. Obviously cherry picked, but I bet these differences add up.
- saagarjha 7y agoOf course, you can omit the braces if there’s a single statement (though I personally never do this).
- gpm 7y agoSure, and you can put the else on the same line as the end of the if block even without doing that. Or you could make the difference bigger by putting the opening brace on a newline. I wrote the style of C that I actually write, but it's probably not fair for me to label it as a property of just the language.
- saagarjha 7y agoI think it’s a fair point to bring up given that many people actually do write their code that way.
- hart_russell 7y agoI'd like to hear others' opinions: There's a guy at my work who loves to use doubly, triply, quadruple-ly nested ternary operators. I always find them super hard to read. Am I just a dunce, or do I have a point in thinking it's unnecessarily terse.
- nateberkopec 7y agoI love ternary operators, but nesting them is a recipe for turning your code into an unreadable mess.
- WalterBright 7y agoFormat them like you would if-else, then they're easy to read: x = y ? 1 : 2;
- 1f60c 7y agoThe latter, IMO. Something like Screen.oled ? "black" : "rgb(21,21,21)" is easy enough to read. Beyond that, if statements win for me.
- giggzy 7y agoI prefer less terse code for readability too. LOC is a decent approximation for complexity but it breaks down when people optimize for it. It's a tricky problem to decide which idioms are most expressive/readable/maintainable as it's has a group dynamic. My rule of thumb, if I feel I've written clever code it's time to rethink approach.
- lyjackal 7y agoUsually it's awful. I wish JavaScript had if else expressions. Sometimes it can help to break up the lines with indents if you need a single expression: var myvar = foo < bar ? bar : bar < baz ? baz : 0
- _jsdw 7y agoI settled on this a while ago which I think is perfectly readable: const myvar = foo < bar ? bar : bar < baz ? baz : lark < 10 ? lark : 0
- ben_jones 7y agoIMO a good start would be to create a metric based on a combination of: * Lines of code * # of keywords/operators * # of non-standard keywords/operators (i.e. 'go' versus 'if') * Use of std. library vs. 3rd party libraries * Depth of call-stack, i.e. is it passing through 3 parent classes
- deleted 7y ago[deleted]
- kitd 7y agoThe traditional method of measuring code complexity is Cyclomatic Complexity [1] which, roughly speaking, measures the number of branch points in the code. [1] https://en.wikipedia.org/wiki/Cyclomatic_complexity https://en.wikipedia.org/wiki/Cyclomatic_complexity