admin管理员组文章数量:1026989
For synchronous getter functions, the naming convention is well-defined:
var getFerby = function(){
..
return ferby;
};
However, if the ferby I want is not locally (synchronously) available, a mon method is to handle that situation with a callback:
/**
* Asynchronously gets a ferby and passes it to the callback.
*
* Once the ferby is retrieved, these rules MUST be followed:
* 1) Don't feed it after midnight.
* 2) Don't give it water.
* 3) Don't let it near bright light.
*
* @param {ferbyCallback} callback - The callback function that expects a ferby.
*/
var fooFerby = function(callback){
getFerbyLoader().load(function(ferby){
callback(ferby);
});
};
/**
* The callback for the fooFerby function.
*
* @callback ferbyCallback
* @param ferby The ferby
*/
What is a good naming convention for fooFerby
so that I know by name that it expects a callback?
For synchronous getter functions, the naming convention is well-defined:
var getFerby = function(){
..
return ferby;
};
However, if the ferby I want is not locally (synchronously) available, a mon method is to handle that situation with a callback:
/**
* Asynchronously gets a ferby and passes it to the callback.
*
* Once the ferby is retrieved, these rules MUST be followed:
* 1) Don't feed it after midnight.
* 2) Don't give it water.
* 3) Don't let it near bright light.
*
* @param {ferbyCallback} callback - The callback function that expects a ferby.
*/
var fooFerby = function(callback){
getFerbyLoader().load(function(ferby){
callback(ferby);
});
};
/**
* The callback for the fooFerby function.
*
* @callback ferbyCallback
* @param ferby The ferby
*/
What is a good naming convention for fooFerby
so that I know by name that it expects a callback?
- i like getFerbyCB, _getFerby, cbGetFerby, or getFerbyAsync... – dandavis Commented Jun 11, 2013 at 15:14
-
@dandavis Thanks for the suggestions. Personally, I like the prefixes so you only have to look at the front of the name to get the general feel for the function, so
_getFerby
andcbGetFerby
would be candidates for that. Looking at synonyms for get, some more candidates areacquireFerbie
,grabFerby
,obtainFerbie
,procureFerbie
, or (my personal favorite)fetchFerby
. I was hoping for a semi-standardized naming convention for this. – Briguy37 Commented Jun 11, 2013 at 15:40
2 Answers
Reset to default 5I use the prefix "fetch", instead of "get" for asynchronous getters.
The idea is that if it is not locally available, you need to fetch it.
.NET uses BeginDoAction. I like the same approach in JavaScript. So in your case, the function would be beginGetFerby
.
NodeJs takes the convention that most methods are asynchronous, and the synchronous methods have a 'Sync' suffix, e.g. doActionSync. You could do the opposite, and have an 'Async' suffix, so your function would be getFerbyAsync
. I like that approach too.
For synchronous getter functions, the naming convention is well-defined:
var getFerby = function(){
..
return ferby;
};
However, if the ferby I want is not locally (synchronously) available, a mon method is to handle that situation with a callback:
/**
* Asynchronously gets a ferby and passes it to the callback.
*
* Once the ferby is retrieved, these rules MUST be followed:
* 1) Don't feed it after midnight.
* 2) Don't give it water.
* 3) Don't let it near bright light.
*
* @param {ferbyCallback} callback - The callback function that expects a ferby.
*/
var fooFerby = function(callback){
getFerbyLoader().load(function(ferby){
callback(ferby);
});
};
/**
* The callback for the fooFerby function.
*
* @callback ferbyCallback
* @param ferby The ferby
*/
What is a good naming convention for fooFerby
so that I know by name that it expects a callback?
For synchronous getter functions, the naming convention is well-defined:
var getFerby = function(){
..
return ferby;
};
However, if the ferby I want is not locally (synchronously) available, a mon method is to handle that situation with a callback:
/**
* Asynchronously gets a ferby and passes it to the callback.
*
* Once the ferby is retrieved, these rules MUST be followed:
* 1) Don't feed it after midnight.
* 2) Don't give it water.
* 3) Don't let it near bright light.
*
* @param {ferbyCallback} callback - The callback function that expects a ferby.
*/
var fooFerby = function(callback){
getFerbyLoader().load(function(ferby){
callback(ferby);
});
};
/**
* The callback for the fooFerby function.
*
* @callback ferbyCallback
* @param ferby The ferby
*/
What is a good naming convention for fooFerby
so that I know by name that it expects a callback?
- i like getFerbyCB, _getFerby, cbGetFerby, or getFerbyAsync... – dandavis Commented Jun 11, 2013 at 15:14
-
@dandavis Thanks for the suggestions. Personally, I like the prefixes so you only have to look at the front of the name to get the general feel for the function, so
_getFerby
andcbGetFerby
would be candidates for that. Looking at synonyms for get, some more candidates areacquireFerbie
,grabFerby
,obtainFerbie
,procureFerbie
, or (my personal favorite)fetchFerby
. I was hoping for a semi-standardized naming convention for this. – Briguy37 Commented Jun 11, 2013 at 15:40
2 Answers
Reset to default 5I use the prefix "fetch", instead of "get" for asynchronous getters.
The idea is that if it is not locally available, you need to fetch it.
.NET uses BeginDoAction. I like the same approach in JavaScript. So in your case, the function would be beginGetFerby
.
NodeJs takes the convention that most methods are asynchronous, and the synchronous methods have a 'Sync' suffix, e.g. doActionSync. You could do the opposite, and have an 'Async' suffix, so your function would be getFerbyAsync
. I like that approach too.
本文标签: javascriptNaming convention for asynchronous getterStack Overflow
版权声明:本文标题:javascript - Naming convention for asynchronous getter - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745612911a2159112.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论