-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-simplified-code.js
More file actions
40 lines (33 loc) · 1.11 KB
/
Copy pathexample-simplified-code.js
File metadata and controls
40 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Example of simplified code following core principles
function processUserOrder(order) {
// Guard clauses - reduce nesting
if (!order) return null;
if (!order.items || order.items.length === 0)
return null;
if (!order.user || !order.user.verified) return null;
const subtotal = calculateOrderSubtotal(order.items);
const discount = getUserDiscount(order.user);
const total = subtotal - subtotal * discount;
return total;
}
function calculateOrderSubtotal(items) {
return items.reduce((total, item) => {
return total + item.price * item.qty;
}, 0);
}
function getUserDiscount(user) {
// Avoid nested ternaries - use explicit conditions
const PREMIUM_ANNUAL_DISCOUNT = 0.2;
const PREMIUM_MEMBER_DISCOUNT = 0.15;
const PREMIUM_DISCOUNT = 0.1;
const STANDARD_DISCOUNT = 0.05;
if (!user.premium) return STANDARD_DISCOUNT;
if (user.annual) return PREMIUM_ANNUAL_DISCOUNT;
if (user.member) return PREMIUM_MEMBER_DISCOUNT;
return PREMIUM_DISCOUNT;
}
function getStatus(user) {
// Clear naming and simplified logic
const isActive = user.isActive && user.isVerified;
return isActive ? "active" : "inactive";
}