grassJava
grassJava

Reputation: 43

Javascript Node.js Simplifying IF ELSE

I already read the shorthand examples in stackoverflow, but still it seems I can't find the answer to my question.

How do you simplify this "if else" when checking for null or empty?

var firstName = object.firstName || '',
lastName = object.lastName || '';
if (firstName === lastName) {
    return firstName;
} else {
    if (!lastName) {
        return firstName;
    } else if (!firstName) {
        return lastName;
    } else {
        return lastName + ", " + firstName;
    }
}

Upvotes: 1

Views: 749

Answers (5)

user663031
user663031

Reputation:

There's nothing wrong with your code.

My personal style is to avoid vertical sprawl by putting single-statement if blocks on the same line. You can also omit the else since you are returning:

if (firstName === lastName) return firstName;
if (!lastName) return firstName;
if (!firstName) return lastName;
return lastName + ", " + firstName;

You could also combine the cases where you are returning firstName:

if (firstName === lastName || !lastName) return firstName;
if (!firstName) return lastName;
return lastName + ", " + firstName;

Or, you could chain ternary operators, using newlines to your preference:

return firstName === lastName || !lastName ? firstName : 
  !firstName ? lastName : 
  lastName + ", " + firstName;

Your intent might be better expressed by handling the common case first, after which you can simply return whichever of lastName or firstName is present:

if (firstName && lastName) return lastName + ", " + firstName;
return lastName || firstName;

Upvotes: 4

loxxy
loxxy

Reputation: 13151

I think, one of the conditions can be performed during the assignment itself.

Doesn't this look simpler:

var firstName = object.firstName,
    lastName = object.lastName || firstName;

if(!firstName || firstName === lastName) return lastName;

return lastName + "," + firstName;

Upvotes: 0

deadbeef404
deadbeef404

Reputation: 623

I like torazaburo's second answer, but I'd probably keep the brackets in as that's the style I'm used to. You were "told that the code was too long", which is probably not helpful advice, at the least it's a bit cryptic. As a JS developer, I'd be perfectly happy see the following lines of code (assuming your business logic is sound):

if (firstName === lastName || !lastName) {
  return firstName;
} else if (!firstName) {
  return lastName
}
return lastName + ", " + firstName;

Upvotes: 0

milkandtang
milkandtang

Reputation: 586

You don't need the top-level else if you're going to be returning from the function; you can just short-circuit and return early, otherwise this can be simplified a bit and still be pretty readable, I think:

var firstName = object.firstName
var lastName = object.lastName

if (!lastName || firstName === lastName) {
    return firstName
}

return firstName ? [lastName, firstName].join(', ') : lastName

You also may want to have something up-front though if neither is set, like an if (!firstName || !lastName) return 'unknown' or the like; otherwise you're returning undefined which is maybe what you want.

Maybe easier is just using an array in the first place:

var name = []

if (object.lastName) name.push(object.lastName)
if (object.firstName) name.push(object.firstName)

return name.join(', ')

Or you could get wacky with something like that:

var name = ['lastName', 'firstName'].map(function (key) {
   if (object.hasOwnProperty(key)) {
       return object[key]
   }
})

return name.filter(Boolean).join(', ')

But that's way harder to follow the intent than the simple case, I think. There are ways to do this with nested ternaries, but I really find a nested ternary hard to read; to me, it's better to add some lines and be explicit and understandable.

Upvotes: 3

Dave Newton
Dave Newton

Reputation: 160191

I don't see any real reason to "simplify" it from a logical perspective, but you could make it less mountainous by simply returning early. You also have a logic error, depending on your needs.

What if my name is "James James"? (I ask because I knew a guy named that.)

Why not keep everything at a single level? Super-easy to read, you know exactly when and why you're returning, and you can stop looking further in the code if you know your conditions and you see a return?

Roughly:

var firstName = object.firstName || ''
  , lastName  = object.lastName  || ''
  ;

if (firstName === lastName) {
  return firstName;
} 

if (!lastName) {
  return firstName;
}

if (!firstName) {
  return lastName;
}

return lastName + ", " + firstName;

You might be able to get away with this if you don't really want to mess with my friend James James:

if (!firstName || !lastName) {
  return firstName || lastName;
}

return lastName + ", " + firstName;

(I didn't test that, just seems like it might be close.)

Upvotes: 0

Related Questions