寻求空值/Null/Undefined检查优化方案及图书信息代码升级建议
Hey there! Let's refine your book info logic to match your business requirements, plus we'll use cleaner, more efficient null/undefined checks along the way.
1. Core Business Logic Breakdown
First, let's lock in the rules we need to enforce clearly:
- If the book title is missing (empty string,
undefined, ornull), mark the book as unavailable and use yournotAvailableMSG - If the title exists but other metadata fields are missing, keep the book marked as available and fill missing fields with
unknownMSG
2. Efficient Null/Undefined/Empty Checks
Instead of writing repetitive == '' || == undefined checks, here are cleaner, more concise alternatives:
- Falsy Check (
!): A quick way to catch all falsy values (empty string,undefined,null, 0,false, etc.) – perfect for the title check since we want to block any "missing" title scenario - Nullish Coalescing Operator (
??): Returns the right-hand value only if the left isnullorundefined(ignores empty strings, 0, etc.) – ideal for metadata fields where empty strings might be intentional, but missing data should default tounknownMSG - Optional Chaining (
?.): Safely access nested properties without throwing errors if a parent value isundefined/null(useful if your book data comes from nested API responses)
3. Refactored Code Snippet
Here's how to apply these to your code, with clearer structure and logic:
const notAvailableMSG = process.env.notAvailableMSG; const unknownMSG = process.env.unknownMSG; let bookInfo = {}; // Switched to object (arrays are for lists, objects fit key-value book metadata better) // Check if title is missing (covers empty string, undefined, null) if (!bookInfo.title) { // Mark book as unavailable bookInfo.isAvailable = false; bookInfo.status = notAvailableMSG; bookInfo.isbn = isbn; // Assuming `isbn` is defined elsewhere in your code } else { // Book is available – fill missing metadata with unknownMSG bookInfo.isAvailable = true; // Example metadata fields – adjust to match your actual data bookInfo.author = bookInfo.author ?? unknownMSG; bookInfo.publisher = bookInfo.publisher ?? unknownMSG; bookInfo.publishDate = bookInfo.publishDate ?? unknownMSG; bookInfo.isbn = isbn; }
Quick Refactor Notes:
- Changed
bookInfofrom an array to an object – this makes it easier to manage key-value metadata like title, author, etc. - Added an explicit
isAvailableflag to make the book's status clear at a glance (no need to parse messages to determine availability) - If you want to treat empty strings as "missing" metadata too, replace
??with||(e.g.,bookInfo.author || unknownMSG) – just note that||will replace all falsy values, including 0 orfalse
4. Bonus: DRY Helper Function
If you have lots of metadata fields to check, a small helper function will keep your code clean and reusable:
function getMetadataValue(value) { return value ?? unknownMSG; } // Use it like this for each field: bookInfo.author = getMetadataValue(bookInfo.author); bookInfo.publisher = getMetadataValue(bookInfo.publisher); bookInfo.publishDate = getMetadataValue(bookInfo.publishDate);
内容的提问来源于stack exchange,提问作者Elvira
相关产品推荐
相关产品推荐

