A value constant is defined and it is checking if rowData.projects is undefined. If it is undefined, it will return this part rowData.name:(rowData.projects.length>0?rowData.projects[0].name OR it will return ' '.
I am not understanding how to interpret this part:
Close⌠it is actually TWO ternary operations. If rowData.projects is undefined it will return rowData.name OR it will go into another ternary operator where it checks rowData.projects.length > 0. If THAT is true, it will return rowData.projects[0].name otherwise it will return an empty string.
This second ternary operation is the FALSE part of the first ternary operation.
Yeah can be confusing. Hopefully that makes sense.
Edit: Net effect is that it returns rowData.name OR rowData.projects[0].name but only if projects is greater than zero.
In my opinion programmers often use too much whitespace (spaces and blank lines). In this case I think there is not enough. The following is the likely way I would format that.
Normally I have no trouble with ternarys but with the original code, I had trouble understanding at first glance what was happening with the name. Because there are several different things happening with it, that warranted making things simpler and easier to understand.
There might be a pleasant middle-ground somewhere between those if statements and the one-liner ternary, but will it be as easy to understand when we come back to the code in six months time to refactor or debug?
Frequently clever doesnât mean complex
I was going to take things further to investigate simplification by using the optional chaining operator instead. But I can only procrastinate on other things for so long.
It depends on the experience of the programmer. A programmer experienced with the ternary operator would likely want to convert code using exclusively if statements into code that uses the ternary operator.
It reminds me of an experience I had many years ago. A beginner COBOL programmer proudly said he was taught to never use not in an if condition. I indicated it is not a problem. He looked at me as if I was being stupid.
The ternary operator originated in C. It is not necessary but I think it can be useful. There are many other features exclusive to JavaScript that I think make JavaScript unnecessarily cryptic. Many JavaScript programmers seem to think it is necessary to be cryptic.
Carrying on from my if statement simplification, letâs get rid of most of the if statements.
function getProjectName(projects, name) {
if (projects === undefined) {
return name;
}
if (projects.length === 0) {
return "";
}
return projects[0].name;
}
function projectNameTemplate(rowData) {
const value = getProjectName(rowData.projects, rowData.name);
return `<span>${value}</span>`;
}
Iâve used some basic tests at the end of the code to help exercise the getProjectName function, and to help guide me to things I missed out when restructuring the code.
const rowData = [
{
projects: [],
name: "not used"
},
{
projects: undefined,
name: "name1"
},
{
projects: [{
name: "name2"
}],
name: "not used"
}
];
{
const actual = getProjectName(rowData[0]);
const expected = "";
console.assert(actual === expected, `Zero projects should give empty string`);
}
{
const actual = getProjectName(rowData[1]);
const expected = rowData[1].name;
console.assert(actual === expected, `No project should give default name`);
}
{
const actual = getProjectName(rowData[2]);
const expected = rowData[2].projects[0].name;
console.assert(actual === expected, `A project should give the project name`);
}
I can now use parameter context matching, and pull up the empty string response to the top of the function using ?. as an optional chaining operator. After that a simple ternary gives us one name or the other.
function getProjectName({projects, name}) {
if (projects?.length === 0) {
return "";
}
return projects ? projects[0].name : name;
}
function projectNameTemplate(rowData) {
const name = getProjectName(rowData);
return `<span>${name}</span>`;
}
That is my preferred simplification of things, when ?. is allowed to be used. It helps us get the ââ case out of the way so that we can focus on the names being returned.