什么时候我应该使用变量而不是原始值

When should I use a variable vs a raw value?

本文关键字:原始 变量 我应该 什么时候      更新时间:2023-09-26

何时应该定义变量?在if语句中使用原始值是否比一次性使用变量更快?例如,原始值:

if(variable == 503) {
    //run code 
} 
VS:

if(variable == anotherVariable) {
    //run some code
}

我正在寻找哪一个会更快,更安全(在一个if语句和一般)。

我想说这取决于变量代表什么。如果它是一个将在多个地方重用的全局常量,那么一定要使用变量。否则,如果它是一次性使用值,则不需要变量。

我倾向于从一个值开始。然后,只要我遇到另一种情况,我重用相同的值,然后我把它移动到一个全局(或必要的作用域)变量。

编辑:

在评论中进行了一些讨论之后,很明显,从长远来看,最好使用描述性变量名写出值。

经验法则是总是为变量和值使用描述性名称(并可能添加注释)。但是,由程序员自行决定是否有足够的上下文来使一个值在没有变量名的情况下存在。要考虑未来的开发人员阅读你的代码,不要假设他们会明显知道你在说什么(这是一个容易犯的错误)。

我建议总是为这些值使用描述性名称。在这个特殊的例子中,503是什么意思?

我完全同意其他的答案-只是试着给另一个灵感…我记得当我开始编码时,我过度投入于微优化,比如哪个变量会表现得更好,但毕竟我个人提出了关于变量和函数名称的明确编码风格规则:

  1. 其他也有编码风格。向有经验的人学习意味着一方面利用他们的经验,可以这么说,更接近"全局"风格,从而使彼此的代码具有更好的可读性。
  2. 选择尽可能具有描述性的名称。它不仅使你的代码更具可读性和可维护性,而且还导致关注代码结构本身内部的功能。
  3. 保持一致并不意味着再灵活,也不意味着在新的经历之后就不再发展自己的风格,但总的来说这是一种很好的做法。
  4. 让名称也告诉您类型。
  5. 让名称也告诉您范围。

在这些一般规则之后,这里有一些实际的例子:

我只是选择了一个非常抽象的例子来展示一般功能…

var _outerScope = 'I prefer to mark my variables with the most global scope with a leading underscore';
(function (){
    var innerScope      = 'while this variable has its scope inside of this function its just meant to be used in here'
    if (_outerScope !== innerScope) {
        'everything is a bit more clear';
    }
    var fSetAttrTitle   = function ( $Selector, iSelector ) {         // read as: "function set attribute title" awaits an "jQuery object" and "integer"
      sOriginalTitle = $Selector.attr('title');                       // ra: "string original title" = "jQuery object selectors attribute title"
      $Selector.attr('title', 'this container has id: ' + iSelector); // ra: "jQuery object selectors attribute title" = "this container has id: " plus "integer selector"
      return sOriginalTitle;                                          // ra: "return string original title"
    };
    var isIdSel2inArray = false;                       // this should be self explanatory
    var aSelector       = ['#sel1', '#sel2', '#sel3']; // ra: "array selector" = [...]
    var iSelector       = aSelector.length;            // ra: "integer selector" = length of "array selector" | normally i would use "i" instead of iSelector in this case but for illustration lets stay with it
    while ( iSelector-- )                       // ra: "iterate until iSelector is 0"
    {
      sSelector = aSelector[ iSelector ];              // ra: "string selector" is a piece out of "array selector" with number "integer selector"
      if (sSelector !== '#sel2') {                     // ra: "if string selector is not '#sel2' then array selector is return value of set attribute title"
        aSelector[ iSelector ] = fSetAttrTitle( jQuery( sSelector ), iSelector );
      } else {                                         // ra: "if string selector is '#sel2' then '#sel2' is in array"
        isIdSel2inArray = true;
      }
    }
    if (isIdSel2inArray === true) {
      alert('ra: "if boolean is id sel2 in array is true alert this text"');
    }
}).call(this);
if (typeof innerScope === 'undefined') {
    'Of course I can not use this variable here while it has no underscore '
    + 'its not in the outer scope but the next one has an underscore so it is '
    + 'save to use it here ' + (typeof _outerScope !== 'undefined');
}

希望它有点鼓舞人心:)