0

I'm wondering if I can upgrade my code. I have the following steps: when a title is not found, a book is not available. When some parts of the metadata of a book are not found, the book is still available, but the requested data should be returned as unknown.

var notAvailableMSG = process.env.notAvailableMSG;
var unknownMSG = process.env.unknownMSG;
var bookInfo = [];

// if title is not found = book is not available.
if(bookInfo.title == '' || bookInfo.title == undefined ){
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  bookInfo.author = notAvailableMSG;
  bookInfo.publisher = notAvailableMSG;
  bookInfo.price = notAvailableMSG;
  bookInfo.currency = notAvailableMSG;
  bookInfo.url = notAvailableMSG;
  bookInfo.releasedate = notAvailableMSG;
  bookInfo.sellable = notAvailableMSG;
}
// Check for other empty values
if(bookInfo.author == '' || bookInfo.author == undefined){
  bookInfo.author = unknownMSG;
}
if(bookInfo.publisher == '' || publisher == undefined){
  bookInfo.publisher = unknownMSG;
}
if(bookInfo.price == '' || bookInfo.price == undefined){
  bookInfo.price = unknownMSG;
}
if(bookInfo.releasedate == '' || bookInfo.releasedate == undefined){
  bookInfo.releasedate = unknownMSG;
}

Can this code be written in a better way to let this piece of code run faster (make it more readable)?

Elvira
  • 1,410
  • 5
  • 23
  • 50
  • 2
    "Running faster" and "more readable" are separate things. And usually you should primarily care about the latter. – H.B. Mar 27 '18 at 08:49
  • 3
    Looks like `var bookInfo = [];` should be `var bookInfo = {};` – Andy Mar 27 '18 at 08:52

4 Answers4

2

I think the easiest way to check if a value is empty is checking if it's falsy. It isn't the fastest code, but in my opinion is easier to read:

if(!bookInfo.author){
  bookInfo.author = unknownMSG;
}
if(!bookInfo.publisher){
  bookInfo.publisher = unknownMSG;
}
if(!bookInfo.price && bookInfo.price !== 0){
  bookInfo.price = unknownMSG;
}
if(!bookInfo.releasedate){
  bookInfo.releasedate = unknownMSG;
}

You can check this post to see when a value is falsy in JS:

Falsy/Truthy JS Table

EDIT

As H.B. said you can use a function to check each key of the book. Also you can use Object.keys if you want to check each key of the object:

['author', 'publisher', 'price', 'releasedate', /* ... */].forEach(key => {
  if (bookInfo[key] === undefined || bookInfo[key] === null || bookInfo[key] === '') {
    bookInfo[key] = unknownMSG;
  }
});
dloeda
  • 1,516
  • 15
  • 23
  • You could also remove those curly braces and have the statement on the same line as it's conditional. Might make it neater. – Andy Mar 27 '18 at 08:57
  • Would not recommend this. If the book is free (0), the price will be unknown. Should just refactor the thing into a function. – H.B. Mar 27 '18 at 08:59
  • I think the same, but there's a discussion over that braces in the big JS companies. It depends on the style guide of the company (Google, Airbnb, etc) – dloeda Mar 27 '18 at 08:59
  • Nice comment @H.B. – dloeda Mar 27 '18 at 09:00
1

Another possible refactoring:

const checkUnknown = prop => {
  if(bookInfo[prop] == '' || bookInfo[prop] == undefined){
    bookInfo[prop] = unknownMSG;
  }
}
checkUnknown('author');
checkUnknown('publisher');
checkUnknown('price');
checkUnknown('releasedate');

More fancy, taking title into account:

const checkUnknown = (prop, customCallback) => {
  if (bookInfo[prop] == '' || bookInfo[prop] == undefined) {
    if (customCallback)
      customCallback();
    else
      bookInfo[prop] = unknownMSG;
  }
}
checkUnknown('author');
checkUnknown('publisher');
checkUnknown('price');
checkUnknown('releasedate');
checkUnknown('title', () => {
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  bookInfo.author = notAvailableMSG;
  bookInfo.publisher = notAvailableMSG;
  bookInfo.price = notAvailableMSG;
  bookInfo.currency = notAvailableMSG;
  bookInfo.url = notAvailableMSG;
  bookInfo.releasedate = notAvailableMSG;
  bookInfo.sellable = notAvailableMSG;
});
H.B.
  • 166,899
  • 29
  • 327
  • 400
1

When I have long lists of properties, I prefer avoiding repeated code or repeated ifs, if possible.

In this kind of scenarios I would go with something like:

var properties = [
  'author',
  'publisher',
  'price',
  'currency',
  'url',
  'releasedate',
  'sellable'
];

if(!bookInfo.title) {
  bookInfo.isbn = isbn;
  bookInfo.title = notAvailableMSG;
  properties.map(function(prop) {
    bookInfo[prop] = notAvailableMSG;
  });
}

// Check for other empty values
properties.map(function(prop) {
  if(!bookInfo[prop]) {
    bookInfo[prop] = unknownMSG;
  }
});
Piero Nicolli
  • 196
  • 1
  • 5
0

Can't you just do this at all in the one if clause?

var notAvailableMSG = process.env.notAvailableMSG;
var unknownMSG = process.env.unknownMSG;
var bookInfo = {};

// if title is not found = book is not available.
if(bookInfo.title == '' || bookInfo.title == undefined ){
  bookInfo.isbn = isbn;
  bookInfo.title = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.author = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.publisher = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.price = ((!notAvailableMSG && bookInfo.price != 0) ? unknownMSG : notAvailableMSG);
  bookInfo.currency = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.url = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.releasedate = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
  bookInfo.sellable = ((!notAvailableMSG) ? unknownMSG : notAvailableMSG);
}
Stephan T.
  • 5,843
  • 3
  • 20
  • 42
  • Thank you for your answer and time. It is indeed possible to do it in one if. However I don' t really like the readability of this one. – Elvira Mar 27 '18 at 10:47