I Hate Clever Javascript Programmers [modified]
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
Finally I see what it is doing after 10 mins of staring at the code.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
There is nothing clever here; the code was obfuscated with a tool; it was intended to be hard to understand; the original code is probably very readable.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
Try rewriting it using if ... else ... instead of ? :, this might increase readability to the point where you'll understand it. But don't get your hopes high. This looks like some code intentionally obfuscated, potentially hand-crafted, intentionally written so it's not comprehensible, in order to prevent exactly what you are trying to do.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
-
If I read this correctly and my understanding of JavaScript precedence is correct, I think this is an ugly version of a big else-if statement. Something like this:
if (aw == null) {
return a0 === "!=";
} else if (a0 === "=") {
return a2 === aX;
} else if (a0 === "*=") {
return a2.indexOf(aX) >= 0;
} else if (a0 === "~=") {
return (" " + a2 + " ").indexOf(aX) >= 0;
} else if (!aX) {
return a2 && aW !== false;
} else if (a0 === "!=") {
return a2 !== aX;
} else if (a0 === "^=") {
return a2.indexOf(aX) === 0;
} else if (a0 === "$=") {
return a2.substr(a2.length - aX.length) === aX;
} else if (a0 === "|=") {
return a2 === aX || a2.substr(0, aX.length + 1) === aX + "-";
}return false;
which you could also write as
return (aW == null) ? a0 === "!="
: (a0 === "=") ? a2 === aX
: (a0 === "*=") ? a2.indexOf(aX) >= 0
: (a0 === "~=") ? (" " + a2 + " ").indexOf(aX) >= 0
: (!aX) ? a2 && aW !== false
: (a0 === "!=") ? a2 !== aX
: (a0 === "^=") ? a2.indexOf(aX) === 0
: (a0 === "$=") ? a2.substr(a2.length - aX.length) === aX
: (a0 === "|=") ? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-"
: false;where this shows the IF on the left and the RETURN THIS on the left of the "?" on each line. Ugly. Un-maintainable. Yuck.
I actually prefer the second version from a readability point of view (now that you've reformatted so it makes sense). Stick a comment at the top to say what it's doing and it wouldn't be so bad. The variable names and dynamic typing I'll admit is horrendous for readability though.
-
Personal use...
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997Either way... I'm glad I'm not maintaining that guy's stuff. Personal or Professional, a good dev is not going to cut corners in the quality of their code in either. Or am I the only anal retentive coder out there.
I wasn't, now I am, then I won't be anymore.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
The uncompressed source is on GitHub. https://github.com/mjijackson/shadowbox[^]
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
One line of code thing aside, this is a pretty idiomatic use of the ternary operator. What's so clever about it?
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
-
Like others have said ... that looks to me like Obfuscated code - emitted by a tool of some kind. If I had a developer working for me that wrote code like that I'd fire his ass. -Max
I agree. Although inline IFs are useful, that code looks as bad as it would look in the more traditional IF { } block form. I'm not convinced it's been generated by an obfuscator though.
-
There is nothing clever here; the code was obfuscated with a tool; it was intended to be hard to understand; the original code is probably very readable.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
As others have said there is nothing demented or intentionally spiteful about this at all. They simply used an optimization tool (minifier), which removes all white space and uses the minimum amount of characters for variables. It compresses JavaScript quite a bit for downloading. It is a highly common, and very recommended, practice. The actual source code with all comments and descriptive variable names is on the project site.
-
If you work your head around inline IFs you'll find them really useful. I use them in C# all the time!!
I agree -- they help promote a nice functional style. But this example is over the top.
-
If you work your head around inline IFs you'll find them really useful. I use them in C# all the time!!
I'm not saying I don't understand this code. I'm saying this code is ridiculous. And it's called a ternary comparison, not "nested if's".
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997 -
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
Anytime I see variables like a0, a1, a2 etc. it's probably because the code has been run through a compression or obfuscation utility to make it smaller and less likely to be swiped. I'm guessing that's what's happened here. [edit: and if I'd read the responses so far I'd have seen that my reply was redundant]. Cheers, Drew.
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
Yes. my master. :suss:
I'd blame it on the Brain farts.. But let's be honest, it really is more like a Methane factory between my ears some days then it is anything else...
-----
"The conversations he was having with himself were becoming ominous."-.. On the radio... -
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
-
I'm trying to decipher the inner workings of ShadowBox (a lightbox variant), and I found this (except when I found it, it was all on one line of code:
return (aW == null) ? a0 === "!=" : a0 === "="
? a2 === aX : a0 === "*="
? a2.indexOf(aX) >= 0 : a0 === "~="
? (" " + a2 + " ").indexOf(aX) >= 0 : !aX
? a2 && aW !== false : a0 === "!="
? a2 !== aX : a0 === "^="
? a2.indexOf(aX) === 0 : a0 === "$="
? a2.substr(a2.length - aX.length) === aX : a0 === "|="
? a2 === aX || a2.substr(0, aX.length + 1) === aX + "-" : false;Before anyone else mentions it, I *know* what a ternary comparison is and I use them all the time (and was doing so before many of you were even born), but I don't nest them like this. THAT is the shameful nature of the code. So, please stop trying to educate me as to the ways of the force.
".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997modified on Thursday, April 28, 2011 3:50 PM
I 100% agree with you. Good code is readable. Code that is difficult to read leaves space for difficulty to find bugs which cost the company money. Maybee its a form of insecurity these guys have. E.g. If I make my code impossible to read, they'll have to keep me employed.
"Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>
modified on Wednesday, May 11, 2011 6:54 AM