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
Real UK Grime
Jun 16, 2009
Not seen much SQL floating about, so here is today's project I was given to 'optimize':

code:
select
    @RepSapNumber as SapNumber,
    TeamFK,
    dateadd(day, -4, WeekEnding) as WeekStarting,
    WeekEnding,
    'Total' as ContactType,

    /* Team 1 */
    dbo.fn_TotalQuoteVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 1, @startDate, @endDate) as [01 Quote Volumes],
    coalesce(dbo.fn_TotalWonJobVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 1, @startDate, @endDate), 0) as [01 Total Job Volume Won],
    dbo.fn_TotalJobQuotesRaisedByStaffTeamDivision(@repSapNumber, @TeamFK, 1, @startDate, @endDate) as [01 Total JobQuotes Raised],
    coalesce(dbo.fn_TotalLostJobVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 1, @startDate, @endDate), 0) as [01 Jobs Lost Volume],

    [...snip teams 2-50...]

    /* Team 51 */
    dbo.fn_TotalQuoteVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 51, @startDate, @endDate) as [51 Quote Volumes],
    coalesce(dbo.fn_TotalWonJobVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 51, @startDate, @endDate), 0) as [51 Total Job Volume Won],
    dbo.fn_TotalJobQuotesRaisedByStaffTeamDivision(@repSapNumber, @TeamFK, 51, @startDate, @endDate) as [51 Total JobQuotes Raised],
    coalesce(dbo.fn_TotalLostJobVolumesByStaffTeamDivision(@repSapNumber, @TeamFK, 51, @startDate, @endDate), 0) as [51 Jobs Lost Volume]

    from
		    vr_ReportView as results
Each scalar function is a variation of
code:
select count(*) from atable group by somethingorother
vr_ReportView contains roughly 1,000 records. Words fail. :gbsmith:

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

On an SQL flavoured note, something I saw recently. With a table with an id and sub id, where the pair id, sub_id is unique, and sub_id is guaranteed to be 1, 2, etc... for each id.
code:
WITH cte AS (
  SELECT
     ...
     row_number() OVER(
       PARTITION BY id
       ORDER BY sub_id
     ) AS rownum
  FROM
    ...
) SELECT
  *
FROM
  cte
WHERE
  rownum = 1

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!

Zombywuf posted:

On an SQL flavoured note, something I saw recently. With a table with an id and sub id, where the pair id, sub_id is unique, and sub_id is guaranteed to be 1, 2, etc... for each id.
code:
WITH cte AS (
  SELECT
     ...
     row_number() OVER(
       PARTITION BY id
       ORDER BY sub_id
     ) AS rownum
  FROM
    ...
) SELECT
  *
FROM
  cte
WHERE
  rownum = 1
What's the horror here? I suppose if you can 100% guarantee that records with sub_id = 1 will never, ever, ever "go missing" then it's a bit redundant.

Or is the horror that he named that column "rownum" since that could possibly be a little bit misleading?

Zombywuf
Mar 29, 2008

Jethro posted:

What's the horror here? I suppose if you can 100% guarantee that records with sub_id = 1 will never, ever, ever "go missing" then it's a bit redundant.

Or is the horror that he named that column "rownum" since that could possibly be a little bit misleading?

I should have been more clear, if the records with a given id do not have sub_ids increasing in steps of 1, starting from 1, the data is wrong, i.e. returning the data where sub_id <> 1 is incorrect.

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost
Something I found today in a very large, professionally developed (.NET 2.0) C# application:

code:
// typed from memory, might not be entirely accurate.
public class NamesAndValues
{
   private Dictionary<string,object> mStore;
   private SortedList<int,string> mInsertionOrder;

   public void Add(string key, object data)
   {
       mStore.Add(key, object);
       mInsertionOrder.Add(mStore.Count, key);
   }

   public void Remove(string key)
   {
       mStore.Remove(key);
       mInsertionOrder.RemoveAtIndex(mInsertionOrder.IndexOfValue(key));
   }
}
:suicide:

The sorted list is completely unnecessary and is actually the cause of a bug. You can just use a normal list and add to the end. Removal is an O(n) operation either way due to the need to search through the SortedList for the value instead of the key. Also, if you Add() n > 1 items and then Remove() one or more of the first n-1 items and then Add() one more item, the SortedList will throw an ArgumentException due to a duplicate key (mStore.Count will be n, then n-1, then n again, so mInsertionOrder.Add(n, string) after the removal will find the key n in the SortedList and fail).

plushpuffin fucked around with this message at 22:53 on Aug 26, 2009

