5 ms·
> Syntactic sugar is usually bad. I disagree with this one. It can be bad, but if it helps syntax get out of the way to reveal the intent of your code, I thin
by skohan 5y ago
> Syntactic sugar is usually bad.
I disagree with this one. It can be bad, but if it helps syntax get out of the way to reveal the intent of your code, I think it's pretty good
- rckrd 5y agoYes, maybe usually was too strong of a word - I think this varies with the language. In Go and JavaScript (where I mostly work), it's usually not that useful. In languages like C# and Java, I've found syntactic sugar to be very useful (e.g. lambda notion)
- silviot 5y agoI'm under the impression that, in Javascript, > async/await is syntactic sugar on top of the promises and provides a way to handle the asynchronous tasks in a synchronous manner. as is stated on this website I just found with one second of googling to support my thesis: https://dmitripavlutin.com/javascript-async-await/ https://dmitripavlutin.com/javascript-async-await/ If that is true, I believe it's incredibly useful syntactic sugar.
- rckrd 5y agoFair enough, there are clearly lots of useful syntactic shortcuts. I'm referring to things like prefix/postfix increment/decrement, object spreading vs. object rest, and ternary expressions. I'd avoid cases where extending the language is done in a non-obvious and ambiguous way.
- dfee 5y agoI love me some spread operators and ternary expressions. …mumbles something about prying those from my cold dead hands.
- skohan 5y agoYeah I think there's a time and a place for ternaries, but when used correctly they're just so concise. Like if you have super long expressions in your ternaries, or you have a bunch of nested/chained ternaries it can be hard to parse, but in a lot of cases it's so nice to get something onto one line instead of needing an if/else
- ensiferum 5y agoYes in c/++ it also allows you to use const. const foo = bar ? 1 : 2; Without ternary you wouldn't be able to use const specifier on foo.
- mjcohen 5y agoLove the ternary operator in gawk. Here's some extreme examples, from a text formatter I wrote over 20 years ago. It is over 6,000 lines of awk, and I haven't used it in over 15 years. Note: These lines should be split, but many of them are not. How do I do that? I read formatdoc, but that did not help. ; # see where text starts; mut = ((substr($0, lf1-length(lf1str), length(lf1str)) != lf1str) \ ? 1 \ : ((substr($0, lf1, 1) == " " ) \ ? (lf1+1) \ : lf1) \ ); # start of string; --------------------------- # return file and line number with optional text; function filineg(ffilename, ffilerec, fltext) { ; # first version always gives a line number, second does not for line 1; return (ffilename \ (ffilerec == "" ? "" : (":" ffilerec)) \ ((fltext == "") \ ? "" \ : (" in \"" fltext "\"") \ )); } ------------------------------------ outstr[2] = (head \ ( (lnum > 1) \ ? (substr(bigsep, 2, lnum-1) " ") \ : (substr(spaces, 1, lnum)) \ ) \ (header2==""?" ":header2) null("output space if null - 5-20-98") \ substr(bigsep, 1, \ ltocout-lhead-lpn-1 \ -(lheader-lheadchop)-lnum \ -((null_loc > 0) \ ? lnullstring \ : 0 \ ) \ ) " " pn); # parens make it line up; ---------------------- function telldepth(deptxt, dep1, dep2) { ; # change depth from dep1 to dep2; ; # good luck figuring this out in a month!; ; # tell how depth changes; recnumprint("{" deptxt " " \ ((dep1=="") \ ? ((dep2=="") \ ? ("leaves depth as is") \ : ("sets depth " dep2)\ ) \ : ("changes depth " dep1 " to " dep2) \ ) " in " filine() "}"); }
- mastersummoner 5y agoI have to say, I really love the back and forth between you and the various posters in this thread. Lots of good discussion and points being made, and reasonable compromises being agreed on - or even polite disagreement with thoughtful explanations why.
- ajuc 5y agoLambda isn't syntactic sugar, it's a language feature. In Java it's kind of a syntactic sugar (lambda is a shortcut for an anonymous class with 1 method) because of historic reasons, but I'd prefer if it wasn't the case TBH. It was always a dirty hack.
- ithinkso 5y agoI'm actually on the opposite side of this. I know HN is more web-focused so embedded isn't as much represented but I'm scared every time I'm trying to learn Javascript or even Rust. Syntactic sugar complicates code for me, it may help to reveal the intent what the code is doing but I *have to* know how the code is doing it. Of course you can just learn what the compiler will do in every of those instances but that's a lot to learn and it increases exponentially. On the other hand in C you have assignments, conditionals, loops and function calls - nothing else. In the end the code will do the same thing but I'm willing to spent few more keystrokes and maybe an added comment just for the peace of mind of knowing what is actually happening. (One may say that the assembler output is still a blackbox but the general structure of the computation will remain intact) It may be outdated, I agree, but it's just something I can't shake off
- 0xb0565e487 5y agoIt complicates code until it makes it easier, sometimes.
- mastersummoner 5y agoAny thoughts on Ruby? As someone who's worked with it a great deal of the time, I find it extremely simple and concise to write in.
- ithinkso 5y agoI haven't tried Ruby at all to be honest. If I need to do some quick'n'dirty scratchpad calculations on x86 I'm using python but I find myself still using 'C-like' approach instead of idiomatic python. I.e. even 'for x in X:' gives me heebie-jeebies for some unexplained reason (though I use it). I think it's because I've been conditioned for so many years to think about data/code as just that, data in memory. So every time I need to access something I'm thinking *where* it is first, not about getting it - that's secondary and, paradoxically, unimportant. I'll use it in the next stage/line but by then I already have it (I don't know if that makes any sense at all). It makes no sense to do it most of the time in most circumstances, and I think downvotes I'm getting represent that (though I'm not preaching it or anything, just commenting about my point of view), but if you have a hard deadline of X cycles you think about code differently.
- taneq 5y agoI see it as a language/api/environment smell, but also sometimes pragmatic. A good example would be the old MFC - somewhere between 'not ergonomic' and 'ungodly mess' if you were using it manually, but paired with VC++ 5.0 it was pretty good for bashing out general business and productivity apps.
- jbverschoor 5y agoC is just syntax sugar on top of asm. We can go on and on. Syntax sugar is a compiler-level of abstraction. It’s good
- tonymet 5y agoCan you share the ones you like? I agree with OP that usually “sugar” hides hideous performance issues like allocs