简体   繁体   English

如何改进这段 JS 代码以使其看起来更优雅

[英]How to improve this JS code to look more elegant

In my React app using Material UI, I wrote a code to manage the size of the fields of a contact form.在我使用 Material UI 的 React 应用程序中,我编写了一个代码来管理联系表单字段的大小。 Those fields are elements that can be added or removed depending on a configuration.这些字段是可以根据配置添加或删除的元素。

The code I wrote works as expected but looks ugly and I need help to make it better.我编写的代码按预期工作,但看起来很丑,我需要帮助使其变得更好。

let size = 12;
    if (useMediaQuery(theme.breakpoints.up('sm'))) {
      const hasTime = fieldsConfig.some(
        f => f.name === 'preferredContactHours',
      );
      const hasContactType = fieldsConfig.some(
        f => f.name === 'preferredContactWay',
      );
      if (name === 'phone' && (!hasTime || !hasContactType)) size = 7;
      else if (name === 'preferredContactWay' && hasTime) size = 6;
      else if (name === 'preferredContactWay' && !hasTime) size = 5;
      else if (name === 'preferredContactHours' && hasContactType) size = 6;
      else if (name === 'preferredContactHours' && !hasContactType) size = 5;
    }

The size is 12 by default, so if any of the conditions are not applicable, the field will still be full size in the grid.大小默认为 12,因此如果任何条件不适用,该字段在网格中仍将是完整大小。

The fields hasTime and hasContactType are optional so I need to check if they are present or not.字段hasTimehasContactType是可选的,所以我需要检查它们是否存在。

The rest is checking if the fields are or aren't there and setting sizes accordingly.剩下的就是检查字段是否存在并相应地设置大小。

What I'd need is to have it better and more efficient than all this verbose code, if that's possible.如果可能的话,我需要的是让它比所有这些冗长的代码更好、更高效。

There are many ways to improve the quality of the code.有很多方法可以提高代码质量。 This is not the only way.这不是唯一的方法。 I think this can turn your code in a more high cohesion e less coupled style.我认为这可以使您的代码具有更高的内聚性和更少的耦合风格。 Hope this helps you.希望这对你有帮助。

ps I not tested the code. ps我没有测试代码。

 const sizeResult = document.querySelector('#size') let size = 12 const name = 'preferredContactWay' const fieldsConfig = [{ name: 'preferredContactHoursss' }] const useMediaQuery = () => true // Object size configuration. This helps to avoid `if`s. const sizeConfiguration = { preferredContactWay_true: 6, preferredContactWay_false: 6, preferredContactHours_true: 6, preferredContactHours_false: 6, phone: 7, } /* SRP (Single Responsibility Principle). One of the SOLID principles and a good practice of the Clean Architecture Each function must have only one reason to change (single responsibility) */ function getHasTimeValue() { return fieldsConfig.some( f => f.name === 'preferredContactHours', ) } function getHasContactTypeValue() { return fieldsConfig.some( f => f.name === 'preferredContactWay', ) } function getSize(_name) { const param = getHasTimeValue() ? getHasTimeValue() : getHasContactTypeValue() return param ? sizeConfiguration[`${_name}_${param}`] : sizeConfiguration['phone'] } function Main() { if (useMediaQuery()) { size = getSize(name) sizeResult.innerHTML = size } } Main() sizeResult.innerHTML = size
 <div id="size"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM