|
(I apologize for the length of this post, but you really have to see this in all its glory.) Ladies and gentlemen, I give you The Invoice Print File Join Method. It starts out reasonable enough. There are 15 checkboxes that determine what types of invoices to print. Therefore there could be up to 15 files that have already been written to disk. So obviously we need 15 StreamReaders and 15 ArrayLists. code:
code:
code:
The following code ensues for each file, i.e. 15 times: code:
Now we have to find the iChoosen file: code:
We round things off with a nice, simple if-else chain. code:
|
# ¿ Aug 18, 2011 08:56 |
|
|
# ¿ May 13, 2024 05:16 |
|
xf86enodev posted:The official library reference suggests the use of constants or magic numbers is correct, so I guess Mark just wanted to save bandwidth. He was probably referring to the use of 2 instead of os.SEEK_END. The python.org example only uses a magic number for the number of bytes to read, not for the seek mode. The number 128 in the Dive Into Python example is also something that could be put in a constant like MP3_TAG_SIZE or whatever, but that isn't a constant you can expect Python itself to know about.
|
# ¿ Aug 22, 2011 08:54 |
|
Vaguely on the subject of "input" in Python: One time a coworker of mine wrote code that essentially did this:code:
|
# ¿ Jan 18, 2012 08:22 |
|
Lysidas posted:What are you talking about? That wasn't very clear, sorry. Yes, I'm referring to the fact that input always exists because it's a built-in, and do_a_thing() should've been called function_that_never_returns_false().
|
# ¿ Jan 18, 2012 14:46 |
|
tef posted:python Python operators are kind of weird in general (I think they changed this behavior for Python 3): code:
|
# ¿ Jan 19, 2012 19:43 |
|
The Gripper posted:Why is this? It's almost like the parenthesis changes it from a value of True to a reference to True. Similar weirdness with: code:
|
# ¿ Jan 19, 2012 19:58 |
|
ToxicFrog posted:Here's the bit that confuses me: Yes, False == 0. I'll repeat myself from the previous page: code:
LOOK I AM A TURTLE fucked around with this message at 07:15 on Jan 20, 2012 |
# ¿ Jan 19, 2012 21:25 |
|
Doh004 posted:Found this today while trying to determine a specific sector in some other team's solution: I blame stuff like this partly on the relative difficulty of doing little ad-hoc checks for membership in some constant list in C#. In Python, even your average terrible programmer can figure out how to do this: Python code:
C# code:
|
# ¿ May 31, 2012 16:44 |
|
epswing posted:Correct me if I'm wrong but the codes should be in a hash-based container like a Set or a Dictionary, making the 'contains' lookup O(1) instead of O(n). You're not wrong, although I'm sure you'd only feel the difference if you were executing the function many, many times per second.
|
# ¿ May 31, 2012 17:44 |
|
In fairness, you are their first ever account.
|
# ¿ Jun 13, 2012 12:53 |
|
God drat do I hate "single point of exit" as a design philosophy. It's the cause of conditional blocks the size of skyscrapers that are impossible to parse when all you need is if (gently caress == null) return false; Granted, this only happens if the code is also full of massive functions. It's probably okay to use single point of exit if you manage to keep functions small, and it's probably (somewhat) okay to make a lot of long functions if you can make them readable, but combine single point of exit with behemoth functions and you have planted the seeds of despair.
|
# ¿ Jun 20, 2012 14:04 |
|
xf86enodev posted:Posting C/C++ code should just crash your browser. In the future, probations will be automatically handed out for posts that don't follow PEP8.
|
# ¿ Jun 21, 2012 06:17 |
|
carry on then posted:Now I want to write a language where a side effect of the addition operator is that a file on your hard drive is chosen at random and deleted. Just to see what apologists would come up with. No one is forcing you to use the addition operator. Instead of 1 + 2 you can simply do 1 - -2 instead.
|
# ¿ Sep 2, 2012 19:10 |
|
Deus Rex posted:ignoring the other issues, I don't think it's a big deal to iterate over an associative array like that even if the keys will never be used in the loop body. better to make it obvious to later maintainers that the array is associative than to go for the most brevity, IMO Aren't PHP "arrays" always associative?
|
# ¿ Oct 4, 2012 10:17 |
|
I don't think I've ever manually built XML like that, unless you count HTML. But recently I ported (from VB6 to C#) a part of an application that manually generates a particular type of EDI files, and I didn't change the approach at all. My code is therefore full of crap like this:C# code:
|
# ¿ Nov 22, 2012 09:40 |
|
pigdog posted:Same thing for anything code-related. It makes better sense to offer the beginners concrete and hard rules, which they may grow out of, rather than trying to explain the intrinsics of electricity to a 2-year-old. There are a lot more 2-year-olds doing programming than electrical work, though.
|
# ¿ Nov 26, 2012 09:36 |
|
bobthecheese posted:
This sounds not so much like a horror as a sadly neglected coding masterstroke.
|
# ¿ Jan 11, 2013 11:40 |
|
The way it pulls the proj_agg_duration function dynamically from the "magic_stats" module is needlessly complex if the pasted code is all there's to it, but when I look at it it makes me think there probably is or has been some alternative way for the "magic_stats" module to be loaded from somewhere else depending on some branching logic further up. If the developer straight up doesn't know that you can do magic_stats.proj_agg_duration(...) then yeah, that's a horror.
|
# ¿ Feb 4, 2013 13:42 |
|
We had a bug in our Easter function recently. It didn't work too well if you gave it 0 as input. (Not really a horror or anything. The bug was just that 0 was being passed in the first place.) But our stupid Hungarian notation, which is ugly on the best of days, is particularly foul here.C# code:
|
# ¿ Feb 15, 2013 08:16 |
|
Not sure what the problem is here, flase and treu are perfectly valid synonyms for false and true.
|
# ¿ Mar 14, 2013 11:40 |
|
Rothon posted:The *Impl "pattern" is also used in Java to help out with unit testing. The *Impl class isn't actually the only implementation: tests define mock implementations and use those to avoid dependencies on other parts of the application. You need the interface because the mock libraries use java.lang.reflect.Proxy under the hood which can only implement interfaces, not extend classes. It's the same with .NET. For instance, the popular mocking library Moq can only mock interfaces and virtual methods on non-sealed classes, because it uses the proxy library Castle DynamicProxy, which has that same limitation. I know that TypeMock's Isolator and Microsoft's own Fakes framework can mock almost anything, but they cost a lot of money. I hate making 1-to-1 class/interface pairs where the odds that I'll ever need to make a second implementation of the interface are basically zero, but the alternative is to make every public method on the class virtual, which is no better, and which is more likely to be forgotten if new methods are added, and also leads to more complexity in the mocking code. And of course every interface will be named stuff like IClassName, since that's exactly what they are. It's got nothing to do with "programming by interface", really; it's purely a technique for mocking. But it's very annoying to have to do it.
|
# ¿ Apr 3, 2013 08:12 |
|
Suspicious Dish posted:Why do they think changing for-loops to while-loops will do anything? I'm also curious to learn what they mean by "and so forth" in that same parenthetical. Should we also be changing if-statements to gotos?
|
# ¿ Apr 4, 2013 14:14 |
|
What's the most indented line of code you've ever seen? I've got one that's 19 levels deep, which with 4 spaces per tab means that the line in question begins at column 77. I don't really care about the old 79-chars-per-line rule when doing C#, but you know something is wrong when you're practically already there before you've even written anything. I won't show the actual method since it's 777 lines long, but here's what the hierarchy looks like: code:
C# code:
|
# ¿ Jun 13, 2013 07:42 |
|
Ithaqua posted:That's silly, especially if you're using MStest when you can just decorate the test method with [ExpectedException(typeof(SomeException))] The problem with ExpectedException is that you can't distinguish between an exception in your test code and an exception in the code under test. You can end up with tests that pass even though they never even get to the actual test part of the test, because your test setup happens to throw the same exception that you were asserting on. Okay, it's not particularly likely, but the day it happens to you you'll be none the wiser. I also view it as a bad thing that asserting on exceptions is so structurally different from asserting on values, which is why I always use this little snippet instead of ExpectedException: C# code:
C# code:
LOOK I AM A TURTLE fucked around with this message at 11:09 on Jul 12, 2013 |
# ¿ Jul 12, 2013 10:28 |
|
Crosscontaminant posted:I wrote this. I'm sorry. Python code:
|
# ¿ Aug 20, 2013 11:47 |
|
nielsm posted:
You win this round, nielsm.
|
# ¿ Aug 20, 2013 11:59 |
|
Protocol7 posted:Contrary to popular belief: Possible explanations: 1. "I was going to fill it in later." 2. "I couldn't figure out what to assert on." 3. "It's better than nothing."
|
# ¿ Oct 7, 2013 22:44 |
|
Eric Lippert, one of the designers of C#, has mentioned on many occasions that he regrets that Nullable<T> can only be applied to value types, as opposed to all types. He has also confirmed that the reason is basically that the features -- i.e. 1. Nullable types and 2. The distinction between reference types and value types -- entered the language in the wrong order. From a theoretical point of view the biggest problem is that it prevents the Nullable<T> type from being truly monadic, since you can't have a Nullable<Nullable<T>>. This series of posts touches on it: http://ericlippert.com/category/monads/
|
# ¿ Oct 10, 2013 09:49 |
|
Maybe at first he used a list, then someone told him he should use a hashtable instead, and, well...
|
# ¿ Oct 17, 2013 07:56 |
|
SavageMessiah posted:All this is moot on ~*Oracle*~ because '' IS NULL there... I've actually come to appreciate this about Oracle. 9 out of 10 times the distinction between null and '' is of no interest to my business logic anyway, so I like not having to worry about which one I put in the database. The only thing I hate is when I forget that col = '' doesn't work (you have to do col IS NULL). Even '' = '' returns false.
|
# ¿ Nov 13, 2013 21:39 |
|
The Laplace Demon posted:The code in all it's glory. Leaving aside the quadruple pointer, the most infuriating thing about this is the attitude of "hey guys my code don't work! here it is whats wrong?"
|
# ¿ Mar 17, 2014 09:12 |
|
I don't know, guys. Doubling my balance sounds like a great idea!
|
# ¿ Mar 28, 2014 11:59 |
|
Howmuch posted:I'm working on a big project at work and noticed a variation of this being used in the code in multiple places. This is probably being done to facilitate unit testing. I think it's usually overkill, personally, but there are cases where it makes sense. Suppose for example that you have a method with a test like if (DateTime.Today.DayOfWeek == DayOfWeek.Monday) { ... }. How would you write a unit test for that if you can't override the value of DateTime.Today? Run it only on Mondays? There are a number of ways to deal with this issue, one of which is to hide DateTime.Now behind an interface and inject a mocked implementation in unit tests. Now, if they're doing this but there are no actual unit tests to take advantage of it, then that's certainly the worst of both worlds.
|
# ¿ Mar 29, 2014 11:34 |
|
Howmuch posted:Nope. Probably a cargo cult thing, then. Or an "I'll definitely write tests for this. Later." thing.
|
# ¿ Mar 29, 2014 12:46 |
|
Let me see if I understand this. (.) . (.) takes a function that's missing exactly two parameters and composes it with another function that expects the return type of the first function as its first input parameter. Is that right?code:
code:
code:
AlsoD posted:Somebody learnt to program in Forth clearly That is... not clearer to me.
|
# ¿ Apr 1, 2014 12:55 |
|
Bongo Bill posted:There are even fewer men in computers than women because men are larger. Men in computers tend to be even larger than average. Makes you think.
|
# ¿ Apr 10, 2014 14:21 |
|
Verloc posted:After diving a little deeper, I've come to the conclusion that about 1/3 of the silent catches are legit poo poo-that-should-be-caught stuff. The rest seem to mostly be a byproduct of the original author not really getting how nullables work. About 190 of those catches are basically flow control statements as written by a moron. Instead of null-checking any nullable value that he passes into code that will eat poo poo spectacularly if handed a null(formatted datetimes, ternary operators, string splits, property accessors, etc. etc.), everything is just wrapped in try/catch blocks and if said null intolerant code gets a null and buttfucks itself out of existence, just keep on' truckin'. I'm still grappling with the fact that almost a hundred silent catches on legit exceptions qualifies as 'a drastic improvement over what I initially assessed' however. That's awful. It's a painful thing to do, but you could consider replacing them with try { ... } catch (NullReferenceException) { }, so at least they only catch what they're supposed to catch. Clearly it would be better to make sure the exceptions don't happen to begin with, but from the sound of it that's going to be a challenge.
|
# ¿ Apr 26, 2014 13:17 |
|
rjmccall posted:I did describe it as the end point of a long series of questionable decisions. I will happily concede that the chief mistake is making an expression-language. Making an imperative expression language with braces and semicolons is just a refinement of that mistake. I haven't heard the term "expression language" used in this sense before. Are you referring to languages that have only expression and no statements? Or languages where every code unit has to return a value? "Imperative expression language" is an oxymoron to my uniformed self.
|
# ¿ Apr 29, 2014 07:50 |
|
"Modify" for an operation that actually returns a modified copy instead of mutating the original object is slightly weird, but is somewhat reasonable when it's used in the context of a guaranteed immutable object. Mutation and modification are not the same thing. I'm also inclined to buy the explanation about compatibility with the old API. With all this in mind, I'm going to give PHP as pass on thiSedro posted:Except sometimes it returns false
|
# ¿ Jul 4, 2014 09:02 |
|
|
# ¿ May 13, 2024 05:16 |
|
Would -Why explain the reasoning and justification behind obscure pieces of code? If it does I'd like to apply it to this if-statement (named changed slightly):C# code:
|
# ¿ Oct 29, 2014 09:47 |