Seth Turtle
May 6, 2007

by Tiny Fistpump
code:
	/**
	 * This method initializes jMenuItem6
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private javax.swing.JMenuItem getJMenuItem6() {
		if(jMenuItem6 == null) {
			jMenuItem6 = new javax.swing.JMenuItem();
			jMenuItem6.setText("Set As Defaults");
		}
		return jMenuItem6;
	}
	/**
	 * This method initializes jMenuItem7
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private javax.swing.JMenuItem getJMenuItem7() {
		if(jMenuItem7 == null) {
			jMenuItem7 = new javax.swing.JMenuItem();
			jMenuItem7.setText("Return to Defaults");
		}
		return jMenuItem7;
	}
	/**
	 * This method initializes jMenuItem8
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private javax.swing.JMenuItem getJMenuItem8() {
		if(jMenuItem8 == null) {
			jMenuItem8 = new javax.swing.JMenuItem();
			jMenuItem8.setText("View Raw Message");
		}
		return jMenuItem8;
	}
This was copied from one of his shorter Java files. It's only 909 lines long. And that particular class extends another class, which has similar constructions in it. I'm currently converting his code for use in an applet. Because my boss told me to. There are 187 Java files total, all written with the above philosophy.

Same project. Same author. From a class meant to wrap objects that are stored in some kind of global hash. The class does not implement an interface.

code:
    public String getCurrentSetting() {
		// JCheckBox, JRadioButton, JToggleButton
		if (localComp instanceof JToggleButton)
			return ((JToggleButton)localComp).isSelected() ? "True" : "False";
		return null;
    }

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Lines 6921-6934 of a 10458 line function:

code:
else if ((en>=shred.energy) && (cp<5)&& ((((koj_timer<((shred.energy+rake.energy-10-en)*10) && 
 ((mdebuff_timer > ((shred.energy+mangle.energy-10-koj_m1-en)*10) && ((rake_timer > ((shred.energy+rake.energy-10-koj_m1-en)*10))||(rk==0))
 && (cp<3) && (mdebuff_timer >= 100+lag) && rake_timer>(0) )||((sr_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||((rip_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||(koj_timer<(100+lag))))||(((mdebuff_timer > ((shred.energy+mangle.energy-10-en)*10) 
 &&(( rake_timer > ((shred.energy+rake.energy-10-en)*10))||(rk==0)) && (cp<3) && (mdebuff_timer >= 100+lag) && ((rake_timer>0)||(rk==0)) )
 ||((sr_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||((rip_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||(koj_timer<(100+lag)))))||((cp>=3)||(rip_timer<0)))||(time_to_die_boss<=kill_mode)))
				
/*
else if ((en>=shred.energy) && (cp<5)
  &&(((koj_timer<rake_timer)&&(en>=((shred.energy+rake.energy-10-koj_m1))))||(rake_timer>=((shred.energy+rake.energy-10-en)*10))||(rk==0))
  &&(((mdebuff_timer<rake_timer)&&(en>=((shred.energy+mangle.energy-10-koj_m1))))||(mdebuff_timer>=((shred.energy+mangle.energy-10-en)*10)))
)
 */	  
 {

Plorkyeran fucked around with this message at 07:18 on Aug 28, 2009

Threep
Apr 1, 2006

It's kind of a long story.
Oh god, I had to google it to see the full horror and it's so much worse

Vinterstum
Jul 30, 2003

Plorkyeran posted:

Lines 6921-6934 of a 10458 line function:

Oh WoW addons, you'll be the doom of us all.

EDIT: Hah, not an addon, a "direct simulation therycrafting tool" :eng101:

Vinterstum fucked around with this message at 07:17 on Aug 28, 2009

Zombywuf
Mar 29, 2008

Threep posted:

Oh god, I had to google it to see the full horror and it's so much worse
code:
&&(sr_timer<=(time_to_die_boss))))
Seems a very apt line from that file.

Nippashish
Nov 2, 2005

Let me see you dance!
This is from a math rendering module for mediawiki. It's a important to get rid of all those dangerous capital >'s.
code:
// circumvent certain security functions of web-software which
// is pretty pointless right here
$latex_formula = preg_replace("/>/i", ">", $latex_formula);
$latex_formula = preg_replace("/</i", "<", $latex_formula);

1337JiveTurkey
Feb 17, 2005

