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
nielsm
Jun 1, 2009



Jabor posted:

It's the whole lambda scoping thing. Lambdas close over variables, not values.

Evil, but I see how it kinda makes sense.

For whatever reason it didn't catch my eye at first, why use
return BuildAction(wrapperAction, ++i);
instead of
return BuildAction(wrapperAction, i+1);
? I mean, you'd expect i to go out of scope after that so why even bother changing its value. Except it does stay in a scope :eek: That should have raised a flag.

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

nielsm posted:

Evil, but I see how it kinda makes sense.

It allows you to have persistent, modifiable state in a lambda, which you you either can't or it's-a-real-pain-to-do otherwise. The obvious "toy example" is a lambda which tracks how many times it's been called, but there are some actual useful uses for it. For example, we could implement a PRNG as so (largely psuedocode):

code:
newGenerator(int seed)
  return function() {
    seed = (seed * P) % n;
    return seed;
  }
Whether it's a good idea to have it as the default (closing over values by default and requiring a keyword such as outer to access a persistently-scoped variable might perhaps be more intuitive), it's pretty common to anonymous functions across all languages and should really only catch you out once.

Fehler
Dec 14, 2004

.
code:
typedef double real;
const real two = (real)2;
const real pi = 3.141592653589793;
This project is gonna be fun!

Scaevolus
Apr 16, 2007

Fehler posted:

code:
typedef double real;
const real two = (real)2;
const real pi = 3.141592653589793;
This project is gonna be fun!
Looks like a FORTRAN programmer has been forced to write C, and is resisting.

Jerry Bindle
May 16, 2003

Fehler posted:

code:
typedef double real;
const real two = (real)2;
const real pi = 3.141592653589793;
This project is gonna be fun!

Defining '2' as a constant has a legitimate use for static analysis tools, further mo- haha I'm just playing. Why would anyone do that? Is there a language where you have to do stuff like this?

