views:

1059

answers:

4

Are there any tools available for calculating Cyclomatic Complexity in Javascript?

I've found it a very helpful metric in the past while working on server side code, and would like to be able to use it for the client side Javascript I write.

A: 

I don't know about such a tool for JavaScript, but my feeling is that, from the moment you're thinking about the cyclomatic complexity of your code, there is a high chance that your code is already written in a too complexe way.

gizmo
I'm thinking about cyclomatic complexity to ensure that my code does not get complex. This is for a new project where none of the code has been written yet!
Karl
+1  A: 

Since cyclomatic complexity is evaluated counting the number of keyword "if, switch, while for break" etc.. every tools that works with C will do the job, like sourcemonitor: http://www.campwoodsw.com/sourcemonitor.html

Actually, on javascript the more you try to modulize your code, the more you will slow it down, so take it with a grain of salt ;)

EDIT: I Really can't understand what's going on on this answer, I get another downvote, when in my answer I tell a good tool for calculating cyclomatic complexity in javascript, and this in particular works very well.

For the second assertion, mine is a comment that comes from experience, I never tell don't modulize your js code, I only tell to make attention in doing it, because often there is a tradeoff with speed, and when I talk of speed I mean that 2 different slowdown can happen: at download time and at execution time (and in slow device like pda/smartphone this is important).

Since tools like this often drive developer into writing more code trying to chase the smaller index possible, but in js more code unfortunately means that slowdowns can happen, and the overuse of these tools is bad. Surelly these tools can give you hints of where your code can be improved, but you've to master how to use the tool and not blindy rely on it.

So if you downvote me again, please write a comment in which you explain why you do so, the discussion can only benefit from this, thank you and sorry for the vent.

kentaromiura
Modularizing your code slow's it down? How does that make sense?
MDCore
Actually it has sense, since in javascript writing more code means more downloading time (is a tradeoff that all knows btw), I wrote javascript code that runs in very slow architecture so, for me, this is an important factor. I didn't say "don't modulize", I just say "be warned"
kentaromiura
I didn't downvote you, but it doesn't seem like modularized slows down your code in javascript is enough to justify writing your code in a procedural style. But I guess that is a case by case basis.
Mark Rogers
You also should be setting "Expires" headers far into the future for your javascript so that the client browser will cache it. This reduces the hit that recurring visitors have to deal with when they visit your site.
Zachary Spencer
+4  A: 

I've been working on a JavaScript static code analysis tool that does caclulate cyclomatic complexity for JavaScript. You can try an online version of jsmeter here: http://boyopeg.googlepages.com/jsmeter.html

+4  A: 

I have moved my jsmeter tool to a new site: http://jsmeter.info

Noah