I learned an interesting/disturbing use for XSLT when I needed to add some unit tests to some XML data exporter recently. It turns out the exporter doesn't have unit tests in the traditional sense but instead has one unit test. This test is naturally thousands of lines long and spread over several files and includes a set of unit tests for testing itself. This is only prudent for a multithreaded framework which appears* to run different programmatically constructed exports selected using cost/export time based heuristics. These exports are then tested using a combination of XSLT stylesheets which use patterns that cause the transformation to fail when their associated XPath expression matches. Now all I have to do is figure out how those stylesheets get associated with the proper permutations of options and then add my own somewhere in there.

*I'd be more sure if there was a single drat useful comment in the whole thing. :suicide:

tripwire
Nov 19, 2004

        ghost flow

Nippashish posted:

This is from a math rendering module for mediawiki. It's a important to get rid of all those dangerous capital >'s.
code:
// circumvent certain security functions of web-software which
// is pretty pointless right here
$latex_formula = preg_replace("/>/i", ">", $latex_formula);
$latex_formula = preg_replace("/</i", "<", $latex_formula);
I don't understand this at all. At first I thought the angle brackets were being escaped but thats a forward slash, not a backslash. What does "/>/i" actually match?


edit: Ah its php. I guess /i is to ignore case. What the hell?

bitprophet
Jul 22, 2004
Taco Defender

tripwire posted:

edit: Ah its php. I guess /i is to ignore case. What the hell?

It may be PHP code, but /regex_goes_here/flags_go_here is standard PCRE which is found in just about every language and many command line tools :shobon:

Doesn't change the stupidity of specifying the case-insensitivity flag for non-alpha characters though :psyduck:

TOO SCSI FOR MY CAT
Oct 12, 2008

this is what happens when you take UI design away from engineers and give it to a bunch of hipster art student "designers"
Does that code do anything? It looks like a noop to me.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
The comment implies that if they don't do this, it'll be filtered by some other bit of code. Which is sort of a coding horror in itself.

zergstain
Dec 15, 2005

Yeah, code that somehow lets only lower(upper?)case angle brackets through.

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
Maybe he's running a meaningless regex to defeat something analogous to perl's taint mode?

plushpuffin
Jan 10, 2003

Fratercula arctica

Nap Ghost

Otto Skorzeny posted:

Maybe he's running a meaningless regex to defeat something analogous to perl's taint mode?

Unless whatever tainting mechanism PHP uses is very different from Perl's, you can only untaint data by extracting sub-pattern matches from a regex, not by using a regex to remove or replace characters. I don't know PHP very well at all, though.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Well, I notice it says "some web-software", implying that it's not necessarily a default option of PHP (which I wouldn't discount out of hand, since it is PHP) but rather perhaps a package some people have on their servers that causes issues, and this hack is to fix it

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

plushpuffin posted:

Unless whatever tainting mechanism PHP uses is very different from Perl's, you can only untaint data by extracting sub-pattern matches from a regex, not by using a regex to remove or replace characters. I don't know PHP very well at all, though.

I was assuming that any taint feature in PHP would be a shittier and dumber cargo-cult-copy of its Perl analog (like every PHP feature)

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.

Ryouga Inverse posted:

Well, I notice it says "some web-software", implying that it's not necessarily a default option of PHP (which I wouldn't discount out of hand, since it is PHP) but rather perhaps a package some people have on their servers that causes issues, and this hack is to fix it

I would think it's probably mod_security, but it's only a guess as I haven't played around with it much.

Supervillin
Feb 6, 2005

Pillbug

Ryouga Inverse posted:

Well, I notice it says "some web-software", implying that it's not necessarily a default option of PHP (which I wouldn't discount out of hand, since it is PHP) but rather perhaps a package some people have on their servers that causes issues, and this hack is to fix it

I'd just like to see what possible input string could go through those two lines and come out different.

king_kilr
May 25, 2007

Otto Skorzeny posted:

I was assuming that any taint feature in PHP would be a shittier and dumber cargo-cult-copy of its Perl analog (like every PHP feature)

magic_quotes, that is all.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Supervillin posted:

I'd just like to see what possible input string could go through those two lines and come out different.

One with uppercase angle brackets.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

Supervillin posted:

I'd just like to see what possible input string could go through those two lines and come out different.

What I'm saying is that, while we can all lol about />/i because haha angle brackets don't have a lowercase, the real coding horror here is the security package this bit of code is meant to fix interoperability with.

BattleMaster
Aug 14, 2000



Plorkyeran posted:

Lines 6921-6934 of a 10458 line function:

