| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Defensive Programming:
Guard Clause Hell:
Nullish Coalescing and Optional Chaining:
const value = obj?.property?.nestedProperty;const value = obj.property ?? 'default';Data Validation and Schema Libraries:
const schema = Joi.object({
x: Joi.string().required()
});
const { error, value } = schema.validate(data);
if (error) throw new Error('Validation failed');TypeScript and Type Safety:
interface Data {
x: string;
}
const data: Data = fetchData();Imagine an API response where you expect an object with a property x.
function processData(response) {
if (response && response.x) {
console.log(response.x);
} else {
console.log('Property x is missing');
}
}function processData(response) {
const x = response?.x ?? 'default value';
console.log(x);
}const schema = Joi.object({
x: Joi.string().required()
});
function processData(response) {
const { error, value } = schema.validate(response);
if (error) {
console.log('Validation failed');
} else {
console.log(value.x);
}
}interface ApiResponse {
x: string;
}
function processData(response: ApiResponse) {
console.log(response.x);
}You have a scenario where BigQuery returns rows as an array or undefined, which can lead to unstable code when accessing properties of the response.
const [response] = await bigquery.runQuery(query);
return response?.processed_at;You can create a wrapper function that ensures the response always has a consistent structure. This function can provide a default object if the response is undefined.
async function runQueryWithDefaults(query) {
const [response] = await bigquery.runQuery(query);
return {
processed_at: response?.processed_at ?? null,
// Add other properties as needed with default values
};
}const result = await runQueryWithDefaults(query);
return result.processed_at;Let's create a full example to demonstrate this concept:
const { BigQuery } = require('@google-cloud/bigquery');
const bigquery = new BigQuery();
async function runQueryWithDefaults(query) {
try {
const [response] = await bigquery.query(query);
return {
processed_at: response?.processed_at ?? null,
// Add other properties as needed with default values
};
} catch (error) {
console.error('Query failed:', error);
return {
processed_at: null,
// Add other properties with default error values
};
}
}
async function main() {
const query = 'SELECT processed_at FROM your_table LIMIT 1';
const result = await runQueryWithDefaults(query);
console.log(result.processed_at);
}
main();| Back | FazBrowse Home | New Git URL |