0

So i am pretty new with JavaScript, and it is realy not my cup of tea. We have ran into some issues in a large codebase with code that looks a bit like so:

var no = {somecontent:"text"};//Just any random object
document.write(no.length);
if(no.length != 0){
    document.write("Something is in this list!");
}
if(typeof(no.length) == "undefined"){
    document.write("OH GOD WHY?! THE LIES!");
}

Minus the last write, we just had the '!= 0' check for length of something which we assumed was an array. Since it is Undefined type, it simply returns true and assumed "something is in the list". This obviously caused a lot of issues, and we are afraid that there are a lot more of these errors in the codebase. (1500 lines by now)

So now my question is. Is there any sort of java parser/checker/compiler whichever that catches this in a form of an error or warning like: "You are trying to test for X while object/parameter is "undefined"!" ?

I imagine this must be a pretty common thing to happen, expecially when the programmer is inexperienced with the language. (Comming from Java/C#) But i couldn't find any answer on this besides adding: "typeof(no.length) == "undefined"" To every if statement in the code. Which seems like overkill and probably slams performance in the ground at runtime.

Any help with this issue is greatly appreciated!

Thanks in advance, Smileynator

Smileynator
  • 677
  • 8
  • 24
  • hi, simple objects, like `no` doesn't have length property – nahab Nov 27 '15 at 14:34
  • Possible duplicate of [Detecting an undefined object property](http://stackoverflow.com/questions/27509/detecting-an-undefined-object-property) – Jonathan Lam Nov 27 '15 at 14:35
  • 1
    your array is `no.Rens` – nahab Nov 27 '15 at 14:35
  • Advice: Be careful about how you check undefined. It is not a string: `if ( typeof(no.length) == undefined ) ...` – Andy W Nov 27 '15 at 14:37
  • If that is not the case, why does your code not return true, while my example does? – Smileynator Nov 27 '15 at 14:40
  • 1
    This would require a compiler with knowledge of types to lint your code... Perhaps a tool explicitly geared for typed Javascript could help you, like TypeScript. In general, transitioning your code to TypeScript may be a good idea if the added type checking helps you. – deceze Nov 27 '15 at 14:40
  • 2
    @AndyW `typeof` returns a string, it's correct to compare it to a string. – deceze Nov 27 '15 at 14:40
  • @deceze So there is no real way to do this inside javascript without doing it yourself (or with any tool to write javascript with?) And Typescript basically does some of these check things for you? (or at least limits the ways in which you can make a mistake like this? – Smileynator Nov 27 '15 at 14:43
  • 1
    Accessing undefined properties is legal in Javascript and is in fact the correct way to *test* for the existence of properties (undefined properties yield, well, `undefined`). As such Javascript won't stop you from doing so and there's no way to make it consider doing so an error and warn you in any way. You simply need to be aware of what you're checking and not write silly code like that. ;P – deceze Nov 27 '15 at 14:51
  • As a C# user, can you imagine that this boggles my mind? I recognize that it is a lot like C#'s "NULL" check. But C# would throw an error once i tryp to compare an integer to a variable that is in fact "NULL". I just looked a bit into typescript, and that looks like a promessing solution to my idiotic way of doing JavaScript. – Smileynator Nov 27 '15 at 14:54
  • 1
    I've seen lots of people get headaches transitioning from a statically typed compiled language to a dynamically typed interpreted language. You simply need to get out of the headspace that the compiler is going to catch most errors for you and need to develop practices to catch errors yourself, e.g. by writing other kinds of comparisons or by writing unit tests. Or by transitioning to a type-saver meta language. – deceze Nov 27 '15 at 14:57
  • Does make sense in a way. But to save myself this search i would rather pick the meta language. So about TypeScript, would this make sure my original example would throw some kind of warning on "if(no.length != 0){"? Typing or not, it seems like it wouldn't. Or it would say i am trying to compare int to undefined type? – Smileynator Nov 27 '15 at 15:01
  • 1
    No idea, I've never used TypeScript that extensively at all. It's just a suggestion you can try. – deceze Nov 27 '15 at 15:05
  • @deceze Great catch! Had not noticed that. – Andy W Nov 28 '15 at 04:34

1 Answers1

1

Sounds like you are looking for a tool to automatically check large javascript files for type safety.

You may want to check out Flow, or the blogpost 5 attempts at type safety in javascript.

wintvelt
  • 13,855
  • 3
  • 38
  • 43