1

In the interest of code compression, I want to change instances of true and false to 1 and 0, respectively. I won't do any strict boolean equals (like boolA === true). Is this a safe thing to do?

For reference to my specific issue, I'm trying to implement a universal .equals() method that appears on every object and can be given any other object:

Object.prototype.equals=function(o){function f(a,b){for(i in a){j=a[i]if(typeof j=="object"?!f(j,b[i]):j!=b[i])return!1}return!0}return f(this,o)&&f(o,this)}

GitHub

My goal is to make this and a slew of other basic utilities each less than 140 bytes long so I can get my web apps to my users faster and charge them less data.

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • It's safe so long as your calling code is aware that the actual return value is a number rather than a Boolean. i.e. it's safe as long as you don't have `if (myFunc() === true) ...` – p.s.w.g Aug 21 '14 at 00:13
  • 1
    First thing that comes to mind is that your code might be unclear if you did this. – Marty Aug 21 '14 at 00:13
  • Minifiers won't do that anyway. They will shorten to `!1` and `!0` – elclanrs Aug 21 '14 at 00:17
  • well with compressed code, it's already unclear XP - https://github.com/BlueHuskyStudios/Micro-JS-Equals/blob/master/lib/micro-equals.js – Ky - Aug 21 '14 at 00:17
  • 1
    I mean unclear in that if I am using a collection of functions that I didn't write and getting numbers, I don't know whether those numbers are intended to represent boolean values or whether I can expect to see things like `751` at some point. You could *mitigate* that by naming you functions in way that imply boolean values e.g. `isThing()`, but it's still confusing. – Marty Aug 21 '14 at 00:19
  • It can be a problem if you return `false` or `0`, `1`, `2`, e.g., like giving a position in an array as a result, or `false` if it's not found. `false` and `0` will be difficult to discern if not checking type. – Jared Farrish Aug 21 '14 at 00:20
  • My code writing priorities start out: 1) Correctness, 2) Clear and maintainable, 3) Appropriate performance and size, 4) .... Your idea puts the first two priorities at risk in the interest of saving a couple characters. That is rarely (if ever) a wise choice. – jfriend00 Aug 21 '14 at 00:24
  • @jfriend00 good points! Of course, I write the program out in longhand and make sure it's well and proper before minifying it, but I still want to transmit as little as possible over the internet, especially after having to wait long minutes for a website to load just an hour outside of my hometown. – Ky - Aug 21 '14 at 00:31
  • Why are you using *true* and *false* in the first place? Typically you'd just return the result from the expression that creates the value. – RobG Aug 21 '14 at 00:32
  • @RobG sorry, I should've linked my project in the question. This is my solution of a universal `equals` method within `Object.prototype` – Ky - Aug 21 '14 at 00:33

1 Answers1

6

It is not safe in the sense that it will have equivalent behavior, since 1 !== true and 0 !== false. It won't crash the browser, though. Ultimately, it depends on how you define "safe" -- and this is subjective.

However, you can return !0 for true and !1 for false, which will have the same bevaior as returning true or false, respectively, but this is going to make your code significantly less readable.

My two cents: Don't do a minifier's job. Write for readability and maintainability, then use a proper minifier as part of your deployment process if you want to reduce the size of your code.

Community
  • 1
  • 1
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • I define "safe" as in "the program and those who use it should still behave as expected" – Ky - Aug 21 '14 at 00:20
  • 1
    @Supuhstar Then the answer is "no," because `0` and `1` do not have boolean type and therefore will behave differently than `false` and `true` in multiple ways. – cdhowie Aug 21 '14 at 00:21
  • Also, I'm trying to do this for a 140-byte challenge – Ky - Aug 21 '14 at 00:23
  • @Supuhstar - I define "safe" as easily readable and maintainable as to minimize mistakes and promote clarity. If you can automate that into an alternate version for transport speed, it should not be a consideration in your programming toolkit. – Jared Farrish Aug 21 '14 at 00:23
  • @cdhowie can you elaborate? In how many ways will they behave differently? – Ky - Aug 21 '14 at 00:23
  • "*use a proper minifier as part of your deployment process*". That's a bit late, minify before UAT at least, and before system testing if you're confident. – RobG Aug 21 '14 at 00:24
  • @RobG do you have a link to something I can use to get https://github.com/BlueHuskyStudios/Micro-JS-Equals/blob/master/lib/micro-equals.js at or below 140 bytes? – Ky - Aug 21 '14 at 00:29
  • @Supuhstar—I don't understand your comment. I was commenting on the suggestion to use a minifier as a deploy step, when it should be employed during testing otherwise how do you know the minified code is OK? – RobG Aug 21 '14 at 00:35
  • @RobG I'm not sure what you're asking. I've already tested the code and I'm now trying to minify it down below 140 bytes. If you have something that'll help with that, please point me to it – Ky - Aug 21 '14 at 00:37
  • @Supuhstar—I'm not asking you anything, I was commenting on @cdhowie's answer. Your code has an undeclared variable *j*. If your real question is "what is the shortest way to write this function", then post that as a question. I think it can be shorter, but that doesn't mean it's a good idea to do it. "Fewer characters" is never a **good** reason to change code. – RobG Aug 21 '14 at 00:44
  • @RobG ah, sorry; I thought it was to me. Care to bring this to a chat? – Ky - Aug 21 '14 at 00:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59696/discussion-between-supuhstar-and-robg). – Ky - Aug 21 '14 at 00:54
  • @RobG *"minify before UAT at least"* -- I would consider setting up or pushing to a UAT environment to be a form of deployment. – cdhowie Aug 21 '14 at 03:35
  • @cdhowie—I guess the meaning terms depends on workplace culture, specifying the deploy environment would clear things up. ;-) – RobG Aug 21 '14 at 05:30