My work wants me to write my own USB stack so that I can learn usb inside and out, so far its 2kB of coding horror, could post the whole thing :(

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
Well poo poo, and here I thought reinventing the wheel was over after your undergrad. At least I hope you don't have to do it on 30 year old development boards.

BigRedDot
Mar 6, 2008

Ensign Expendable posted:

Well poo poo, and here I thought reinventing the wheel was over after your undergrad.
Are you kidding? Some folks specifically want 13 sided wheels, instead of the 16 sided ones the last guy made, and who the hell are you?

Jerry Bindle
May 16, 2003

BigRedDot posted:

Are you kidding? Some folks specifically want 13 sided wheels, instead of the 16 sided ones the last guy made, and who the hell are you?

Seriously, I hugely prefer making my own wheel for a few months before I have to work on the lumpy rear end-backwards wheel that exists.

BigRedDot
Mar 6, 2008

dietary supplement posted:

Seriously, I hugely prefer making my own wheel for a few months before I have to work on the lumpy rear end-backwards wheel that exists.
Now I really wish I had the photoshop skills to make an image of a wheel with a bunch of backwards asses protruding from it.

Jerry Bindle
May 16, 2003

BigRedDot posted:

Now I really wish I had the photoshop skills to make an image of a wheel with a bunch of backwards asses protruding from it.

You are posting on an internet forum about computer codes, don't pretend you don't have the free time to learn photoshop.

I guess I'll edit in some content, ran across this code snippet today. Likely an ASM oldie being forced into C.

code:
#define  SetPin(p,b)  asm("bsf p, b");
#define  ClrPin(p,b)  asm("bcf p, b");

Jerry Bindle fucked around with this message at 05:26 on Jul 12, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
i needed to iterate over an at-most 3x3 rectangle in a 2d array

so naturally I wrote this
code:
/** Destroy Gems within one block of the given position, including the Gem at (x, y). */
private void radial(Gem[][] target, int x, int y) {
	int h = target.length, w = target[0].length;
	Gem emp = emptyGem();
	for (int i = ((y - 1 > -1) ? y - 1 : y); i <= ((y + 1 < h) ? y + 1 : y); i++) {
		for (int k = ((x - 1 > -1) ? x - 1 : x); k <= ((x + 1 < w) ? x + 1 : x); k++) {
			set(target, k, i, emp);
		}
	}
}

Thel
Apr 28, 2010

Aleksei Vasiliev posted:

i needed to iterate over an at-most 3x3 rectangle in a 2d array

so naturally I wrote this
code:
/** Destroy Gems within one block of the given position, including the Gem at (x, y). */
private void radial(Gem[][] target, int x, int y) {
	int h = target.length, w = target[0].length;
	Gem emp = emptyGem();
	for (int i = ((y - 1 > -1) ? y - 1 : y); i <= ((y + 1 < h) ? y + 1 : y); i++) {
		for (int k = ((x - 1 > -1) ? x - 1 : x); k <= ((x + 1 < w) ? x + 1 : x); k++) {
			set(target, k, i, emp);
		}
	}
}

Doesn't have nested ternary operations, not horrific enough. :colbert:

Seriously, though, :stare:.

duck monster
Dec 15, 2004

Aleksei Vasiliev posted:

Labeled breaks aren't the same thing as gotos.

And yes but my case was justified and not bizarre like that thing. I haven't actually worked on it for awhile since I got bored with parsing binary->XML, but I'm pretty sure gotos would have helped me write cleaner code in that method.

Completely abuse the gently caress out of exceptions?

duck monster
Dec 15, 2004

Scaevolus posted:

Looks like a FORTRAN programmer has been forced to write C, and is resisting.

I've seen C written like that by an engineer who had only ever done verilog. Every single loving constant was defined as such Including things like const_two = 2 , etc.

Oh and the macros. God drat it macros are cool, but when every loving operation is first defined as a macro THEN applied to code, it very rapidly becomes brain damage inducing trying to read.

TheresaJayne
Jul 1, 2011

duck monster posted:

I've seen C written like that by an engineer who had only ever done verilog. Every single loving constant was defined as such Including things like const_two = 2 , etc.

Oh and the macros. God drat it macros are cool, but when every loving operation is first defined as a macro THEN applied to code, it very rapidly becomes brain damage inducing trying to read.

Ever heard of obsfuscation?

Its a common thing to obsufscate code to make you "irreplacable" and so secure your position in a job. (yeah right that will work right)

The1ManMoshPit
Apr 17, 2005

Fehler posted:

code:
typedef double real;
const real two = (real)2;
const real pi = 3.141592653589793;
This project is gonna be fun!

Maybe they use a static analysis tool or high warning level and don't want to be bombarded with loss of precision messages if they decide to change real to float?

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
No need to add comments on any of this poo poo, it's self-documenting.

code:
function onChangeMode3(e) {
    if (e.value == '0') {
        e.getParent().getNext().getNext().getFirst().value = e.getParent().getNext().getFirst().value;
        e.getParent().getNext().getFirst().value = '';
        if ($('do_multiple').checked) {
        	removeValidation2(e.getParent().getNext().getFirst(), 2);
        }
        else {
        	removeValidation2(e.getParent().getNext().getFirst(), 3);
        }
        e.getParent().getNext().getFirst().setProperty('disabled', 'disabled');
    }
}

Ensign Expendable
Nov 11, 2008

Lager beer is proof that god loves us
Pillbug
Is e some kind of insane list of lists (of lists)?

nielsm
Jun 1, 2009



Ensign Expendable posted:

Is e some kind of insane list of lists (of lists)?

Looks like some DOM-alike tree structure. I assume getFirst() actually means "get first child", and not "get first on this level", otherwise it wouldn't really make sense.

The code would make more sense if you had an example of the document tree it's presumably working on.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

Fehler posted:

code:
typedef double real;
const real two = (real)2;
const real pi = 3.141592653589793;
This project is gonna be fun!
I'm curious what they're doing that demands pi to that precision but still uses IEEE floats

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

nielsm posted:

The code would make more sense if you had an example of the document tree it's presumably working on.

Yes it would, wouldn't it.

It would also make more sense if the coder would have explained what he was trying to do. And instead of using e.getParent().getNext().getNext().getFirst().value, he would or could use something like $('foo12').value.

Those get-methods are part of MooTools.

There is all kinds on insane poo poo in this codebase, but I'm unfortunately starting to understand how it works.

poo poo like <div class = "ColorDropDown CoolBeans WhatIsThisShit">, which gets processed by "the framework", which then splits that class with space, then does stuff like:
code:
var renderFunction = null;
try{
renderFunction eval("render" + classes[i])
} catch (){}
if( renderFunction ){
 renderFunction.apply(elem);
}

function renderColorDropDown(){
    document.write(["who the gently caress",  
      "uses document.write",
      "in 2000 loving 11"].join('')
    );
}

Wheany fucked around with this message at 18:30 on Jul 12, 2011

PrBacterio
Jul 19, 2000

Wheany posted:

No need to add comments on any of this poo poo, it's self-documenting.

code:
function onChangeMode3(e) {
    if (e.value == '0') {
        e.getParent().getNext().getNext().getFirst().value = e.getParent().getNext().getFirst().value;
        e.getParent().getNext().getFirst().value = '';
        if ($('do_multiple').checked) {
        	removeValidation2(e.getParent().getNext().getFirst(), 2);
        }
        else {
        	removeValidation2(e.getParent().getNext().getFirst(), 3);
        }
        e.getParent().getNext().getFirst().setProperty('disabled', 'disabled');
    }
}
That *kind of* looks like he's doing a tree node rotation there, but probably not. At least whenever I've implemented AVL trees or somesuch the resulting code often didn't look all that dissimilar to that ... :o

CRIP EATIN BREAD
Jun 24, 2002

Hey stop worrying bout my acting bitch, and worry about your WACK ass music. In the mean time... Eat a hot bowl of Dicks! Ice T



Soiled Meat
No specific examples but I have a million line code base (ANSI-C) that assumes that divide by 0 should return 0.

ohgodwhat
Aug 6, 2005

Well what else would you return!

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

ohgodwhat posted:

Well what else would you return!

Nothing; however, demons may fly out your nose as a side effect

Fehler
Dec 14, 2004

.

The1ManMoshPit posted:

Maybe they use a static analysis tool or high warning level and don't want to be bombarded with loss of precision messages if they decide to change real to float?
Considering what the rest of the file looks like I'm 99% sure the guy(s) who wrote that have never heard of static analysis. First thing I had to do was run a code formatter to fix all the crazy indentation (or lack thereof).

Misogynist posted:

I'm curious what they're doing that demands pi to that precision but still uses IEEE floats
No idea. And even if they needed the precision, as far as I know M_PI actually has higher precision than the one from the file, so there's still no point in redefining it.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope

PrBacterio posted:

That *kind of* looks like he's doing a tree node rotation there, but probably not. At least whenever I've implemented AVL trees or somesuch the resulting code often didn't look all that dissimilar to that ... :o

No, he is assigning a value from a dropdown (a <select> tag) to a checkbox (<input type=checkbox>). Why? I have no idea.

Why don't the <select> and <input> tags have ids, so he could have done $('input_id').value = $('select_id').value? I have no idea.

The only way to know what that code was doing was to put a breakpoint in that function and watch e's value.

It also would have helped if there was any other comments in this code except:
code:
//gets color value
function getColorValue()
code:
/*
function getSomeValue(){
var value = $('someElement').value.split(/\s/);
return value;
}
*/
code:
//Fixes bug F3EA123-D5
//var foo = "foo";
//var bar = "bar";
var quux = "quux";
return quux + "baz";
That last comment is semi-useful. At least I could open the bug database and search for that bug, but instead the dude could have explained why and what change was made.

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
var is_feature_supported = ["true", "true", "false", "true", "false"];

At first glance, that might not look odd, and I sure missed it at first.

But those are not booleans values, those are strings.

So
if( is_feature_supported[i] )
always gets executed, because in JavaScript non-empty strings are always true.

NotShadowStar
Sep 20, 2000
And that's why you always, always, always use equals equals equals in javascript!

Factor Mystic
Mar 20, 2006

Baby's First Post-Apocalyptic Fiction

NotShadowStar posted:

And that's why you always, always, always use equals equals equals in javascript!

Then you're be writing is_feature_supported[i] === "true" which would be a worse horror. If you're going to play along with that array of features business then just use actual bools.

NotShadowStar
Sep 20, 2000
A static code analysis like JSLint will pick up on that if you don't. If you force yourself to do if(something === "true") then most people would go 'huh, what the hell am I doing?'

It's not THAT bad of a horror though, turning it into an object would be much clearer. if (featuresSupported.integratedButtSex) {}

Wheany
Mar 17, 2006

Spinyahahahahahahahahahahahaha!

Doctor Rope
featuresSupported.integratedButtSex = "false";

Jerry Bindle
May 16, 2003
When I wrote this a few weeks ago I thought it was brilliant
code:
bd = &bdt[USTATbits.ENDP << 1 + USTATbits.DIR];
Its simple, compiles to about 5 instructions, and works perfectly.

This morning I ran across it and had to wonder what the hell it did. Some day I'll get around to reading code complete that I've had on my shelf for months.

Scaevolus
Apr 16, 2007

"<< 1" instead of "* 2" is generally a horror.

Vanadium
Jan 8, 2005

I dunno, I'd be okay with it if that was a | and not a +.

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

Scaevolus posted:

"<< 1" instead of "* 2" is generally a horror.

Look at that code he wrote again. Look at those names. That stuff probably gets dumped into some crappy compiler for whatever embedded system it runs on that has no concept of strength reduction or peephole optimizations. You're still better off wrapping the thing in a macro (I assume the compiler doesn't support inline functions either :( ), but that's really no worse than manually unrolling a loop when the compiler won't do it for you. So long as you document it.

Presto
Nov 22, 2002

Keep calm and Harry on.

Scaevolus posted:

"<< 1" instead of "* 2" is generally a horror.

Well, assuming this is C/C++, plus is higher precedence than shift, so what that actually does is
code:
bd = &bdt[USTATbits.ENDP << (1 + USTATbits.DIR)];

Jerry Bindle
May 16, 2003

Presto posted:

Well, assuming this is C/C++, plus is higher precedence than shift, so what that actually does is
code:
bd = &bdt[USTATbits.ENDP << (1 + USTATbits.DIR)];

Wow good catch. At least I thought it worked right. Also it turns out that power of two multiply operations all compile to shift instructions, I was worried that they would link in the multiply library for parts without a hardware mul.

Edit: I had tested it with 2* in there, but then switched it to <<1 because I'm so clever!!

Jerry Bindle fucked around with this message at 18:19 on Jul 13, 2011

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
:eng99:

Adbot
ADBOT LOVES YOU

Presto
Nov 22, 2002

Keep calm and Harry on.

dietary supplement posted:

Edit: I had tested it with 2* in there, but then switched it to <<1 because I'm so clever!!
It's generally a bad idea to mix bitwise operations with arithmetic operations. You can either do (A << 1 | B) or (A * 2 + B); but, as we have just seen, you're asking for trouble if try to do both at the same time.

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