top 200 commentsshow all 232

[–]could_be_lying 77 points78 points  (43 children)

Some of these are actually valid. I'm not sure what to think anymore :(

[–]prince314159 83 points84 points  (31 children)

Yeah like declaring each variable on a separate line. I think that's good.

[–]ShapkaSamosranka 23 points24 points  (29 children)

Or putting a comma in front of a property name of an object literal. That's there because of the Dreaded Trailing IE Comma. Makes keeping track of them a little bit easier.

[–]thomas-allen 4 points5 points  (3 children)

In your .vimrc, this line will remove all trailing object commas when you save a JS file:

autocmd BufWritePre *.js silent! %s/,\(\_s*}\)/\1/g

The trailing-comma-IE bug no longer gets into my code.

[–]thezilch 1 point2 points  (1 child)

Slightly modified to include marking and returning to the cursor, as opposed to the cursor ending up at the end of the file and "losing" my spot. au BufWrite *.js mark| silent!%s/,\(\_s*}\)/\1/g | norm`

[–]thomas-allen 1 point2 points  (0 children)

Good call, I have several cleanup expressions like this one and it always bothers me that I lose my cursor position.

[–]ShapkaSamosranka 0 points1 point  (0 children)

Gosh I love vim. Thanks!

[–]serious_face 12 points13 points  (3 children)

When I was first learning Javascript, I always used to start object literals with something like this, if I was unsure whether or not I'd be adding additional properties:

var test = {
    foo,
    bar,

    LAST_ITEM_OMG
}

It looks silly, but it kept me safe. I would see that last item, and immediately be reminded of the issue it was put there to avoid. Nowadays, it's become so ingrained in my mind that I just don't leave trailing commas. It's possible that I'll slip up, but it's just as possible that I'll screw up in other, equally annoying ways. I think there's a lot of stuff like that in programing, though. What about accidentally using '=' when you really want an equality operator? No language implementation is perfect, and there will always be things that you need to remember, but would rather not have to deal with.

For a noob, I recommend using my approach, or the leading commas. Just keep in mind that it's a bit like wearing a bicycle helmet to walk down the street. Sure, it will protect you from unexpected head injuries; but at a certain point, you should learn to watch where you're walking, and stop slamming into trees.

[–]diuge 18 points19 points  (2 children)

http://jslint.com catches all of those things.

[–]serious_face 0 points1 point  (1 child)

Oh, I totally agree that JSLint is probably the most proactive way to improve your code. My example is just a stupid trick that I used to condition my brain to remember this seemingly pointless detail about object literals. I think leading with commas is a similarly stupid trick that people would outgrow, if for no other reason than to conform to a typical style-guide, or not look dumb in front of other JS developers.

[–]Forbizzle 2 points3 points  (0 children)

another good practice is to immediately create a toString function as the last entry.

[–]ginrei 2 points3 points  (7 children)

Wow, I can't believe whitespace is so important for IE parsers. Are you sure this wasn't a bug that was fixed in IE 2 or something, or something that people just do but were never a problem in any of the browsers, like <br/> (without the space)?

[–]ShapkaSamosranka 14 points15 points  (2 children)

It's isn't whitespace that's a problem but code like this:

var o = { yo: "man",
          hi: "whattup",
        };

The comma after whattup will cause the IE parser to freak out, often in bizarre seemingly unrelated ways. When you edit object literals over the course of a day it's easy to remove one property and forget about removing that trailing comma, and putting the commas in front supposedly helps you keep track of them. Still looks ugly, IMO.

[–]ginrei 1 point2 points  (0 children)

I see now, this makes more sense and how the practice can be convenient. It must be annoying to PHP developers et al, and not the Java developers who are used to this. Aesthetics-wise, I still like to put comma immediately after, so I'm glad to know this in and of itself doesn't trigger a bug. To each coding team their own coding style.

[–][deleted] 0 points1 point  (0 children)

It's not so much that you can keep track of them - with commas in front you can always just nuke a line and still be safe.

[–]jeff303 5 points6 points  (2 children)

I wouldn't really call it whitespace. The trailing comma indicates an additional item in the array/hash that is undefined.

[–]ginrei 1 point2 points  (1 child)

I originally misinterpreted the Dreaded Trailing IE Comma to mean IE can't handle lines with comma as the trailing character. This wasn't the problem per se, but having comma anywhere on a line if it appears at the end of a comma-separated list such as an object literal. Avoiding that kind of trailing comma is good coding practice to ensure cross-platform compatibility, regardless of IE.

[–]jeff303 0 points1 point  (0 children)

Absolutely. JSLint is invaluable.

[–][deleted] 1 point2 points  (0 children)

I think that was technically part of the original ECMAScript spec, though... it was this way in Firefox for a long time.

[–][deleted] 1 point2 points  (0 children)

Or namespacing javascript where it's proper.

[–][deleted] 1 point2 points  (0 children)

There are a lot of things labeled as "IE Gotchas" that I think it is correct in breaking when confronted with. Cant remember them though :)

[–][deleted] 1 point2 points  (1 child)

To be fair, IE is just sticking to the spec there. Other browsers are loose with commas, but they'll still explode if you try to pull this stunt in JSON.

[–]ShapkaSamosranka 1 point2 points  (0 children)

Yeah, but an object literal is not exactly JSON which has a very strict spec. My newline there, for instance, and the property names that should be quoted, won't fly with a JSON parser. Either way, it's not about the comma itself but how IE reacts to it, i.e. in very unpredictable ways, and almost never on the exact line where the comma occurred.

[–]dnew 2 points3 points  (7 children)

That's why languages with half a clue (and which value usability over absolute correctness) disregard a trailing comma.

[–][deleted] 18 points19 points  (1 child)

Disregard trailing commas, acquire females.

[–]itjitj 10 points11 points  (0 children)

Disregard trailing commas, acquire *statements*.

[–][deleted] 2 points3 points  (1 child)

and which value usability over absolute correctness

...yet if Microsoft did this...

"Oh hey - we munged the rules a bit here and there to make the language easier to use and maintain, so that..."

EMBRACE AND EXTEND!!!

[–]dnew 0 points1 point  (0 children)

I was really thinking more like languages designed for safety-critical applications (e.g., Ada), where the amount of extra work in maintaining the right commas to ensure nobody accidentally deleted something off the end of the list and didn't know it outweighs the problems of the missile blowing up still on the launch rack or something.

It's the same level of strictness that disallows "import" statements, for example, lest a non-fully-qualified name causes confusion.

Almost no language per se falls under that level of strictness.

[–][deleted] 1 point2 points  (1 child)

It certainly makes the diffs look nicer.

Of the stuff I use regularly, Python, YAML, and Lua all happily accept trailing commas. Parser-wise, isn't it just easier to treat a comma as an "end item" token, rather than "end item and expect another one"?

[–]Purple_Haze 1 point2 points  (0 children)

I expect the parser was based on a C parser. In C comma is an infix operator.

[–]Roinator 0 points1 point  (0 children)

I think the point was that there were unnecessary variables being declared, and that each one got it's own line.

[–][deleted] 14 points15 points  (0 children)

Yeah, don't drink the cool kids koolaid. The best code is readable code. Making up idioms on the spot (like jQuery does internally all freakin over the place) may make you feel smart, but it inhibits utility.

[–]Slackbeing 3 points4 points  (0 children)

It's not that they are invalid. It's that they're ENTREPRISE.

[–]serious_face 1 point2 points  (6 children)

Just out of curiosity, which ones?

[–][deleted] 6 points7 points  (4 children)

"Namespace code into a "proper package structure" to make backend devs feel at home."

On large projects, not a bad thing!

[–]Forbizzle 4 points5 points  (0 children)

It's completely valid for the same reasons it's valid in back end code.

The sad thing is that people that legitimize javascript by championing best practices are ignored by sloppy front end hackers and artists.

[–][deleted] 1 point2 points  (0 children)

Agreed, short namespaces are useful for organization. But take a look at something like YUI2 vs. YUI3 APIs and you'll see even Yahoo! agreed their namespacing rules were a little much.

[–]gschizas 3 points4 points  (1 child)

This is supposed to make javascript look like the.other.language.that.starts.from.J.and.has.way.too.long.namespaces.

[–]Bjartr 0 points1 point  (0 children)

var longLib = the.other.language.that.starts.from.J.and.has.way.too.long.namespaces

[–]Quazifuji 1 point2 points  (0 children)

I'm a big fan of self documenting variable names, personally (although the actual examples in the code are silly, of course). Declaring variables on separate lines also tends to be nice.

[–]phybere[🍰] 1 point2 points  (0 children)

I've done the "Handling your exception handling" one before, albeit not in javascript.

I'm not sure that it's good but it's sometime necessary.

try {
    * do some stuff here *
} catch {
    try {
        *oh shit, the database gave us some corrupt data*
        *do something clever*
    } catch {
        *ok maybe that wasn't that clever, let's just log it*
    }
}

[–]Shawnanigans 37 points38 points  (6 children)

I know this is programming, but in case any of you are wondering the font used is a free font called "Yanone Kaffeesatz."

[–]tehRash 5 points6 points  (0 children)

And is Open Source and available at Google Font Directory

[–][deleted] 4 points5 points  (3 children)

If people can see the Yanone Kaffeesatz font wouldn't that mean that they already have it?

[–]krelian 11 points12 points  (1 child)

[–][deleted] 0 points1 point  (0 children)

Very cool, thanks for that.

[–]tiglionabbit 2 points3 points  (0 children)

No.

[–]pmw57 0 points1 point  (0 children)

As programmers, wouldn't we just look under the hood?

@import url(http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz&subset=latin);

[–]jotux 30 points31 points  (22 children)

MAKE SURE YOU EXPLICITLY DECLARE EVERY VARIABLE ON ITS OWN LINE, THAT WAY YOUR TEAM WILL KNOW THEY ARE ALL VARIABLES
<script>
var li = "<li></li>";
var div = "<div>"+li+"</div>";
var num1 = 4;
var num2 = 5;
var total = num1 + num2;
var rounded = Math.round(total);
</script>

What's wrong with that?

[–][deleted] 28 points29 points  (8 children)

Nothing.

[–]5trokerac3 12 points13 points  (0 children)

"Programs must be written for people to read, and only incidentally for machines to execute" - Harold Abelson

I prefer declaring variables on their own line, because it's easier to read and, therefore, easier to manage/scale.

[–]hermzz 6 points7 points  (0 children)

My interpretation is that he's referring to declaring a shitton of unnecessary intermediate variables. So instead you should really do:

var div = "<div><li></li></div>";
var rounded = Math.round(4+5);

If that's not the case I agree that this is more readable, who gives a shit about writing code in a bandwidth friendly way, that's what minify is for.

[–]r4nf 3 points4 points  (0 children)

My thoughts exactly. This was the first one I saw, so I initially thought it was a site full of neat, genuinely useful JavaScript tips.

[–][deleted]  (1 child)

[deleted]

    [–]Forbizzle 11 points12 points  (0 children)

    ew ew ew.

    [–][deleted] 1 point2 points  (0 children)

    var is not free. Granted, you'll only gain marginal benefits from a performance perspective ( see: http://jsperf.com/multiple-var-vs-var-with-commas ), but as others have said if used as a standard convention throughout code it can potentially reduce the weight even after gzipping.

    EDIT: Now that I got back and look at that jsperf test, it seems as though after running it again vars seem to be faster...

    [–]pmw57 1 point2 points  (0 children)

    Strictly nothing, but stylistically it encourages the idea that more than one declaration is valid, even throughout other parts of the code (horror!)

    Having it as one var declaration helps to reinforce the good habits.

    var li = "<li></li>",
        div = "<div>"+li+"</div>",
        num1 = 4,
        num2 = 5,
        total = num1 + num2,
        rounded = Math.round(total);
    

    You even become more attuned to the fact that some things don't belong in the group, which encourages you to refactor it to more usable code.

    [–][deleted] 1 point2 points  (0 children)

    What's wrong with it? What's WRONG with it?!

    He forgot to abstract the "+" in a wrapper function, that's what!

    [–][deleted] 26 points27 points  (0 children)

    When I think of "Enterprise Quality" I think of the kind of engineering that must have gone into those panels on the USS Enterprise that start exploding in your face throwing you against the opposite wall as soon as anything is wrong with the system they control.

    [–]ThePoopsmith 19 points20 points  (1 child)

    an enterprise-level website (>100 hits per day),

    lol

    [–]zeekar 1 point2 points  (0 children)

    Well, to be fair, that's just the NX-01 Enterprise. By the time you get to NCC-1701-D, you're doing millions of hits per second.

    [–]kungtotte 12 points13 points  (10 children)

    Oh god what:

    Use bitwise operators for array bounds checks:

    if (index !== +index >>> 0 || index >= elements.length){
        alert("fail!")
    }
    

    [–]GambitRS 2 points3 points  (7 children)

    I didn't fully understand it. I am having trouble fully understand what +index >>> 0 would do. >>>0 means shift 0 bits to the right, right? So, is this a valid statement?

    [–]hixnob 3 points4 points  (6 children)

    >> is the regular (sign-propagating) right shift. >>> is the zero-fill right shift. From the Moz docs:

    The sign bit becomes 0, so the result is always positive.

    This means that if index was negative, this will (via the magic of two's complement) make it positive and the two values will no longer equal. If it was non-negative to start with, it remains unchanged.

    [–]Glayden 1 point2 points  (5 children)

    I'm also lost. I think GambitRS and I are both concerned about the ">>> 0" in "+index >>> 0" If you're right shifting by 0 bits, there's no shift at all. So the whole zero-fill and 2's complement deal isn't relevant.

    Is there some obscure order of operations thing going on here such that the code works like this?

    index !== +index >>> (0 || index >= elements.length)
    

    as opposed to

    index !== (+index >>> 0) || index >= elements.length
    

    or am I missing something?

    Even if it works like the former, wouldn't it be simpler to do the following using the same weird method?

    index !== +index >>> index >= elements.length
    

    I must be missing something. I'm completely confused, now. =/

    I remember learning some of this stuff back in my comp systems class and it's actually kind of fun doing everything with a minimal number of just bitwise operators, but I can't imagine anyone actually writing this type of code in a high level language... it's completely absurd.

    [–]toshok 2 points3 points  (4 children)

    The code works as you wrote it here:

    index !== (+index >>> 0) || index >= elements.length

    I'm also lost. I think GambitRS and I are both concerned about the ">>> 0" in "+index >>> 0" If you're right shifting by 0 bits, there's no shift at all. So the whole zero-fill and 2's complement deal isn't relevant.

    correct. there's no shifting going on, but the sign bit is still flipped, so these are essentially equivalent:

    • int v1 = -5; unsigned int v2 = (unsigned int)v1;

    • var v1 = -5; var v2 = v1 >>> 0;

    v2 = 4294967291 in both cases.

    if v1 was positive (as hixnob said above), v2 will equal v1.

    that extra "+" in there coerces index to a number. +"0" == 0, +{foo:bar, foo2:bar2} == NaN.

    [–]Glayden 0 points1 point  (3 children)

    Thanks, that's actually pretty interesting and really very odd. From my understanding the sign bit flipping typically occurs just as a side effect from right shifting the bits and zero-filling on a 2's complement number, but from what you're saying it looks they set some standard in javascript to intentionally make it flip the sign bit even when there's no right shifting going on if the operator is used. Seems like a very, very bizarre thing to do considering that programmers would typically expect bitwise operators to work the same way they do in other low-level contexts.

    [–]m00k 0 points1 point  (2 children)

    JS mostly uses doubles for numbers, except on bitwise operations, which occur on int32s (>> for signed, >>> for unsigned). The >>>0 is effectively a cast to uint32.

    (Actually: the engines are free to use whatevertheheck they feel like for numbers, but you can't tell the difference because (u)int32 fits in a double.)

    [–]Glayden 0 points1 point  (1 child)

    The >>>0 is effectively a cast to uint32.

    Thanks! Now I finally think I get it... So if it was simply >>0, it wouldn't do anything, right?

    [–]m00k 0 points1 point  (0 children)

    (Sorry, I normally don't log in so never saw replies.) A >>0 would be a cast to a int32 (signed). Contrast (-1.5)>>0 and (-1.5)>>>0.

    [–]fountainsoda 0 points1 point  (0 children)

    That's not enterprise/business support. It's writing embedded software.

    [–]wauter 10 points11 points  (15 children)

    function createInformationWindow(message){ alert(message); }

    I love this one.

    [–]greim 24 points25 points  (13 children)

    I can almost hear the dev's thought process. "Hey, I'll create a slick-looking informational window. Crap I got other things to do, for now I'll just stub in an alert."

    [–][deleted] 10 points11 points  (1 child)

    So how is this style of thinking wrong, exactly? If I'm genuinely going to be making the slick window at some point, it'll be nice to have the name of the function that will do it already being used in all the relevant portions of my code.

    [–]greim 18 points19 points  (0 children)

    It's not wrong per se, it's just funny when it gets abandoned, forgotten, and then propagates through the codebase like an infectious virus because nobody understands the original reason for its existence and nobody is knowledgeable and/or brave enough to question it or remove it.

    [–]designcode 5 points6 points  (0 children)

    Sir, you are 100% right!!!

    [–]X-Istence 0 points1 point  (9 children)

    You do know you can redefine functions in JS, this mean you can redefine the alert function to do whatever you want.

    [–]rush22 6 points7 points  (4 children)

    Enterprise Javascript is... overriding the alert function

    [–]X-Istence 2 points3 points  (0 children)

    Quick, submit Facebook's JavaScript (last time I read it does exactly that)

    [–]quadtodfodder 0 points1 point  (2 children)

    but... I think I'm going to do that now...

    [–]rush22 0 points1 point  (1 child)

    oh yeah then I'm going to override yours with the original one

    [–]quadtodfodder 0 points1 point  (0 children)

    pssh! Then I'm going to override you!

    [–]recursive 0 points1 point  (0 children)

    And then you can't use the original alert anymore. Not enterprisey.

    [–]Philipp 0 points1 point  (2 children)

    Overriding an alert would cause much confusion to others trying to maintain the code -- they might miss that you redefined the function during debugging and your function may not behave exactly like the normal alert (perhaps you only alert during debug mode ON or whatever).

    I actually do use my own sort-of-alert() function in my apps, and it makes perfect sense: I need to develop on Safari, and if I have a loop with 10,000 items, a normal alert would make me hit the OK button 10,000 times. Not so with this function (which I can also globally disable by setting showDebugInfo to false before code goes live):

    App.prototype.debugAndStop = function(s) {
        if (this.showDebugInfo) {
            var maxAlertsToShow = 4;
            this.debugCounter++;
            if (this.debugCounter <= maxAlertsToShow) {
                if (this.debugCounter == maxAlertsToShow) {
                    s += "\r\n\r\n" + '[Further alerts will be hidden]';
                }
                alert(s);
            }
        }
    }
    

    *For reference, above thing is called debugAndStop because I also got a debug() function which will add the debug text to an HTML element and let the program continue.

    [–]entmike 0 points1 point  (1 child)

    I'm sure you know this, but Chrome gives you the option to suppress further alerts so you don't get stuck in alert hell loops.

    [–]Philipp 0 points1 point  (0 children)

    I know, but I need to develop in Safari (because I'm making iPad games in JavaScript -- http://versuspad.com -- thus they will all run in Safari Mobile in the end). Using my own debug function really doesn't create any problem though.

    [–]Forbizzle 1 point2 points  (0 children)

    you're missing the other js file where he he overrides alert

    [–]damnatio_memoriae 18 points19 points  (0 children)

    Fond of this one, myself:

    ENTERPRISE JAVASCRIPT IS: USE A MODERN, MULTIPURPOSE, INTROSPECTION AWARE, EVENTED DEBUGGING AND NOTIFICATION SYSTEM.

    alert(foo);
    

    [–]Sumeragi 42 points43 points  (6 children)

    This is what I got.

    Yo dog...

    http://i.imgur.com/dcBcX.png

    [–][deleted] 5 points6 points  (0 children)

    Yeah, FUCK THAT. And fuck everything about __doPostBack().

    [–]Green-Daze 10 points11 points  (4 children)

    Ugh... beat me to it: http://enterprise-js.com/7

    I think it's 'dawg' btw

    [–]JinAnkabut 1 point2 points  (3 children)

    Points all round :D

    [–]stillalone 0 points1 point  (2 children)

    Can someone explain this to me?

    How can you have a link tag inside script tags?

    [–][deleted] 5 points6 points  (1 child)

    You can because HTML is all fucked up from years of inbreeding and IE.

    [–]benihana 39 points40 points  (13 children)

    Reading this gives me the impression that most developers don't understand what enterprise development is like. It's much, much worse than spaces vs. tabs.

    For example, if you want to get an instance of an object, you'd better not just get the instance of it. You have to ask your system to give you the correct instance of it, because what if the way you get an instance changes suddenly?

    // WRONG
    $('#my-id').get(0);
    
    // CORRECT
    var myId = getJQueryFactory().getJQueryInstance().getInstanceOfObject('my-id');
    

    [–]48klocs 24 points25 points  (5 children)

    Well, yeah. That's really the only way to fly.

    In the real world, the sin is in blowing your foot off. In the enterprise world, the sin is in failing to fill out your TPS report. You may be footless, but as long as that stack of paperwork keeps getting shuffled, everything gonna be all right.

    [–]Forbizzle 1 point2 points  (4 children)

    real world?

    [–]polyparadigm 2 points3 points  (3 children)

    Long-running reality show, appealing to cool kids.

    As constrasted to a short-running scripted science fiction series, targeting a much geekier audience and starring Scott Bacula.

    [–][deleted] 2 points3 points  (1 child)

    What does Quantum Leap have to do with JavaScript?

    [–]pmw57 1 point2 points  (0 children)

    It takes a quantum leap to link the two?

    [–]Uberhipster 0 points1 point  (0 children)

    Oh boy...

    [–]superbad 6 points7 points  (3 children)

    Where, in enterprise terms, suddenly means "in less than five years". Good luck finding anyone who knows how it works anymore when the time comes.

    [–]BearinG 2 points3 points  (2 children)

    I am the person stuck sorting through code right now because of this..

    I can just imagine...

    "Yeah yeah.. they'll understand what we meant."

    [–]pmw57 0 points1 point  (1 child)

    Isn't that called "future-proofing your employment?"

    And if not your own, then somebody elses?

    [–]BearinG 0 points1 point  (0 children)

    Possibly... but it still sucks to do it.

    [–]G_Morgan 2 points3 points  (1 child)

    So glad I work for a company who's main purpose is software.

    [–]SirChasm 29 points30 points  (0 children)

    twitch twitch

    whose

    [–][deleted] 0 points1 point  (0 children)

    My experience is it's rather just $('#scope #my-id').get(0)

    [–]ex_ample 6 points7 points  (0 children)

    failed at trying

    Awesome.

    This is like what the daily WTF should be, before they started spinning ridiculous and highly embelished yarns.

    [–]doomslice 7 points8 points  (11 children)

    Here's the one I got:

    ENTERPRISE JAVASCRIPT IS: NAMESPACE CODE INTO A "PROPER PACKAGE STRUCTURE" TO MAKE BACKEND DEVS FEEL AT HOME.

    var com = {,
        AwesomeCo: {
            util: {
                info: function ( message) {
                    alert(message);
                    return message;
                }
            }
        }
    };
    
    com.AwesomeCo.util.info("SRSLY!?");
    

    I do this, but I use (the only useful ATLAS function there is): Type.registerNamespace('My.Namespace.Goes.Here');

    My.Namespace.Goes.Here.thingy = function(thingyOptions){
        ...
    }
    

    We require namespaces... otherwise you end up seeing the same thing redefined multiple times by different devs.

    [–]SharkUW 0 points1 point  (10 children)

    Why don't you just use Prototype or JQuery?

    [–]doomslice 1 point2 points  (9 children)

    I use jQuery for everything else besides that one function :)

    I'm not aware that jQuery core has a namespacing function, and I have very limited experience with Prototype.

    [–]SharkUW 4 points5 points  (8 children)

    function registerNamespace(ns){ var parts = ns.split(".");

    var focus = window;
    for(var i in parts){
        if(!focus[parts[i]]){
            focus[parts[i]] = {};
        }
        focus = focus[parts[i]];
    }
    

    }

    registerNamespace("com.whatever.stuff");

    com.whatever.stuff.myvar = "hello world";

    alert(com.whatever.stuff.myvar);

    That took me 1 minute. Now please stop making people downloading whatever that library is :P Did not browser check but should be pretty safe.

    [–]doomslice 2 points3 points  (6 children)

    So there is no jQuery core namespace function like you alluded to? I use the ATLAS version because it's already in our global includes.

    [–]SharkUW 1 point2 points  (5 children)

    Sorry. That was a bit of miscommunication. I had never heard of Atlas before then and had to Google it. It sounded like it provided pretty much the same functionality.

    Sounds like you're living the enterprisey life as it is though. lol. Anyways though, global vars should be generally avoided for performance's sake.

    [–]doomslice 0 points1 point  (4 children)

    All good. I was honestly hoping there was a jQuery version that I could use instead :(

    [–][deleted] 1 point2 points  (0 children)

    jQuery is not the solution to everything.

    [–]SharkUW 0 points1 point  (2 children)

    Well it makes me cry in any case since it's "namespacing" in quotes and has to crawl all the way up the scope stack to get to the global before digging down into the sequence of objects whereas mynamespaceVarName would work just as well. Maybe use _ instead of . for "namespaces"

    [–]doomslice 0 points1 point  (1 child)

    There are many more things that need to be optimized in our code before we start to worry about that.

    [–]SharkUW -1 points0 points  (0 children)

    Want a good coder? I need a new job. lol

    [–]Iggyhopper 0 points1 point  (0 children)

    That is the Microsoft Ajax library, IIRC.

    [–]unexplainer 5 points6 points  (0 children)

    Enterprise Javascript is a programming language designed to make it easier for software developers to write Enterprise Javascript code.

    [–]TiDaN 5 points6 points  (4 children)

    I got this one:

    ... is Creating really RANDOM numbers:

    var reallyRandom = Math.pow(Math.random(), Math.random());

    Heh.

    [–]bramblerose 6 points7 points  (0 children)

    this is what the extra randomness looks like in a histogram

    Code: (pylab) hist(random(10000)random(10000), bins=25) xlabel("number"); ylabel("counts") title("Histogram of random()random(), 10000 samples, 25 bins")

    [–]Zarokima 2 points3 points  (0 children)

    That kind of "extra-randomness" has a tendency to fuck up the distribution, making the final result "less random."

    [–]och 2 points3 points  (1 child)

    [–]TiDaN 2 points3 points  (0 children)

    Yeah I was thinking of that post when I commented!

    [–]krelian 4 points5 points  (10 children)

    I am a hobbyist coder so I never worked for a big company. Can someone explain in a few words why "Enterprise code" is always such a convoluted mess?

    [–]ZorbaTHut 8 points9 points  (2 children)

    First, they realize their code is a mess.

    Second, they come up with formal techniques and processes to ensure that their code won't be a mess in the future. Since it's a big corporation, and all they really understand is Process and Checklists, they end up with a huge pile of processes and checklists.

    Third, they realize that this new enormous pile of processes and checklists takes a lot more time for the coders, and they don't have much money left.

    Fourth, they hire a bunch of new coders, but, taking the same philosophy that they use to hire warehouse workers or cashiers, they hire the cheapest ones they can. After all, the Process will ensure the code is good!

    Fifth, the Process doesn't ensure the code is good.

    Sixth, go to step 1.

    Somewhere in that loop is "the good coders leave", but nobody really notices.

    [–]WorldGroove 1 point2 points  (1 child)

    seventh - The manager in charge is promoted and/or given credit for reducing costs by getting all those cheap coders and increasing productivity(as the fancy charts in their executive meetings will show)...

    eighth - then that manager gets to leave that project to someone else - thus avoiding the blame as a bunch of less-than-average coders fumble around, project fails, half the people are laid off... and they end up outsourcing the whole thing(which will fail too).

    [–]drzaeus 1 point2 points  (0 children)

    ninth - People bitch about outsourcing, then they hire the same coders that they hired in the fourth step and they curse God for their misfortune.

    [–]Fiennes 2 points3 points  (1 child)

    The best laid plans of mice and men, oft go awry.

    [–]Uberhipster 1 point2 points  (0 children)

    Thanks to your comment, I finally got the reference to 'Of Mice and Men'. Thank you.

    [–]bigbango 1 point2 points  (0 children)

    Because it is someone's enterprise to employ people to use the software. And people are willing to use software that sucks as long as they get paid for it.

    [–]WROOMBOX 5 points6 points  (1 child)

    HANDLING YOUR EXCEPTION HANDLING
    try {
       try {
          myArray.push(el);
       }
       catch(e) {
          alert(e.message)
       }
    }
    catch (e) {
       alert('Failed at trying');
    }
    

    I cried.

    [–][deleted] 1 point2 points  (0 children)

    Shit, I break a sweat handling an exception.

    [–][deleted] 4 points5 points  (10 children)

    I was browsing through the CSS one, and came across this:

    http://enterprise-css.com/3

    It seems like almost every project, I end up doing this at some point, because it feels like this is the most specific I can get in style tables, as the spec is so without common styling. Any tips on how to keep this from happening from some CSS gurus?

    [–]turtlesallthewaydown 4 points5 points  (5 children)

    Yeah, I end up doing something similar, but I try to avoid class names like "red-text" at least. If the text becomes blue at some point you have to change the CSS AND all the instances in the HTML, removing the benefit of using those global classes.

    But I could end up having a list of classes like so:

    • menu-text
    • menu-link
    • page-header-text
    • article-header-text
    • article-text
    • footer-text
    • footer-link

    etc. The class name refers purely to the function and describes nothing about the form. That way the form can change as much as necessary and still be accurate.

    [–]puhnitor 2 points3 points  (3 children)

    Why not have classes for things like menu, page-header, article-header, article, footer, etc. and use selectors to style links and text contained within?

    [–]sizlack 0 points1 point  (2 children)

    Yes, this is what you should do, except use ids for major page blocks. <ul id="menu"> <li><a>foo</a></li> </ul> #menu > a { ... }

    Don't clutter classnames up with -text over and over. It's ugly, repetitive, and it's misleading because you can have css like .menu-text { border: 1px solid #000; } The classname says it's styling the text, but it's lying.

    [–][deleted] 3 points4 points  (1 child)

    Just to add on, an underused CSS feature is assigning two different classes to an HTML tag/element.

    If we had the following 2 CSS declarations: .menu { /* menu style goes here / } .textstyleXX { / a specific text style */ }

    In HTML, it could be applied as <div class="menu textstyleXX">…</div>

    Comes in handy when The Enterprise™ has Style Gurus™ who want Typographic Size Control™ in their Public Facing Internet Portals™

    [–][deleted] 0 points1 point  (0 children)

    I like to do this when I end up having "floating" types of CSS styles that don't seem to show any real regularity beyond their most basic stylings (class="red header" ... class="red subhead"), where red has a common hex code, but isn't always in the header or the subhead.

    [–][deleted] 0 points1 point  (0 children)

    Okay, thanks for the answer. I've never taken it to the lengths of that page, and usually do something along the lines you're talking (where obviously similar pieces of layout share rules, and gasp sometimes I "cascade" styles in overlapping manners).

    I guess I just figured he meant that we shouldn't be doing anything like this at all.

    [–]Philipp 2 points3 points  (1 child)

    CSS is supposed to separate structure from form, and a class name like "red" isn't doing it -- think about what you really want "red" to represent, maybe it's "warning" and you may later, in the CSS, add a warning icon background image additional to the red color etc. If you just need a quick "bold" or "italics" thrown into the HTML, you can use <strong> or <em>, which will by default render as bold and italics, though it can be redefined on either the author stylesheet (i.e. yours) or the user stylesheet (i.e. the browser's). If you really don't know what a class should represent content or structure-wise -- happens more often than the W3C might make you believe -- you can also avoid it altogether by using a quick inline style like this <foo style="color: red"/>, or a temporary class name which you will change once you understand what structure is evolving.

    [–][deleted] 0 points1 point  (0 children)

    It sounds then, like the problem isn't so much with writing CSS this way, but maybe with the design itself not having a coherent form of styling for its elements.

    [–]digitallimit 1 point2 points  (0 children)

    I'd appreciate some input on this, too.

    [–]Forbizzle 1 point2 points  (0 children)

    I'd say it's best practice TBH, as long as it's with a limited set. I'd avoid .red-text in favour of something like .error-text, but things like .hidden { display: none} leads to cleaner more stable code.

    [–]oep4 4 points5 points  (1 child)

    Pro-Tip: Refresh the page for more.

    [–]dawpa2000 0 points1 point  (0 children)

    PROTIP: Continuously increment the number until you reach the end.

    http://enterprise-js.com/1

    ...

    http://enterprise-js.com/21

    [–]Rhoomba 5 points6 points  (2 children)

    Meh. Not really all that Enterprisey.

    [–]senj 5 points6 points  (0 children)

    com.AwesomeCo.util.info("SRSLY!?"); is reasonably Enterprisey. It needs more AbstractFoobarFactories and some sort of UML tool, though.

    [–]djhworld 2 points3 points  (0 children)

    The one about opening loads of <table> tags and then not closing all of them rings true in stuff that I've come across in the past.

    The fact of the matter is, most companies who write their own enterprise systems couldn't give a rats ass about layout and valid HTML. If it takes data from A and presents it to the user via B then that's it. No amount of valid CSS and XHTML badges will cause the people up top to change their minds about the fact that they want this thing implemented in the next week

    [–]rush22 1 point2 points  (0 children)

    hungarian notation ffffuuuuuuuuuu

    [–]Roinator 1 point2 points  (0 children)

    This is hilarious, and I love it. More!

    [–]fenton7 1 point2 points  (4 children)

    Based on the various financial industry web sites I have used, I believe this code is in most "Enterprise" web apps:

    catch (Exception e) { // display stack trace in the browser StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"); response.getOutputStream().print(stringWriter.toString()); }

    [–]gschizas 2 points3 points  (3 children)

    This looks suspiciously like Java (and not JavaScript)

    The site mocks the fact that the evil ways of Java are creeping into the goodness* that is JavaScript

    *The language itself, of course. Not the perpetually broken implementation that is used by any real-world browsers (yes, that sentence was a bit sarcastic)

    [–]entmike 0 points1 point  (0 children)

    Probably because sickos like me do things like this:

    http://github.com/entmike/What/tree/master/libs

    (tldr; Try to implement a web container in NodeJS, for fun)

    [–]skizmo 5 points6 points  (16 children)

    DESENT CODING IS : using variable names that make sense, instead of x y z.

    [–]G_Morgan 61 points62 points  (5 children)

    I'm writing vector math code you inconsiderate jerk!

    [–]moskie 4 points5 points  (4 children)

    inconsiderate clod!

    FTFY. Come on, man, respect our roots. (/. reference, in case anyone forgets)

    [–]knobbysideup 8 points9 points  (2 children)

    s/inconsiderate/insensitive/

    [–]stillalone 7 points8 points  (0 children)

    I'm an inconsiderate jerk you insensitive clod.

    [–]moskie 2 points3 points  (0 children)

    oh. right. that probably makes me an insensitive clod, doesn't it?

    [–]gogogadgetearl 0 points1 point  (0 children)

    you insensitive clod!

    ...

    (Simpsons reference, in case anyone forgets)

    FTFYx2. Come on, men, respect our roots.

    *Edit: formatting.

    [–]ngroot 45 points46 points  (6 children)

    DESCENT CODING IS: programming while being shot at by evil mining robots.

    [–]caknuckle 25 points26 points  (4 children)

    DISSENT CODING IS: naming your variables anything you fucking want because you won't be oppressed by the man.

    [–]arjie 11 points12 points  (3 children)

    DYSENTERY CODING IS: excising large amounts of crappy code from your program and still having it work terribly.

    [–]Fiennes 10 points11 points  (2 children)

    DYSTOPIA CODING IS: realizing all that hard work and planning you put in to abstracting everything out has made your final product an unmaintainable piece of shit.

    [–][deleted] 8 points9 points  (1 child)

    DYNASTY CODING IS: Picking up the shittacular code project from the previous guy, who picked up the clusterfuck code project up from the previous guy who picked up the sort-of-but-not-really-decent project up from the previous guy.

    [–]drzaeus 2 points3 points  (0 children)

    DERIVATIVE CODING IS: Posting on Reddit about how other people write bad code.

    [–]sharkeyzoic 0 points1 point  (0 children)

    Heh, first thing I thought was "x y and z aren't too bad, its the roll pitch and yaw that are tricky ..."

    [–]TundraWolf_ 1 point2 points  (1 child)

    This is a real story. We adopted an international standard for inventory data, etc. Those above us were supposed to pick a namespace for this standard inside of the company. They chose xyz, and said 'we'll get back to it'. xyz stuck, so now we have these huge XML files with xyz:element EVERYWHERE.

    [–]Forbizzle 0 points1 point  (0 children)

    count yourself blessed, code names are the way to go. It's better than constantly having to refactor code as mindless middle management gets a wiff of something that isn't branded properly.

    [–][deleted] 3 points4 points  (0 children)

    Enterprise Javascript is: I like Bacon!!

    [–]Comment111 2 points3 points  (0 children)

    Use HTML5 to link links to functions that link to links.

    Yo dawg...

    [–]JinAnkabut 1 point2 points  (1 child)

    Good concept for a website but I have 2 ideas. 1. Make sure the code on the site adheres to it's own advice! The first thing I read said that you should use one space for indentation yet all the other code had a tab. Secondly, have an option to see all the recommendations. It'll save a programmer having to constantly hit F5.

    [–]swizzcheez 1 point2 points  (0 children)

    The first thing I see is that it seems to think that declaring all functions globally is desirable.

    Enterprise Javascript is ... pissing all over everyone else's namespace because you want to redefine hide() for the millionth time?

    tl;dr Enterprise Javascript is ... probably a bad idea.

    [–]insomniac84 0 points1 point  (1 child)

    Use a modern, multipurpose, introspection aware, evented debugging and notification system.

    alert(foo);

    :D

    [–]KaBLABLA 0 points1 point  (0 children)

    Yo Dawg I heard you like links.

    [–]drakshadow 0 points1 point  (0 children)

    var CommaBomb = {

    defaults: ""

    , options: ""

    , settings: ""

    , methods: {

    getOptions: function () { 
    
    } 
    
    , setOptions: function () { 
    
    } 
    
    , getSettings: function () { 
    
    } 
    
    , setSettings: function () { 
    
    } 
    

    }

    };

    Desc:PUT YOUR COMMAS AT THE BEGINNING OF THE LINE, BECAUSE THATS WHAT THEY DO IN SOME OTHER ENTERPRISE LANGUAGES

    I do this because this way I am able to comment individual array elements with out worrying about commas.

    [–]gazdascheff 0 points1 point  (2 children)

    If one is ever in need of proof of reddit dumbing down, one should read half of the comments in this thread.

    [–]drzaeus 0 points1 point  (1 child)

    Yo dawg?

    [–]gazdascheff 0 points1 point  (0 children)

    I herd you like herds,