[英]Should function name also reflect actions of functions called as callbacks on promise.then()?
在為函數選擇名稱時,我們還應考慮該函數設置的回調函數在做什么?
這個問題有些棘手,但是在此示例中,一切應該很簡單:
function problematicName() {
const elementsCountRequest = new Request(/*...*/);
fetch(elementsCountRequest)
.then(getTextFromResponse)
.then(setElementsCount);
}
function setElementsCount(newCount) {
// function updates DOM here
}
在這里,我們有一個problematicName
ProblemNameName函數,它創建請求,從服務器獲取數據並設置回調setElementsCount
。 應該如何命名函數problematicName
?
getElementsCount
因為它基本上是從服務器獲取計數並設置回調的 updateElementCount
因為我們還應考慮回調(由problematicName
設置)。 因此,當我們考慮通過調用problematicName
進行的所有更改時, setElementsCount
也進行了更改 problematicName
一次執行太多操作,此函數沒有好名字 應該選擇什么名字? 最佳做法是什么?
我認為mongodb在這種情況下具有良好的命名約定,它們具有update
和find
類的方法,並且兩者的混合都是findAndUpdate
,因此在您的情況下,可以將其命名為getAndUpdateElementsCount
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.