SlightlyLoony
Tera Contributor
Options
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
04-07-2011
05:30 AM
Our confident — and definitely different — girl at right is doing just fine. Sometimes doing things differently in your JavaScript code can be a good thing, too. Consider this perfectly ordinary code to log all possible combinations of two variables whose values can be between 1 and 10:
for (var i = 1; i <= 10; i++)
for (var j = 1; j <= 10; j++)
gs.log('' + i + ', ' + j);
If you're an experienced developer on Service-now's platform (and I'm hiring, by the way!), then this is the sort of thing you could do after four glasses of wine, during a 9.5 earthquake, while a fire alarm is blaring, with one hand tied behind your back, and while your cat is clawing his way up your leg. It's just a simple nested loop. But could you do it with fewer lines of code?
The first thing I would say to someone asking this question is this: it's much more important for your code to be clear (especially to other developers) than it is for your code to be short. To the extent you can do both, though, then shorter is generally better, as there's less to absorb when trying to understand what's going on.
Now here's another version in just two lines of code, using an operator you may not be familiar with:
for (var i = 1, j = 1; i <= 10; (j < 10) ? j++ : (j = 1, i++))
gs.log('' + i + ', ' + j);
This code takes advantage of the comma (',') operator, something rarely seen in the wild. A comma operator can be used anywhere an expression is expected. You use it to put multiple expressions (comma-separated) where one was expected. The comma-separated expressions are executed left-to-right, and the value returned is that of the last one executed.
Consider the initializer clause of the for() loop above:
var i = 1, j = 1
When executed, this will create two variables ('i' and 'j') that are local to the for() loop. Just what we need! Now consider the update clause in the same for() loop:
(j < 10) ? j++ : (j = 1, i++)
This clause is executed each time the for() loop iterates to the next item. The ternary operator ('?') first makes a decision about whether 'j' is less than 10. If it is, then it simply increments 'j'. If it isn't, then this term is executed:
(j = 1, i++)
Here we're doing two things when 'j' is 10 or more: we're setting 'j' to 1, and incrementing 'i'. We have to put these in parentheses so the JavaScript compiler understands that both things are part of the ternary clause. There's no use of the returned value, so all the comma is doing for us here is letting us do two things instead of one — but that's all it takes to let us eliminate the inner loop!
I've never seen any other useful thing for the comma operator to do, and most of the time (as in my example), it adds complexity that you may well want to do without. But occasionally the comma operator will give you a clean, elegant way to accomplish something that would otherwise be difficult or ugly, so it's a good tool to have in your developer's toolkit — even if you don't use it very often!
1 Comment
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.