避免重复的null检查功能中的参数调用函数

发布于 2025-01-30 10:52:38 字数 1140 浏览 5 评论 0原文

上下文:
getProfilesFromResponsewebsocketMessageHandler使用的实用程序函数从WebSocket消息中解析配置文件。

什么是更好,干净的设计?

  1. getProfilesFromresponse重复NULL检查吗?
  2. 仅通过一次零检查一次可见的性能增益吗?这是一个WebSocket消息回调处理程序,因此由于沉重的WebSocket流,它将被称为50倍。
  3. getProfilesFromResponse使用响应?.data wenders> wendment.data吗?
function getProfilesFromResponse(response) {
  // should we add null or undefined checks here as well?  

  if ('profiles' in response.data) { // should I instead do response?.data
    // do remaining data manipulations and return the final data
  }
}

function websocketMessageHandler(response) {
  if (!response) {
    // null checks done here
  }
  ...
  ...
  ...
  const profiles = getProfilesFromResponse(response);
  ...
}

我的意见

  1. 不,我们可以为调用getProfilesFromResponse的所有功能制定规则,以自己进行零检查。缺点是我们假设事情永远不会出错,但是我的反论点是,我们确实希望事情崩溃,以便人们实际上确保他们不会用null参数调用功能。
  2. 不是真的,这是O(1)操作。对性能的疏忽影响。
  3. 更喜欢响应?.data作为其更强大的功能。但是,由于我们已经知道响应不是空的,因此我们可以跳过它。

Context:
getProfilesFromResponse is a utility function used by websocketMessageHandler to parse the profiles from a websocket message.

What's a better and clean design?

  1. Should getProfilesFromResponse repeat the null checks?
  2. Is there a visible performance gain by doing the null checks only once? This is a websocket message callback handler, so its going to be called like 50 times a second due to heavy websocket stream.
  3. Should getProfilesFromResponse use response?.data over response.data?
function getProfilesFromResponse(response) {
  // should we add null or undefined checks here as well?  

  if ('profiles' in response.data) { // should I instead do response?.data
    // do remaining data manipulations and return the final data
  }
}

function websocketMessageHandler(response) {
  if (!response) {
    // null checks done here
  }
  ...
  ...
  ...
  const profiles = getProfilesFromResponse(response);
  ...
}

My Opinion

  1. NOPE, we can make a rule for all the functions that are calling getProfilesFromResponse to do the null checks themselves. The downside is we are assuming things never go wrong, but my counter argument is we do want things to crash so that people actually make sure they don't call the function with null argument.
  2. Not really, it's a O(1) operation. Negligent impact to performance.
  3. Prefer response?.data as its a little more robust. But since we already know response isn't empty, so we can skip it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2025-02-06 10:52:38

1-不,您应该避免调用getProfilesFromResponse如果响应是无效的,例如,通过停止websocketMessockegeageHandler execution> excution> execution:

  function websocketMessageHandler(response) {

    if(!response) 
       return; //< avoid further operations
          
    const profiles = getProfilesFromResponse(response);

    // more code here
  }

或bes 2-

  function websocketMessageHandler(response) {

    if(response) {
      const profiles = getProfilesFromResponse(response);
      // more code here
    } else {
      // do something in case response is null or undefined
    }
  }

有:2-始终是避免无用操作的好处。

3-如果正确避免使用null参数调用 getProfiles ,则不必再次检查,您还避免了一个无用的函数调用...

1 - No, you should avoid calling getProfilesFromResponse if response is null, for example, by stopping websocketMessageHandler execution:

  function websocketMessageHandler(response) {

    if(!response) 
       return; //< avoid further operations
          
    const profiles = getProfilesFromResponse(response);

    // more code here
  }

or alternatively:

  function websocketMessageHandler(response) {

    if(response) {
      const profiles = getProfilesFromResponse(response);
      // more code here
    } else {
      // do something in case response is null or undefined
    }
  }

2 - There is always a benefit avoiding useless operations.

3 - If you properly avoided getProfilesFromResponse to be called with null parameter, you don't have to check again, you also avoided an useless function call...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文