|
Debugging someone [long gone's] code:code:
|
# ? Nov 9, 2011 02:50 |
|
|
# ? May 14, 2024 21:32 |
|
Came across this in the bowels of the Zend framework (form decorators):php:<? switch (true) { case (0 == $argc): break; case (1 <= $argc): $decorator = array_shift($decoratorInfo); case (2 <= $argc): $options = array_shift($decoratorInfo); default: $this->addDecorator($decorator, $options); break; } ?> Actually, it might be that someone doesn't quite understand switch. $options is predeclared as an empty array which seems like they're expecting when $argc = 1 that the second array_shift will get skipped. The thing is array_shift on an empty array returns null, which downstream it looks like their code treats null the same as an empty array so it winds up working anyway. :php: EDIT: Oh god this whole thing php:<? foreach ($decorator as $name => $spec) { break; } ?> And Zend is this supposed paragon of good php development. Bhaal fucked around with this message at 21:39 on Nov 10, 2011 |
# ? Nov 10, 2011 21:25 |
|
Bhaal posted:This piece of work relies on php not giving a poo poo about block scope and goes on to do stuff with $name and $spec. So what it's basically doing is grabbing the first "element" in an array, but doing so by the array's internal cardinality in case it's using associative keys (it is). This reminds me of a fun thing I stumbled upon the other day. php:<? function stupid_shit() { for ($i = 0; $i <= 10; $i++) { $x = $i; } return $x } ?> What does it actually return? 10 Welp.
|
# ? Nov 10, 2011 23:01 |
|
NotShadowStar posted:
Welcome to dynamic scoping.
|
# ? Nov 10, 2011 23:05 |
|
NotShadowStar posted:This reminds me of a fun thing I stumbled upon the other day.
|
# ? Nov 10, 2011 23:25 |
|
NotShadowStar posted:This reminds me of a fun thing I stumbled upon the other day. It's the same in python, this: code:
|
# ? Nov 10, 2011 23:38 |
|
Ruby does this too! Sometimes! for i in 0..10 do x = i end puts x #=> 10 (i is also still in scope at this point. ) (0..10).each do |i| y = i end puts y #=> undefined local variable or method `y'
|
# ? Nov 10, 2011 23:51 |
|
Vanadium posted:Ruby does this too! Sometimes! What's weird about this?
|
# ? Nov 10, 2011 23:54 |
|
I don't know what's worse - that behavior or the fact that I was able to figure out why it exists
|
# ? Nov 10, 2011 23:55 |
|
GrumpyDoctor posted:What's weird about this? I've never touched ruby, but it seems inconsistent from the syntax that x is still in scope and y isn't.
|
# ? Nov 11, 2011 00:01 |
|
MEAT TREAT posted:I've never touched ruby, but it seems inconsistent from the syntax that x is still in scope and y isn't. Roughly, .each operates on a new closure. e: I'm not a Serious Ruby Developer but I was under the impression that the particular distinction in question is a huge part of the Ruby way of doing things, hence my confusion at the confusion. raminasi fucked around with this message at 00:07 on Nov 11, 2011 |
# ? Nov 11, 2011 00:05 |
|
Dessert Rose posted:I don't know what's worse - that behavior or the fact that I was able to figure out why it exists I don't see why the behaviour is bad in the first place, provided its applied in a way consistent with the rest of the language? It's often necessary to use the last number or line of a file or element of a list processed or whatever, especially if it's a search and you're breaking out when you've found what you're looking for or whatever. This is a terse, neat and - unless you're trying very hard - clear way of doing it. e: oh sorry thought you were talking about the whole behaviour and not just the ruby thing.
|
# ? Nov 11, 2011 00:08 |
|
code:
|
# ? Nov 11, 2011 00:21 |
|
GrumpyDoctor posted:Roughly, .each operates on a new closure. I'm not a ruby expert, but it's weird to me because I figured the whole block syntax sugar for closures was designed to let people write write their own "control structures", yet they didn't go the whole way of making local variables of the closure act like variables introduced in the scope of a real control structure, ie. as local varibles of the enclosing method.
|
# ? Nov 11, 2011 00:22 |
|
Ithaqua posted:
Bad multi-threaded design perhaps?
|
# ? Nov 11, 2011 00:31 |
|
Ithaqua posted:
That would make sense if there was a lock statement in there between the ifs. Guess that was too cool for school.
|
# ? Nov 11, 2011 00:41 |
|
wwb posted:That would make sense if there was a lock statement in there between the ifs. Guess that was too cool for school. There was absolutely no threading involved, nor the possibility of concurrent access to the object. Just lovely, careless developers.
|
# ? Nov 11, 2011 00:43 |
|
Vanadium posted:I'm not a ruby expert, but it's weird to me because I figured the whole block syntax sugar for closures was designed to let people write write their own "control structures", yet they didn't go the whole way of making local variables of the closure act like variables introduced in the scope of a real control structure, ie. as local varibles of the enclosing method. I think a "closure" that leaked its variables into the scope of whatever defined it would be far more appropriate for this thread than what Ruby does now.
|
# ? Nov 11, 2011 00:45 |
|
Lexical scoping!!
|
# ? Nov 11, 2011 01:13 |
|
baquerd posted:Welcome to dynamic scoping. this isn't dynamic scoping.
|
# ? Nov 11, 2011 01:14 |
|
Vanadium posted:Ruby does this too! Sometimes! Weird, I've been doing ruby for like seven years and I never noticed this artifact. I'll have to ask some of the supremo guru people wtf is going on here. I was under the assumption that 'for x in y' simply called the .each method. e: for x in y DOES call the .each method, but it doesn't create a new scope. This is... not ruby-like. ee: horror of horrors, holy poo poo Matz, the Ruby language creator, is a Mormon NotShadowStar fucked around with this message at 02:14 on Nov 11, 2011 |
# ? Nov 11, 2011 02:10 |
|
wwb posted:That would make sense if there was a lock statement in there between the ifs. Guess that was too cool for school. That, though it would only actually be correct if someObject was marked volatile
|
# ? Nov 11, 2011 02:52 |
|
NotShadowStar posted:Weird, I've been doing ruby for like seven years and I never noticed this artifact. I'll have to ask some of the supremo guru people wtf is going on here. I was under the assumption that 'for x in y' simply called the .each method. I feel like there's some consistency here, and it's that the scoping changes depending on whether it's a keyword or a method call. Local variables in blocks given to keywords (for, if, while) "leak out", and method calls (each) don't. Not defending the decision, as I agree it doesn't feel very Rubyesque, but at least some thought seems to have gone into it.
|
# ? Nov 11, 2011 03:48 |
|
Bhaal posted:This piece of work relies on php not giving a poo poo about block scope and goes on to do stuff with $name and $spec. So what it's basically doing is grabbing the first "element" in an array, but doing so by the array's internal cardinality in case it's using associative keys (it is). This is something about PHP that's really inelegant and annoying and is a much better target for people's irritation than the fact that PHP has function-level rather than block-level scope. PHP arrays are associative arrays with strings as keys, except that you don't have to supply keys if you don't want and then PHP automatically chooses suitable integers to use for keys. But the key-value pairs of course are in a certain order, namely the order they were put in the array. So you can do code:
A more sensible way to do things would probably be to have two different collection datatypes - numerically-indexed arrays whose keys must be 0, 1, 2, ..., n for some n, and associative arrays where you must supply keys for the elements. But PHP.
|
# ? Nov 11, 2011 03:53 |
|
Goddammit C#, primitive types should get their own operators. Adding bytes to bytes or shorts to shorts has a meaningful, expected outcome. Constant manual casting back to the type you wanted after everything silently converts to int for every operation is loving annoying.code:
|
# ? Nov 11, 2011 04:04 |
|
PDP-1 posted:Goddammit C#, primitive types should get their own operators. Adding bytes to bytes or shorts to shorts has a meaningful, expected outcome. Constant manual casting back to the type you wanted after everything silently converts to int for every operation is loving annoying. Java does this stupid poo poo too.
|
# ? Nov 11, 2011 04:31 |
|
PDP-1 posted:Goddammit C#, primitive types should get their own operators. Adding bytes to bytes or shorts to shorts has a meaningful, expected outcome. Constant manual casting back to the type you wanted after everything silently converts to int for every operation is loving annoying. It would be really nice to have interfaces for numeric types and operators, though. code:
|
# ? Nov 11, 2011 06:57 |
|
Sedro posted:In that case there's the possibility of an overflow so it increases the width of the word. int + int is typed as int, long + long is typed as long, and similarly for the unsigned variants. There literally is no "short + short" operator. What happens is that both operands are converted to int, and then the integer + operator is used.
|
# ? Nov 11, 2011 07:12 |
|
The argument for promotion here is that people tend to only use arithmetic on byte or short when they're using them as small integer types, and in those cases you're probably going to be really annoyed by the arithmetic wrapping at a tiny width — e.g. if you were averaging three bytes (useful in certain kinds of image processing). This is then further complicated by the type of integer literals generally not being overloaded — you want to say that 4 is an int, but that means that someByte + 4 is an int instead of a byte. I'm not sure I accept this argument, but there you have it.
|
# ? Nov 11, 2011 08:55 |
|
Code that made me laugh:code:
|
# ? Nov 11, 2011 15:31 |
|
Wheany posted:Code that made me laugh: Every time I see you post in this thread I thank the baby jesus that I am not you.
|
# ? Nov 11, 2011 15:51 |
|
Wheany posted:Code that made me laugh: Besides the creative spelling, what the hell possesses people to write code like this?
|
# ? Nov 11, 2011 17:00 |
|
code:
|
# ? Nov 14, 2011 11:30 |
|
dwazegek posted:
That hurts to see. I bet most of it would be of a similar... quality/ level.
|
# ? Nov 14, 2011 12:12 |
|
Could someone translate for a non-C# initiate?
|
# ? Nov 14, 2011 12:26 |
|
Beef posted:Could someone translate for a non-C# initiate? It's mostly a "what the gently caress would you be doing with 32k threads" issue. A semaphore limits the number of threads that can access a resource or resource pool concurrently. 32k is an enormous number of threads - on my machine, explorer.exe is using 32 threads (you can check in task manager what apps are using how many threads.)
|
# ? Nov 14, 2011 12:53 |
|
hieronymus posted:It's mostly a "what the gently caress would you be doing with 32k threads" issue. A semaphore limits the number of threads that can access a resource or resource pool concurrently. 32k is an enormous number of threads - on my machine, explorer.exe is using 32 threads (you can check in task manager what apps are using how many threads.) In this case, they're using it as a sort of queuing mechanism. Thread A runs a loop where it waits on the Semaphore and then does dome other work, Thread B then uses Release to control how often Thread A loops. Sinestro posted:I bet most of it would be of a similar... quality/ level. From what I saw, the rest isn't that bad, there's a couple of unused members, and the occasional resource that isn't Disposed properly (but at least the GC takes care of those ). Still WTFs, just not particularly interesting ones. And after rereading it, that Semaphore is mostly just an ugly hack to fix a really unlikely (but possible) problem. They should've just fixed the overall design so that the problem can't occur, but, for the most part, that fix does solve it as well. Of course, a bit of documentation on what the gently caress they were hoping to achieve would've been useful.
|
# ? Nov 14, 2011 13:18 |
|
hieronymus posted:It's mostly a "what the gently caress would you be doing with 32k threads" issue. A semaphore limits the number of threads that can access a resource or resource pool concurrently. 32k is an enormous number of threads - on my machine, explorer.exe is using 32 threads (you can check in task manager what apps are using how many threads.) dwazegek posted:In this case, they're using it as a sort of queuing mechanism. Thread A runs a loop where it waits on the Semaphore and then does dome other work, Thread B then uses Release to control how often Thread A loops.
|
# ? Nov 14, 2011 13:31 |
|
Zombywuf posted:Assuming the semaphore has an efficient implementation this sounds like a perfectly good use of a semaphore. Although the hardcoded max suggests it's a bit more horrific than this description gives credit for. Yeah, the use of the semaphore is fine. My knee-jerk reaction was "32000 threads ", but I spoke too soon and that's really not the case here. 32000 is just a bullshit number that was deemed "high enough" to work around an existing problem in their design. Instead of just fixing the problem in the first place.
|
# ? Nov 14, 2011 14:55 |
|
|
# ? May 14, 2024 21:32 |
|
*producer puts thing on shared queue* *releases semaphore* *producer spins until it can acquire semaphore, reads queue* if you're using mutexes, condition variables or semaphores it is likely you're doing something wrong - there should be some nice concurrent data structure like a queue you can use instead. it will probably have less bugs than the homebrew queue.
|
# ? Nov 14, 2011 15:10 |