自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

六個避免過度使用 IF 語句的技巧

開發(fā) 前端
最近,我在重構(gòu)我之前的代碼時,我發(fā)現(xiàn)早期的代碼使用了太多的 if 語句,達到了我以前從未見過的程度。這就是為什么我認為分享這些可以幫助我們避免使用過多 if 語句的簡單技巧很重要。

最近,我在重構(gòu)我之前的代碼時,我發(fā)現(xiàn)早期的代碼使用了太多的 if 語句,達到了我以前從未見過的程度。這就是為什么我認為分享這些可以幫助我們避免使用過多 if 語句的簡單技巧很重要。

接下來,我將與你一起來分享這 6 種避免過度使用 if 的方法,這不是抵制使用if的偏執(zhí),而是換一種方式來思考我們的編程思路。

1. 三元條件運算符

示例 1:

帶有 if 的代碼:

function customerValidation(customer) {
if (!customer.email) {
return error('email is require')
} else if (!customer.login) {
return error('login is required')
} else if (!customer.name) {
return error('name is required')
} else {
return customer
}
}

重構(gòu)代碼:

const customerValidation = customer =>
!customer.email ? error('email is required')
: !customer.login ? error('login is required')
: !customer.name ? error('name is required')
: customer

示例 2:

帶有 if 的代碼:

function getEventTarget(evt) {
if (!evt) {
evt = window.event;
}
if (!evt) {
return;
}
const target;
if (evt.target) {
target = evt.target;
} else {
target = evt.srcElement;
}
return target;
}

重構(gòu)代碼:    

function getEventTarget(evt) {
evt = evt || window.event;
return evt && (evt.target || evt.srcElement);
}

2.短路邏輯運算符

示例 1:

帶有 if 的代碼:

const isOnline = true;
const makeReservation= ()=>{};
const user = {
name:'Damian',
age:32,
dni:33295000
};


if (isOnline){
makeReservation(user);
}

重構(gòu)代碼:

const isOnline = true;
const makeReservation= ()=>{};
const user = {
name:'Damian',
age:32,
dni:33295000
};


isOnline&&makeReservation(user);

示例 2:

帶有 if 的代碼:

const active = true;
const loan = {
uuid:123456,
ammount:10,
requestedBy:'rick'
};


const sendMoney = ()=>{};


if (active&&loan){
sendMoney();
}

重構(gòu)代碼:

const active = true;
const loan = {
uuid:123456,
ammount:10,
requestedBy:'rick'
};


const sendMoney = ()=>{};


active && loan && sendMoney();

3.功能委托

示例 1:

帶有 if 的代碼:

function itemDropped(item, location) {
if (!item) {
return false;
} else if (outOfBounds(location) {
var error = outOfBounds;
server.notify(item, error);
items.resetAll();
return false;
} else {
animateCanvas();
server.notify(item, location);
return true;
}
}

重構(gòu)代碼:

function itemDropped(item, location) {
const dropOut = function() {
server.notify(item, outOfBounds);
items.resetAll();
return false;
}


const dropIn = function() {
server.notify(item, location);
animateCanvas();
return true;
}


return !!item && (outOfBounds(location) ? dropOut() : dropIn());
}

4. 非分支策略

示例 1:

帶開關(guān)的代碼:

switch(breed){
case 'border':
return 'Border Collies are good boys and girls.';
break;
case 'pitbull':
return 'Pit Bulls are good boys and girls.';
break;
case 'german':
return 'German Shepherds are good boys and girls.';
break;
default:
return 'Im default'
}

重構(gòu)代碼:

const dogSwitch = (breed) =>({
"border": "Border Collies are good boys and girls.",
"pitbull": "Pit Bulls are good boys and girls.",
"german": "German Shepherds are good boys and girls.",
})[breed]||'Im the default';


dogSwitch("border xxx")

5.作為數(shù)據(jù)的函數(shù)

我們知道在 JS 中函數(shù)是第一類,所以使用它我們可以將代碼拆分成一個函數(shù)對象。

帶有 if 的代碼:

const calc = {
run: function(op, n1, n2) {
const result;
if (op == "add") {
result = n1 + n2;
} else if (op == "sub" ) {
result = n1 - n2;
} else if (op == "mult" ) {
result = n1 * n2;
} else if (op == "div" ) {
result = n1 / n2;
}
return result;
}
}


calc.run("sub", 5, 3); //2

重構(gòu)代碼:

const calc = {
add : function(a,b) {
return a + b;
},
sub : function(a,b) {
return a - b;
},
mult : function(a,b) {
return a * b;
},
div : function(a,b) {
return a / b;
},
run: function(fn, a, b) {
return fn && fn(a,b);
}
}


calc.run(calc.mult, 7, 4); //28

6.多態(tài)性

多態(tài)性是一個對象具有多種形式的能力,OOP 中多態(tài)性最常見的用法是使用父類引用來引用子類對象。

帶有 if 的代碼:

const bob = {
name:'Bob',
salary:1000,
job_type:'DEVELOPER'
};


const mary = {
name:'Mary',
salary:1000,
job_type:'QA'
};


const calc = (person) =>{


if (people.job_type==='DEVELOPER')
return person.salary+9000*0.10;


if (people.job_type==='QA')
return person.salary+1000*0.60;
}


console.log('Salary',calc(bob));
console.log('Salary',calc(mary));

重構(gòu)代碼:

const qaSalary  = (base) => base+9000*0.10;
const devSalary = (base) => base+1000*0.60;


//Add function to the object.
const bob = {
name:'Bob',
salary:1000,
job_type:'DEVELOPER',
calc: devSalary
};


const mary = {
name:'Mary',
salary:1000,
job_type:'QA',
calc: qaSalary
};


console.log('Salary',bob.calc(bob.salary));
console.log('Salary',mary.calc(mary.salary));
責任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

2024-01-30 08:43:26

IF 語句JavaScripJS

2022-06-28 10:17:23

安全職位首席信息安全官

2021-11-15 09:24:37

MSSP勒索軟件安全服務

2021-07-16 10:27:07

ITIT領(lǐng)導IT管理

2022-11-30 15:01:11

React技巧代碼

2018-03-17 09:04:35

2022-04-29 17:03:37

WordPress開發(fā)者網(wǎng)站安全

2024-03-06 10:50:30

云計算云實例云提供商

2016-12-15 09:53:07

自學編程技巧

2023-05-09 07:09:02

2021-10-09 10:00:52

遠程招聘技巧招聘

2023-10-10 18:24:46

PostgreSQL性能RDBMS

2015-07-30 14:43:04

導航欄iOS開發(fā)

2024-07-15 08:10:57

2023-01-29 07:45:06

DevOps

2011-08-15 14:52:41

MySQL

2023-12-07 16:57:42

2022-09-30 13:32:25

云原生云原生開發(fā)

2024-01-02 18:01:12

SQLSELECT查詢

2013-09-12 11:40:22

VDI部署VDI
點贊
收藏

51CTO技術(shù)棧公眾號