Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
wwb
Aug 17, 2004

Debugging someone [long gone's] code:

code:
// should not be empty
if (entries.Count()> 1)
I wonder why I drink sometimes . . .

Adbot
ADBOT LOVES YOU

Bhaal
Jul 13, 2001
I ain't going down alone
Dr. Infant, MD
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;
                    }
?>
$argc is the element count in $decoratorInfo. I can't see why they'd do this instead of some form of "if( $argc > 0) {...}". The best I can figure is either this is the final form of some previous logic that actually benefitted from a switch, or it's a pity inspiring attempt at being forward thinking in case someone later wants to come extend the args.

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 :bang: this whole thing

php:
<?
foreach ($decorator as $name => $spec) {
                break;
            }
?>
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). :suicide:

And Zend is this supposed paragon of good php development.

Bhaal fucked around with this message at 21:39 on Nov 10, 2011

NotShadowStar
Sep 20, 2000

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). :suicide:

And Zend is this supposed paragon of good php development.

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 sane thing would this return? NULL
What does it actually return? 10

Welp.

baquerd
Jul 2, 2007

by FactsAreUseless

NotShadowStar posted:

php:
<?
function stupid_shit() {
for ($i = 0; $i <= 10; $i++) {
 $x = $i;
}

return $x
}
?>
What sane thing would this return? NULL
What does it actually return? 10

Welcome to dynamic scoping.

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

NotShadowStar posted:

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 sane thing would this return? NULL
What does it actually return? 10

Welp.
Depends on the language's scoping rules. That's the first thing you need understand when learning a new language, and bring over your preconceptions is only going to make it harder.

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

NotShadowStar posted:

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 sane thing would this return? NULL
What does it actually return? 10

Welp.

It's the same in python, this:

code:
for i in xrange(10):
    x = i
print x
prints 9. It's actually quite useful in a lot of situations, and if you understand the scoping rule it should never bite you in the arse.

Vanadium
Jan 8, 2005

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. :shobon:)

(0..10).each do |i|
  y = i
end

puts y
#=> undefined local variable or method `y'

raminasi
Jan 25, 2005

a last drink with no ice

Vanadium posted:

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. :shobon:)

(0..10).each do |i|
  y = i
end

puts y
#=> undefined local variable or method `y'

What's weird about this?

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
I don't know what's worse - that behavior or the fact that I was able to figure out why it exists

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

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.

raminasi
Jan 25, 2005

a last drink with no ice

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

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

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.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug
code:
if (someObject != null)
{    
     if (someObject != null) 
     {
       //code
     }
}
Someone wanted to be really really really really sure that it wasn't null.

Vanadium
Jan 8, 2005

GrumpyDoctor posted:

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.

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.

baquerd
Jul 2, 2007

by FactsAreUseless

Ithaqua posted:

code:
if (someObject != null)
{    
     if (someObject != null) 
     {
       //code
     }
}
Someone wanted to be really really really really sure that it wasn't null.

Bad multi-threaded design perhaps?

wwb
Aug 17, 2004

Ithaqua posted:

code:
if (someObject != null)
{    
     if (someObject != null) 
     {
       //code
     }
}
Someone wanted to be really really really really sure that it wasn't null.

That would make sense if there was a lock statement in there between the ifs. Guess that was too cool for school.

New Yorp New Yorp
Jul 18, 2003

Only in Kenya.
Pillbug

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.

raminasi
Jan 25, 2005

a last drink with no ice

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.

Vanadium
Jan 8, 2005

Lexical scoping!!

tef
May 30, 2004

-> some l-system crap ->

baquerd posted:

Welcome to dynamic scoping.

this isn't dynamic scoping.

NotShadowStar
Sep 20, 2000

Vanadium posted:

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. :shobon:)

(0..10).each do |i|
  y = i
end

puts y
#=> undefined local variable or method `y'

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 :catstare:

NotShadowStar fucked around with this message at 02:14 on Nov 11, 2011

crazyfish
Sep 19, 2002

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 :)

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

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.

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 :catstare:

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.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

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). :suicide:

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:
$myArray = array();
$myArray[4] = 6;
$myArray[30] = 'bob';
$myArray[16] = 'fart';
and now $myArray is an array with 3 elements, and the order of the elements is... what? No, they're not in the order 4 -> 6, 16 -> 'fart', 30 -> 'bob'; they're in insertion order.

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.

PDP-1
Oct 12, 2004

It's a beautiful day in the neighborhood.
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:
        void AddSomeNumbers()
        {
            short a = 1;
            short b = 2;
            short c = a + b;            // error: cannot implicitly convert type
                                        //'int' to 'short'

            short d = (short)(a + b);   // works, but goddamn aggravating
        }
e: tables

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

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.
code:
        void AddSomeNumbers()
        {
            short a = 1;
            short b = 2;
            short c = a + b;            // error: cannot implicitly convert type
                                        //'int' to 'short'

            short d = (short)(a + b);   // works, but goddamn aggravating
        }
e: tables

Java does this stupid poo poo too. :sigh:

Sedro
Dec 31, 2008

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.
In that case there's the possibility of an overflow so it increases the width of the word.

It would be really nice to have interfaces for numeric types and operators, though.
code:
public static T Add<T>(T first, T second)
    where T : INumeric
{
    return first + second;
}

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

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.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
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.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
Code that made me laugh:

code:
checkReqExp: function(value, reqExp){	
	if(value.match(reqExp)) {
		return validation.OK;
	}
	return validation.ERRORS.REQ_EXP_NO_MATCH;
}
It validates a value using a reqular expression.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Wheany posted:

Code that made me laugh:

code:
checkReqExp: function(value, reqExp){	
	if(value.match(reqExp)) {
		return validation.OK;
	}
	return validation.ERRORS.REQ_EXP_NO_MATCH;
}
It validates a value using a reqular expression.

Every time I see you post in this thread I thank the baby jesus that I am not you.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Wheany posted:

Code that made me laugh:

code:
checkReqExp: function(value, reqExp){	
	if(value.match(reqExp)) {
		return validation.OK;
	}
	return validation.ERRORS.REQ_EXP_NO_MATCH;
}
It validates a value using a reqular expression.

Besides the creative spelling, what the hell possesses people to write code like this?

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:
code:
semaphore = new System.Threading.Semaphore(0, 32000);
:stare:

Sinestro
Oct 31, 2010

The perfect day needs the perfect set of wheels.

dwazegek posted:

code:
semaphore = new System.Threading.Semaphore(0, 32000);
:stare:

That hurts to see. :suicide:

I bet most of it would be of a similar... quality/:wtc: level.

Beef
Jul 26, 2004
Could someone translate for a non-C# initiate?

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

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.)

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

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/:wtc: 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 :v: ). 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.

Zombywuf
Mar 29, 2008

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.)
That's a pretty weird description of a semaphore, it certainly describes a possible use case though. Then again I just read the MSDN docs and they're similarly weird. A semaphore is just an atomic counter that blocks if you decrement it below 0.

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.
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.

dwazegek
Feb 11, 2005

WE CAN USE THIS :byodood:

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 :wtf:", 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.

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->
*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.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply