'Alternative to using magic string values like "N/A"
I have special values that I extracted into a constant to signify special cases like "not found" or "does not exist". I thought this would be pretty clean, but now I am regretting my decision because I have to import the constant everywhere, and I need to keep it synced across frontend and backend. What is the best way to redesign the below?
Backend:
const ISBN_NOT_FOUND = 'N/A';
...
function populateBookDetails(book) {
book.author = ...
book.description = ...
if(canFindISBN(book)) book.isbn = findISBN(book);
else book.isbn = ISBN_NOT_FOUND
}
//for frontend to use
res.send(populateBookDetails(book))
Frontend:
const ISBN_NOT_FOUND = 'N/A';
...
function getISBNTruncated() {
if(book.ISBN == ISBN_NOT_FOUND) return 'not found';
else return ...
}
function getISBNFull() {
if(book.ISBN == ISBN_NOT_FOUND) return 'The ISBN for this book could not be found';
else return ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