code:
else if ((en>=shred.energy) && (cp<5)&& ((((koj_timer<((shred.energy+rake.energy-10-en)*10) && 
 ((mdebuff_timer > ((shred.energy+mangle.energy-10-koj_m1-en)*10) && ((rake_timer > ((shred.energy+rake.energy-10-koj_m1-en)*10))||(rk==0))
 && (cp<3) && (mdebuff_timer >= 100+lag) && rake_timer>(0) )||((sr_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||((rip_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||(koj_timer<(100+lag))))||(((mdebuff_timer > ((shred.energy+mangle.energy-10-en)*10) 
 &&(( rake_timer > ((shred.energy+rake.energy-10-en)*10))||(rk==0)) && (cp<3) && (mdebuff_timer >= 100+lag) && ((rake_timer>0)||(rk==0)) )
 ||((sr_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||((rip_timer<(100+lag))&&(en>(rake.energy+sr_energy-10)))||(koj_timer<(100+lag)))))||((cp>=3)||(rip_timer<0)))||(time_to_die_boss<=kill_mode)))
				
/*
else if ((en>=shred.energy) && (cp<5)
  &&(((koj_timer<rake_timer)&&(en>=((shred.energy+rake.energy-10-koj_m1))))||(rake_timer>=((shred.energy+rake.energy-10-en)*10))||(rk==0))
  &&(((mdebuff_timer<rake_timer)&&(en>=((shred.energy+mangle.energy-10-koj_m1))))||(mdebuff_timer>=((shred.energy+mangle.energy-10-en)*10)))
)
 */	  
 {

For some reason I thought that was from some scientific program until I read the posts below it.

oldkike
Jan 10, 2003

hey

www.pleasegimmeadollar.com
A few years ago (before my hire) a coworker of mine had to choose which language/framework and methodology to design a new web application in. Which did he choose? Waterfall and MUMPS/Cache. He even wrote his own coding standards, which stipulate the maximum line cannot be longer than 400 characters, and a class implementation cannot be longer than 800 lines. I got to code review all 60k lines :D!

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

BattleMaster posted:

For some reason I thought that was from some scientific program until I read the posts below it.
It sort of is a scientific program, just a really nerdy one that (badly) models a game instead of the real world.

Scaevolus
Apr 16, 2007

I thought it was a video encoder because of mbebuff_energy.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Otto Skorzeny posted:

I was assuming that any taint feature in PHP would be a shittier and dumber cargo-cult-copy of its Perl analog (like every PHP feature)

http://blog.php-security.org/archives/92-CORE-GRASP-PHP-Tainted-Mode.html

welp

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!

quote:

maybe all these problems are gone soon

Incoherence
May 22, 2004

POYO AND TEAR

Plorkyeran posted:

Lines 6921-6934 of a 10458 line function:

:barf:
To be fair, the thing this is apparently trying to model is the following flowchart:
http://www.unbearably.net/blogfiles/simplecatdps.jpg

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Painless posted:



Maybe someday someone will make the ultimate PHP security patch - a patch that causes PHP to remove itself from the server on first use.

Kelson
Jan 23, 2005

Incoherence posted:

To be fair, the thing this is apparently trying to model is the following flowchart:
http://www.unbearably.net/blogfiles/simplecatdps.jpg

I guess this was the John Madden section?

Unormal
Nov 16, 2004

Mod sass? This evening?! But the cakes aren't ready! THE CAKES!
Fun Shoe
Code to "shuffle a list into a random order" :gonk:
code:
string Shuffle(List<string> ListToShuffle)
{
  int MaxItems = ListToShuffle.Count - 1;
  string RandomList = string.Empty;
  Random randNum = new Random();
 
  if (MaxItems>= 1)
  {
    for (int i = 0; i < MaxItems; i++)
    {
      int nRand = randNum.Next(0, MaxItems);
      string NextItem = ListToShuffle[nRand];
      if (string.IsNullOrEmpty(RandomList))
        RandomList = NextItem;
      else
        RandomList += "," + NextItem;
     }
   }
   else
     RandomList = ServerList[0];

   return RandomList;
}

Lexical Unit
Sep 16, 2003

Please tell me you are a teacher who is grading a high school student's homework.

jarito
Aug 26, 2003

Biscuit Hider

Lexical Unit posted:

Please tell me you are a teacher who is grading a high school student's homework.

I've seen MUCH worse on our exam we give to people applying for jobs.

pointers
Sep 4, 2008

-

pointers fucked around with this message at 03:34 on Jul 8, 2011

Adbot
ADBOT LOVES YOU

Outlaw Programmer
Jan 1, 2008
Will Code For Food
Why bother with the regex at all? I mean, don't you have to make sure that the value is a valid country code anyway? Can't you just check to see if the String is in some canonical set of codes?

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