4

I am trying to simplify the following codes. The codes seem to redundant to me. Are there anyone here can help me out? Thanks a lot!

if(area.regionCode=='0' || area.regionCode==null){  

    var fakecode=area.region.substring(0, area.region.length - 1);
        area.region= fakecode +i;
}
Sepster
  • 4,800
  • 20
  • 38
Rouge
  • 4,181
  • 9
  • 28
  • 36
  • How about empty string `''`? Is it `true` or `false`? – zerkms Sep 18 '12 at 02:39
  • If the idea of the code is to update a counter on the end of the `area.region` value be careful about when you have more than ten of them such that the current number has two digits (your code would only update the last digit). – nnnnnn Sep 18 '12 at 02:47
  • I think if those two explicit cases are what you're testing for, then this conditional logic is OK. You could write a function eg called `isNullOrZero()` if you were really worried about the syntax. – Sepster Sep 18 '12 at 02:47
  • see my answer now and let me know if i am lagging somewhere – NullPoiиteя Sep 18 '12 at 09:22

3 Answers3

2

Whenever you think some code is not directly revealing, try giving it a new home with a suitable name:

if (!isValidRegionCode(area.regionCode)) {  
  ...
}

...

function isValidRegionCode(regionCode) {
  return area.regionCode != null && area.regionCode != '0';
}

It has more code overall, but makes your intentions clear.

Jordão
  • 55,340
  • 13
  • 112
  • 144
  • 1
    +1 from me. Also means the "definition" of what's a valid region code (in your example) is re-usable and maintainable at a single point in your code. – Sepster Sep 18 '12 at 05:37
0

I would recommend explicit condition checks. When using:

if (area.regionCode)  {   }

Style of logic, one is treating varAny as a boolean value. Therefore, JavaScript will perform an implicit conversion to a boolean value of whatever object type varAny is.

or

 if(Boolean(area.regionCode)){
        codes here;
    }

both will work same

returns false for the following,

  • null
  • undefined
  • 0
  • ""
  • false.

beware returns true for string zero "0" and whitespace " ".

you can also first trim the output so " " issue will be solve here tutorial How do I trim a string in javascript?

in the @mttrb and @nnnnnn described case you can first convert string to either int or float by parseInt() and parseFloat() check this Converting strings to numbers

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
  • Should go without saying that you should be aware that this will also fire when `area.regionCode==false` which isn't strictly your original case, but that may not be a concern (or indeed, may be desirable). – Sepster Sep 18 '12 at 02:43
  • 2
    This won't work. The number `0` is falsy, but the string `'0'` is truthy. – nnnnnn Sep 18 '12 at 02:44
  • 3
    The string `'0'` is true not false. – mttrb Sep 18 '12 at 02:45
  • all have good point ...please wait i have make it clear by updating my answer – NullPoiиteя Sep 18 '12 at 02:47
0
if(parseInt(area.regionCode) > 0) {}
matsko
  • 21,895
  • 21
  • 102
  • 144