Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assertive programming with JavaScript

I know why assertive programming is good, so I want to use it with JavaScript. However, I don't want to show users error boxes, and it's unusual thing. Just ignore it and make them retry could be better.

For example this code will make an error box and interrupt users.

function getDomainFromURL(url) {
    assertTrue(url, 'URL should not be null');

    ...parsing
}

So, I'd make like this.

function getDomainFromURL(url) {
    if (!url) return;

    ...parsing
}

Second one is good for usability, I think, and first one is good for developing. Therefore, IMO it would be the best to combine these and make the assert disappear on production code.

Question 1 Do you think so? Or have any different idea?

Question 2 If you think so, is there any good way to do it with Spring3 framework?

like image 597
Sanghyun Lee Avatar asked Jul 14 '11 12:07

Sanghyun Lee


People also ask

What is assertive programming?

Assertive programming follows the principles of fail fast and fail visibly. It is implemented by issuing an informative error message if the function arguments fail to satisfy specific criteria. This is particularly important in R because it is a dynamically typed language.


1 Answers

Asserts should not be used for validating any kind of user input data. They shouldn't be used for error checking either. Assert is the way to make debugging for a programmer easier in case tests fail. Also it is a way to make your code easier to understand.

Consider this example:

function calculateSomeCrazyFormula(a,b,c) {
    var d = a+b+c;
    ....
    k = (x + y)*2;

    assert(k != 0);
    l = d/k;        
    ....
    return x;
}

My formula by it's specification assures that k will never be 0. Here I used assert almost as a comment in my code. It does not validate the logic - it's completely the opposite. With asserts I validate if my implementation of that complex logic is correct. Later, when I come back to my code and see l = d/k I won't get suspicious about what happens when k is 0, because I see my assert there and understand that 0 should never happen.

Also, if there is an error somewhere and 0 does happen, you will usually see some deeper error. For example:

function otherFormula(a,b,c) {
    var x = calculateSomeCrazyFormula(a,b,c);

    var y = doSomeStuff(x);
    var z = doOtherStuff(y);
    return z;    
}

Without asserts you will get the wrong result of this function. You will have no idea why you are receiving 0 when it should be 1. You will have to start debugging the code line by line and understanding what went wrong.

With asserts, however, you will instantly notice the "assertion failed" error and will instantly minimize the scope to look for the problem.

Asserts are useful in any programming language including Javascript, however they shouldn't be there in Live environment. Therefore, some postprocessor system should remove all of the asserts out of your code before the release.

like image 141
bezmax Avatar answered Sep 24 '22 02:09

bezmax