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
RegonaldPointdexter
Mar 13, 2006

Hey guys what's going on?

bmoyles posted:

code:
while(list($key,$val) = each($_POST)) {
        $$key = $val;
}


while(list($key,$val) = each($_GET)) {
        $$key = $val;
}
Not only is it a bad idea, it's not executed well either.

1. $_GET and $_POST instead of $_REQUEST.
2. while(list($key,$val) = each(...)) instead of foreach(... as $key => $val).
3. There's already a function that does exactly this.

So:
Step #1 - Replace that block of code with extract($_REQUEST);
Step #2 - Get rid of it and fix your loving script!

Adbot
ADBOT LOVES YOU

Dazzling Double V
Jul 26, 2007
Better than Rick Santorum's red boxers
A classmate tragically misunderstands how HTML works (dead link)

Dazzling Double V fucked around with this message at 01:30 on Jun 14, 2012

noonches
Dec 5, 2005

It represents my lifestyle and stratus as a street savy irreverent youth, who lives large, and yet hungers for the next level in life.

Dazzling Double V posted:

A classmate tragically misunderstands how HTML works. He even tried to match the syntax coloring from the tutorial he was using. This was the culmination of his efforts before giving up and switching to iWeb.

Wait, is that what his code looks like, or did he use some composer to create a page to display the code like that?

HIERARCHY OF WEEDZ
Aug 1, 2005

noonches posted:

Wait, is that what his code looks like, or did he use some composer to create a page to display the code like that?

It looks like he typed HTML code into Microsoft Word, then saved it as a web page.

That's - amazing.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


RegonaldPointdexter posted:

Not only is it a bad idea, it's not executed well either.

1. $_GET and $_POST instead of $_REQUEST.
2. while(list($key,$val) = each(...)) instead of foreach(... as $key => $val).
3. There's already a function that does exactly this.

So:
Step #1 - Replace that block of code with extract($_REQUEST);
Step #2 - Get rid of it and fix your loving script!

$_REQUEST wouldn't work for this because it goes $_GET -> overwritten by $_POST -> overwritten by $_COOKIE. That code is $_POST -> overwritten by $_GET.

ohgodwhat
Aug 6, 2005

duz posted:

$_REQUEST wouldn't work for this because it goes $_GET -> overwritten by $_POST -> overwritten by $_COOKIE. That code is $_POST -> overwritten by $_GET.

http://us2.php.net/manual/en/ini.core.php#ini.variables-order :mmmhmm:

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost



Yes, the default is GPC which is what I used. I figured anyone who writes code like that doesn't know how to edit the php.ini (or isn't allowed to).

ohgodwhat
Aug 6, 2005

duz posted:

Yes, the default is GPC which is what I used. I figured anyone who writes code like that doesn't know how to edit the php.ini (or isn't allowed to).

I think you put too much faith into idiots not putting too much effort into finding the wrong solutions for their own problems. :v:

argolithm
Apr 2, 2008
In my data-structures CS class, you would think that most of the people who can't really hack programming would have been filtered out by now. The introductory logic class, the "intro to computer science" and "computer science" classes that tell you how to program and such would usually intimidate people.

But... well:
code:
for(int i = 0; !false && i < word.length(); i++)
This appeared from a class-mate in my data structures class. The constant call of length() isn't a problem at all-- that's insignificant enough to let people do it, I don't care about that. What I do care about is the use of !false.

This happened a few months ago. I'm still in awe. Not only is that conditional a tautology, but... !false? Seriously? There's a keyword for that-- it's called "true" for gently caress sake!

I think being a Java school is to blame here.

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

argolithm posted:

But... well:
code:
for(int i = 0; !false && i < word.length(); i++)
This appeared from a class-mate in my data structures class. The constant call of length() isn't a problem at all-- that's insignificant enough to let people do it, I don't care about that.

FYI, I'm almost positive that word.length() call will get optimized into an inline by HotSpot. I actually thought javac would take care of it, but the disassembler says otherwise.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

Drx Capio posted:

It's an application of the Single Function Exit Point philosophy, with which I don't agree. I think it's good for some issues, especially resource management, but largely you want to use RAII for that anyway, and code can get very messy if you can't bail out early (goto, anyone?).

Single function exit point are for BASIC and assembly, that's about it.

If your programming language has function define/call/return features built-in, you might as well use them.

admiraldennis
Jul 22, 2003

I am the stone that builder refused
I am the visual
The inspiration
That made lady sing the blues

argolithm posted:

This happened a few months ago. I'm still in awe. Not only is that conditional a tautology, but... !false? Seriously? There's a keyword for that-- it's called "true" for gently caress sake!

I think being a Java school is to blame here.

Even if I managed to type something as horrible stupid as !false, I would immediately correct it and give myself a slap on the face. Who the gently caress thinks that is acceptable?

I'm a CS student (actually IS but its very similar here), and our first freshman programming class is taught entirely in scheme. Coursework was fairly demanding and topics included natural recursion (for everything), functional programming, lambda programming, etc. Iterative recursion (for loops, etc) aren't even mentioned until halfway through the second semester, in which Java is used to teach object-oriented design, abstraction, encapsulation, inheritance, polymorphism, and so forth.

Neither course focused on syntax or a practical overview of either language. There are other courses for that. These classes were literally How to Design Programs (Functions) and How to Design Classes, and that's what was emphasized.

I'm very happy with the CS program here (Northeastern) so far. I know I've become a much better programmer because of it.

Incoherence
May 22, 2004

POYO AND TEAR

admiraldennis posted:

I'm a CS student (actually IS but its very similar here), and our first freshman programming class is taught entirely in scheme. Coursework was fairly demanding and topics included natural recursion (for everything), functional programming, lambda programming, etc. Iterative recursion (for loops, etc) aren't even mentioned until halfway through the second semester, in which Java is used to teach object-oriented design, abstraction, encapsulation, inheritance, polymorphism, and so forth.

Neither course focused on syntax or a practical overview of either language. There are other courses for that. These classes were literally How to Design Programs (Functions) and How to Design Classes, and that's what was emphasized.

I'm very happy with the CS program here (Northeastern) so far. I know I've become a much better programmer because of it.
Sounds like the program I came through. If it's anything like mine, one of the main things you should get out of it is a fine bullshit filter around the application of Java design patterns.

But... uh... !false definitely qualifies as a Coding Horror. I can at least fathom why someone would write if (true), but not !false.

dustgun
Jun 20, 2004

And then the doorbell would ring and the next santa would come
The !false looks like it comes from debugging the loop (or maybe code right after it). Like, instead of commenting out the loop, he initially just put the false in there to short-circuit it, and then at the end he !ed it? I don't know - !false is just too weird to not have a little backstory that does make some modicum of sense.

Xae
Jan 19, 2005

I ran into this in PL/SQL this week.

code:
CURSOR shittycursor IS

SELECT DISTINCT *
  FROM ( SELECT <15 table join and query here>
          UNION [b]ALL[/b]
         SELECT <Nearly Identical Query here>
	  UNION [B]ALL[/b]
	 SELECT <Nearly Identical Query here>
        )


Replaced with a 3 inner/2 outer join query, dropped the run time from 30 seconds to 5 on the procedure. The true lovely part is that the guy who wrote it is one of the people peer reviewing my work. But it gets better! The contracting group he is part of is getting fired/canceled because their work sucked, and our group is getting it done better and faster. But they don't get laid off until they are done peer reviewing the work of their replacements (us).

Xae fucked around with this message at 17:32 on May 4, 2008

nrichprime
May 29, 2004

Incoherence posted:

I can at least fathom why someone would write if (true), but not !false.

I've heard plenty of people describe conditions as being not false. The author of that code probably overhead someone making a comment along those lines and ran with it.

MrMoo
Sep 14, 2000

nrichprime posted:

I've heard plenty of people describe conditions as being not false.

It's true:
code:
#define	TRUE	(!FALSE)
So with a signed int, true is -1, but aware that comparisons like 2 == TRUE fail because it's a value comparison not a logical comparison, the correct comparison being (2), the retarded version being 2 && TRUE.

MrMoo fucked around with this message at 14:59 on May 5, 2008

avidal
May 19, 2006
bawl-some

dustgun posted:

The !false looks like it comes from debugging the loop (or maybe code right after it). Like, instead of commenting out the loop, he initially just put the false in there to short-circuit it, and then at the end he !ed it? I don't know - !false is just too weird to not have a little backstory that does make some modicum of sense.

Yeah, I've actually done that myself in the past, usually with if statements though, so you can occasionally find one left over in my code, such as: if($conditions && 1) {}

the onion wizard
Apr 14, 2004

TRex EaterofCars posted:

FYI, I'm almost positive that word.length() call will get optimized into an inline by HotSpot. I actually thought javac would take care of it, but the disassembler says otherwise.

Would the compiler be able to tell ahead of time that length() is going to return the same value for the whole loop though?

tef
May 30, 2004

-> some l-system crap ->
I've gone about this at length on irc and will do so when provoked, but pretty much this is some of the ugliest code I've ever had to work on. Anti Pattern doesn't even begin to describe some of it.

Redundancy is good. This is why we have two different naming schemes within the code base, often small little details like numAdult vs NumAdults or numChildren vs NumChild, or QueryReturnDate vs DateBack. We also have at least four different functions that perform uppercase, including uc, uppercase and getuppercase.

There are three components to the project, two are kept in svn with manual version numbers in the top of every file, and the other is kept in the database. This would mean deploying is a nightmare, but it is ok we edit the database bits live and only push the svn code to production.

XSL turned out to be a bad choice of language for text mangling so it is augmented with vbscript heavily. Which has lead to some weird calling conventions - at least one function takes three arguments as a space seperated string like getfoo("3 2 1").

Nearly every function that is in use follows the abstraction inversion principal, it is better to pass one parameter in and have 140 cases inside, rather than pass two parameters in.

I can go on - it's only recently we have moved away from the 'stick everything in one file' development model. Although we still have a 'stick things with a similar name together, rather than things that call each other in the same directory' policy. It would be like doing an OO project where there is one file for each method name, and inside are the defintions for every type of object.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

triplekungfu posted:

Would the compiler be able to tell ahead of time that length() is going to return the same value for the whole loop though?

Can't speak for Java, but in C++ length() would almost certainly be simple enough to inline. That's not the whole story, though, since I don't believe inlining works when length() is in a shared library, which it probably is. (I think you can get inlining to work across library boundaries, but you have to do extra work? I dunno, I should read up on this.)

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

tef posted:

Although we still have a 'stick things with a similar name together, rather than things that call each other in the same directory' policy. It would be like doing an OO project where there is one file for each method name, and inside are the defintions for every type of object.

That's actually pretty close to a legitimate pattern.

tef
May 30, 2004

-> some l-system crap ->

JoeNotCharles posted:

That's actually pretty close to a legitimate pattern.

I'll admit it can make sense, like putting all your include files in one directory, and all your shared objects in another when distributing them. I

I had a hard time thinking of an appropriate metaphor - and I meant putting every length method in length.c and every hashcode method in hashcode.c so the definition of each object would be spread around 15 or so files throughout the project.

In this specific instance, it only serves to ofuscate the structure - for some reason normal websites within the project are in per website directories, but more general templates are in part1/website1.xsl, part2/website1.xsl and so on. We *never* need to refer to things by the part that they are in, and always are doing per website things. (So, on top of the multiple naming schemes we have multiple directory structures too).

Oh and I just found out that in one part we have AirlineCode and in another we have AIrlineCode for the same variable.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

tef posted:

Oh and I just found out that in one part we have AirlineCode and in another we have AIrlineCode for the same variable.

Oh, god, that reminds me of one of my pet peeves: ".lenght()" I've worked on code where the guy before me consistently spelled it that way. It's not even a typo he never bothered to fix, because it's that way EVERY TIME. And now I have to do it too, since I can't change the interfaces! And I've seen multiple people do this, although usually not more than once or twice.

People - if you have that many problems with this word, just use .len()!

_aaron
Jul 24, 2007
The underscore is silent.

JoeNotCharles posted:

Can't speak for Java, but in C++ length() would almost certainly be simple enough to inline. That's not the whole story, though, since I don't believe inlining works when length() is in a shared library, which it probably is. (I think you can get inlining to work across library boundaries, but you have to do extra work? I dunno, I should read up on this.)
I think what he was saying was that the code could look something like this:
code:
for(int i = 0; !false && i < word.length(); i++) {
   if (word[i] == '1')
      word = word + '0';
}
Or something simple like that. Basically, the work in the loop could be changing the length of word.

Lexical Unit
Sep 16, 2003

JoeNotCharles posted:

Oh, god, that reminds me of one of my pet peeves: ".lenght()"
I can one-up that. A struct with longitude, latitude, and longititude. No longititude is not different that longitude, it is just there because somehow longititude got into a spec sheet and it was easier to add to the spec than change it. Old code expects longititude to be populated, new code expects longitude to be populated. Good night, and good luck.

tef
May 30, 2004

-> some l-system crap ->

Lexical Unit posted:

Old code expects longititude to be populated, new code expects longitude to be populated. Good night, and good luck.

The output of the screen scraper produces xml in <quote>...</quote> or sometimes <Quote>...</Quote>. Parts of the website expect <Quote>...</Quote> and so a series of search and replace operations have been tacked on to the website code to fix this. Even though we could fix it in the program itself now we are in the position of not being able to change the output format for fear of breaking other code.

I also managed to fix a number of bugs by sorting the output.

pseudopresence
Mar 3, 2005

I want to get online...
I need a computer!
As I understand it, Java strings are immutable, so as long as the compiler can prove that the variable in the loop test always references the same string object, it can pull the call to length() out of the loop.

Mikey-San
Nov 3, 2005

I'm Edith Head!

Lexical Unit posted:

I can one-up that. A struct with longitude, latitude, and longititude. No longititude is not different that longitude, it is just there because somehow longititude got into a spec sheet and it was easier to add to the spec than change it. Old code expects longititude to be populated, new code expects longitude to be populated. Good night, and good luck.

I see this and raise you:

http://en.wikipedia.org/wiki/Referer

Lexical Unit
Sep 16, 2003

Ah, the classics have a beauty to them that is unparalleled.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

JoeNotCharles posted:

Can't speak for Java, but in C++ length() would almost certainly be simple enough to inline. That's not the whole story, though, since I don't believe inlining works when length() is in a shared library, which it probably is. (I think you can get inlining to work across library boundaries, but you have to do extra work? I dunno, I should read up on this.)

string is a template, so it would never be compiled into a shared library anyway. The compiler is pretty much going to inline it all the time unless you explicitly tell it not to inline anything.

Then it just becomes an issue of the following: is this,
code:
for(int i = 0; i < str.length(); i++)
really that much more harmful than this?
code:
int len = str.length();
for(int i = 0; i < len; i++)
The answer is no, because the string class stores its length explicitly (in every implementation that I've seen), so the first version after inlining is no less efficient than the second for all intents and purposes.

Anyone who would complain about this who isn't working entirely on tiny embedded micro-architectures needs to quit :awesome: stroking their e-penis :awesome: and focus on the problem domain instead of premature optimization. The OP said he didn't care about it, and rightly so. But drat does it piss me off when I get into a conversation with someone -- usually it was one of my "advanced" students -- who thinks they're just oh-so-clever and thinks they're smarter than a tried-and-tested professional compiler.

And to stroke my e-penis a little, code like what I just wrote above pisses me off. It should be
code:
for(size_t i = 0; i < str.length(); i++)
:colbert:

Standish
May 21, 2001

Flobbster posted:

The answer is no, because the string class stores its length explicitly (in every implementation that I've seen)
STL strings allow embedded nulls, so it's hard to even contemplate an implementation that doesn't store its length explicitly.

ThunderPollen
Apr 12, 2008
This is more of a its worse because its a first day thing.

code:

void ***m;

:smithicide:

more falafel please
Feb 26, 2005

forums poster

Flobbster posted:

It should be
code:
for(size_t i = 0; i < str.length(); i++)
:colbert:

++i damnit!

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

more falafel please posted:

++i damnit!

FWIW, I do use pre-increment when I'm dealing with iterators, so that I don't have to worry about the cost of a temporary copy. But for integral values, it's a wash.

Speaking of, has anyone ever examined the optimized assembly code for using ++iterator vs. iterator++ in a for-loop (or anywhere for that matter)? I wonder if the compiler is smart enough to optimize post- to pre- if it sees that the result isn't used as part of a larger expression.

Standish
May 21, 2001

Flobbster posted:

Speaking of, has anyone ever examined the optimized assembly code for using ++iterator vs. iterator++ in a for-loop (or anywhere for that matter)? I wonder if the compiler is smart enough to optimize post- to pre- if it sees that the result isn't used as part of a larger expression.
I'd be surprised if it could optimize that for std::iterators, remember the STL is just another library as far as the compiler is concerned -- from the compiler's point of view, it doesn't know anything about iterator semantics and std::iterator::operator++() (the prefix overload) and std::iterator::operator++(int) (the postfix overload) are completely distinct functions which could have completely different sideeffects.

In contrast, int is a built-in type so the compiler does know exactly when it's safe to substitute ++int for int++.

Standish fucked around with this message at 22:36 on May 6, 2008

Chin Strap
Nov 24, 2002

I failed my TFLC Toxx, but I no longer need a double chin strap :buddy:
Pillbug
Wait, could someone explain what iter++ is doing different than ++iter in a loop? I've never heard this before.

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"

Standish posted:

I'd be surprised if it could optimize that for std::iterators, remember the STL is just another library as far as the compiler is concerned -- from the compiler's point of view, it doesn't know anything about iterator semantics and std::iterator::operator++() (the prefix overload) and std::iterator::operator++(int) (the postfix overload) are completely distinct functions which could have completely different sideeffects.

In contrast, int is a built-in type so the compiler does know exactly when it's safe to substitute ++int for int++.

Good point, it hadn't occurred to me that some rear end in a top hat could overload operator++() to do the correct thing and operator++(int) to do while(1) printf("Penus\n");.

Chin Strap posted:

Wait, could someone explain what iter++ is doing different than ++iter in a loop? I've never heard this before.

Semantically, for(...; iter++) and for(...; ++iter) do the same thing, since the increment stands alone in the expression.

The difference is in the lower-level implementation -- ++iter is more efficient since it increments the operand before evaluating the rest of the expression, if any. iter++, on the other hand, requires that a copy be made, since it increments the operand and then uses its old value in the remainder of the expression.

So, in a tight loop, post-increment does O(n) unnecessary copy operations. In most cases it's not going to be that much of a penalty, but why do it if you can avoid it?

Flobbster fucked around with this message at 05:25 on May 7, 2008

crazypenguin
Mar 9, 2005
nothing witty here, move along
I never understood why they chose to make ++i and i++ different functions, rather than just having a single ++ function and just change when it gets called relative to when it is evaluated.

Adbot
ADBOT LOVES YOU

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Flobbster posted:

The difference is in the lower-level implementation -- ++iter is more efficient since it increments the operand before evaluating the rest of the expression, if any. iter++, on the other hand, requires that a copy be made, since it increments the operand and then uses its old value in the remainder of the expression.

So, in a tight loop, post-increment does O(n) unnecessary copy operations. In most cases it's not going to be that much of a penalty, but why do it if you can avoid it?

:aaaaa:
Wow this is so simple that it never occurred to me. I always thought ++i was a matter of style. Is it safe to assume that modern compilers do this automatically now? Except in the case of the iterator you mentioned.

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