This is a random piece of javascript code… ( javascript ) ✂
// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
var result = "";
if (prefix && isIE) {
// IE/Windows way of handling namespaces
result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
} else {
// the namespace versions of this method
// (getElementsByTagNameNS()) operate
// differently in Safari and Mozilla, but both
// return value with just local name, provided
// there aren't conflicts with non-namespace element
// names
result = parentElem.getElementsByTagName(local)[index];
}
if (result) {
// get text, accounting for possible
// whitespace (carriage return) text nodes
if (result.childNodes.length > 1) {
return result.childNodes[1].nodeValue;
} else {
return result.firstChild.nodeValue;
}
} else {
return "n/a";
}
}
1// retrieve text of an XML document element, including 2// elements using namespaces 3function getElementTextNS(prefix, local, parentElem, index){ 4var result = ""; 5if(prefix && isIE){ 6// IE/Windows way of handling namespaces 7 result = parentElem.getElementsByTagName(prefix + ":" + local)[index]; 8}else{ 9// the namespace versions of this method 10// (getElementsByTagNameNS()) operate 11// differently in Safari and Mozilla, but both 12// return value with just local name, provided 13// there aren't conflicts with non-namespace element 14// names 15 result = parentElem.getElementsByTagName(local)[index]; 16} 17if(result){ 18// get text, accounting for possible 19// whitespace (carriage return) text nodes 20if(result.childNodes.length > 1){ 21return result.childNodes[1].nodeValue; 22}else{ 23return result.firstChild.nodeValue; 24} 25}else{ 26return"n/a"; 27} 28}
but is the following not what you'd rather have in your file for clarity: ( javascript ) ✂
function getElementTextNS(prefix, local, parentElem, index) {
var result = ""
if (prefix && isIE)
result = parentElem.getElementsByTagName(prefix + ":" + local)[index]
else
result = parentElem.getElementsByTagName(local)[index]
if (result)
if (result.childNodes.length > 1)
return result.childNodes[1].nodeValue
else
return result.firstChild.nodeValue
else
return "n/a"
}
Three points being made here: 1) Comments can often as not muddle up the readability of code which was already fairly readable; 2) Braces around one-line code segments can make code appear visually complicated when something simple is going on (though this is a bias from someone who likes Lispy-looking code); 3) Semicolons in JavaScript is just unnecessary visual noise.