8 ms·
Shit programmers write
- smikhanov 13y agoIf this: function nop() { if (Math.random() == 0.1234567890) { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } } wouldn't have been written in JavaScript, my guess would be that this is an attempt to create an empty function (no-op) and prevent it from being optimized away by the compiler. Still unclear who may need to call it.
- sbierwagen 13y agoLittle counterproductive to censor the submission title when the domain name is right next to it.
- mmahemoff 13y agoMore frequently updated: http://thedailywtf.com/ http://thedailywtf.com/
- flatline 13y agoA few of these are funny/wrong, but most just look like laziness or some minor confusion about language/framework specifics. Thedailywtf definitely has better content.
- danabramov 13y agoTo be more specific, http://thedailywtf.com/Series/CodeSOD.aspx http://thedailywtf.com/Series/CodeSOD.aspx has little snippets like these, but way funnier.
- deleted 13y ago[deleted]
- MattBearman 13y agoLove this one: return foo == null ? null : foo;
- wreegab 13y agoThis reminds me of this one, which is quite common: return foo == null ? true : false;
- Leszek 13y agoIt's better than return foo ? true : false; or even return foo == true ? true : false;
- deleted 13y ago[deleted]
- wreegab 13y agoSo transposing a boolean into a boolean is better than transposing a truthy value of an unspecified type into a boolean? You are missing the point, which is it's really silly to transpose a boolean into a boolean using a ternary operator. If you want to argue that "foo == null" is the problem due implicit nil value falseness, than the solution is simply to use strict comparison operator "===", transposing "foo == null" into a boolean doesn't solve the falseness issue if you think this is what the original author was trying to address.
- adamors 13y agoI sometimes do that in PHP because I hate the equivalence between "", false, null, '0' and 0. Seeing true and false written out makes it more readable.
- hrktb 13y ago> if (Session["startDate"] + "" == "") This feels horribly familiar. I'm not sure to recognize the specific language used, but there must be cases where one would like to target empty of filled with non processable characters strings only, and let null and falsy values pass through. This kind of use would typically need a line of comment, but hey...
- goldenkey 13y agoI'm guessing you don't realize that the plus operator has less precedence than the == operator. That if is equivalent to if (startDate + true) EDIT: I was wrong. I haven't slept.
- danielweber 13y ago
- tkellogg 13y agoA lot of these seem to be Microsoft technologies like C# or SQL server. Is that because the owner is more familiar with the MS stack? Or do MS devs have more to complain about?
- danabramov 13y agoFrom my experience, such code is not specific to MS technologies, but is indeed often found in “enterprisey” code, a lot of which just happens to be in Java and C#. And enterprisey code is like that because it's often outsourced to the lowest bidder. I once had to print a method that took 50 pages of paper, so I could understand what it does, and after 15 minutes I realized it's the same 60 lines repeated over and over with different conditions. The guy who wrote it had no idea he could have extracted a method.
- guardian5x 13y agoJust curious: Why does printing it on paper helps you to understand code better than reading it on screen?
- zentiggr 13y agoEvery once in a while you find code whose control structures are wrapped around a ton of working code... paging up and down to try to grok the entire structure pagefaults my short term memory badly enough that having it all visible at once is the only workaround. Sometimes, if you're really lucky, the blocks of working code don't modify their own conditionals, and you can 'fold' them and just look at the if's and loops...
- pseut 13y agoFor me, the ability to spread the paper across a table and look at many different parts of the code simultaneously. It's like having a giant high resolution monitor. Also, faster and more flexible annotation (drawings, etc), but that's secondary.
- MartinCron 13y ago
- deleted 13y ago[deleted]
- collyw 13y agoA lot of these seem to be attempts to fit into existing "best practices". Unit tests that prove nothing. Boilerplate Java code that does nothing. Maybe we should reevaluate some best practices. I have debated before on here that many unit tests seem useless as the units are too small, and you essentially end up testing your language or framework which you already know works. Integration testing on the other hand makes a lot more sens. When I started with Java, it seemed appropriate to put hundreds of getter / setter methods. Is there much advantage to that over allowing the variable to be accessed directly? (Perl felt very strange at first having getter and setter methods combined as one method). As I get more experienced as a developer, my skill set has grown but my coding becomes simpler. Don't use every language feature to show how knowledgeable you are. Use it when appropriate, and when it makes the code more readable / reusable / simpler. Sometimes a higher level abstraction is more difficult to understand , but is overall better choice. An example would be a map as opposed to a for loop. Less chance of side effects in a map, as we don't have to track the iterator variable, but a map is not as intuitive for less experienced coders.
- mbillie1 13y agoThe fact that you can write bad unit tests is orthogonal to the notion that writing unit tests is, in general, a good thing to do.
- sanderjd 13y agoI always feel happiest writing unit tests for functions that contain non-trivial logic. But I always feel sad when testing methods that mostly just introduce side effects. It seems like when I'm running into more of the latter, it is often worthwhile to find ways to refactor toward having more of the former, but sometimes not.
- wlievens 13y agoTrue. I find unit tests the most useful and economical when I'm testing algorithms, rather than data bookkeeping or user interfaces.
- reginaldjcooper 13y ago
- JoeAltmaier 13y agoThe one about if (condition) {...} else if (!condition) ... May actually have a point, if 'condition' is a boolean which can be true or false or undefined.
- pron 13y agoI wonder how much of that is due to problems in documentation/documentation discoverability. This seems precisely the kind of data that we need to make docs better.
- npsimons 13y agoOr, you know, write programming languages that don't suck. See also https://www.destroyallsoftware.com/talks/wat https://www.destroyallsoftware.com/talks/wat
- fdej 13y agoif (Math.random() == 0.1234567890) { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } Reminds me of the code I have to insert to prevent overzealous compilers from completely optimizing away my benchmark loops.
- ep103 13y agopublic bool ShowOptional() { bool bolReturn = false; return bolReturn; } The above actually seems reasonable if you assume the programmer that wrote it was intelligent. I'm imagining that the above code is called multiple times by the application, for a new optional feature in development. Currently, they haven't developed the feature, and so we are always returning false. But in the future, we likely will want to show the feature given some condition. So this programmer has (hopefully) decided that (s)he will write the current code to take this future development into account, and rather than pass some boolean or config value throughout the code, has isolated it to one specific method. Now, when they go to implement Optional, whatever that is, that developer can just update this one method with the expression, and go about coding their feature without any knowledge of the previous code base, which is exactly what's supposed to happen.
- michael_h 13y agoWhy have a variable then? return false; Done.
- pmelendez 13y agoI used to write this kind of snippet when creating the skeleton of a function, this seems like a leftover that was over-sighted in the code review (assuming that there was a code review at all)
- pc86 13y agoIf that’s the case shouldn’t they have just used public bool ShowOptional() { return false; } ?
- pmelendez 13y agoI haven't slept well lastnight (only 4 hours) so please bear with me... but what is the joke on this snippet? "I Don’t Know".ToJson(); public string ToJson() { var s = new StringBuilder("{"); for (var i = 0; i < CustomField.CustomFieldOption.Count; i++) { var item = CustomField.CustomFieldOption[i]; s.Append("\"" + item.CustomFieldOptionId + "\""); s.Append(":\"" + item.OptionName + "\""); if (i < CustomField.CustomFieldOption.Count - 1) { s.Append(","); } } s.Append("}"); return s.ToString(); }
- mephi5t0 13y agoI assume item itself is missing, so nothing to convert... My guess
- deleted 13y ago[deleted]
- pmelendez 13y agoI guess you are talking about: s.ToString(); Just note that s is a StringBuilder and not a string.
- potatolicious 13y agoBeen a while but: var item = CustomField.CustomFieldOption[i]; Shouldn't that be the item to convert? This looks like something stuck onto a class to serialize to JSON. I might be missing the joke also :(
- acron0 13y agoThere aren't any obvious structural errors but two things occur to me. Firstly, JSON serialization is something every platform, including .NET, already has a multitude of libraries available for so this is 'reinventing the wheel' a bit. Secondly, looking at what it's actually doing, it's plausible the author doesn't even need JSON, as it's simply serializing a list of `id:name` fields. Thirdly, there are plenty of more 'inline' ways of achieving this using `string.Join`. All in all, just very 'novice' code but nothing _incorrect_ about it. IMO.
- jtheory 13y agoFor the "Siamese HTML Document" -- that one rings a bell... I swear there was a bug in one of the ancient horrible versions of IE that required you to include your "no cache" tags AGAIN in a second head tag, if you wanted to use http-equiv tags instead of real HTTP headers. I'm pretty sure there hasn't been reason for anyone to do that for a decade or so, though.
- walshemj 13y agoOh I thought the shit programmers write was stuff like "its; 90% complete" or "thats not a bug its a feature"
- slaman 13y agoIs anyone else as infuriated as I am that you can't actually read any of the code? Maybe I'm missing a button somewhere, but I needed to open up the DOM inspector or RSS feed to read every single one. Unacceptable