- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It could print out the table, put it on a wooden table, take a picture, scan it, and use OCR to extract the data.
Repeat 5 times
Admin
Are there any languages that have a way to declare/define a function that given constant inputs, the return value is constant?
So that something like:
<FONT color=#000080>if(myfunction(1,2) > 7) {myvariable=(myfunction(1,2)); }</FONT>
<FONT color=#000080>const int myfuction(x,y) { return(x+y); }</FONT>
would be optimized to zero instructions, and:
<FONT color=#000080>if(myfunction(1,2) < 7) {myvariable=(myfunction(1,2)); }</FONT>
<FONT color=#000080>const int myfuction(x,y) { return(x+y); }</FONT>
would be optimized to:
<FONT color=#000080>myvariable = 3;</FONT>
<FONT color=#000000>aside from using pre-compiler macros/directives that inline the functions code?</FONT>
Admin
That's version 2. They're still ironing out the bugs in v1. I'm not sure, but I wouldn't be surprised if EDS were involved somehow.
Admin
I cannot be so sure about the quantity, but I'm pretty sure that Steve was next on the row
Admin
DECLARE @usr
SET @usr = USER
SELECT 'file-not-found'
WHERE @usr = 'Stupid'
GO
Admin
This sort of WTF is common when programmers with a procedural language bias write SQL. The instinct for modularity doesn't serve them well here - SQL engines are built for speed, not modularity.
The nice thing about the fix is that you don't need to know much about what the query is trying to do; you just do multiple passes eliminating redundancy and silliness, and eventually you boil it down to its essence. This was a tough one - Steven must have done a great job.
Admin
This is done for clarity and symmetry, as well as future-proofing; if in the future the "then" clause is rewritten to not immediately return, you don't want it unconditionally returning func(y). The K&R guys knew what they were doing, even in 197x.
Also, whether or not blocking is done by indention or not has no effect on having to write if/then/else clauses in this way. You should think more carefully before criticizing things you don't fully understand.
Admin
(Ugh, forgot to quote)
This is done for clarity and symmetry, as well as future-proofing; if in the future the "then" clause is rewritten to not immediately return, you don't want it unconditionally returning func(y). The K&R guys knew what they were doing, even in 197x.
Also, whether or not blocking is done by indention or not has no effect on having to write if/then/else clauses in this way. You should think more carefully before criticizing things you don't fully understand.
Admin
I don't know if you count optimalization as directive, but GCC does this:
With -O3 optimalization this code becomes:Welcome to year 2000.
The second example - C:
asm:
Admin
This is called Common Subexpression Elimination (CSE) and is done as a compiler optimization. I think many optimizing compilers would have a reasonable chance at doing the optimization you listed (by detecting a bunch of constants), but they'd have to detect side effects, which is generally more easily done in functional languages. I know Haskell's GHC does not perform CSE but I'm not sure about other Haskell compilers. Clean might do full CSE, haven't looked into it.
(I just so happen to be learning Haskell, so I had these open earlier today)
Admin
LISP is not a proper functional language.
Admin
With exactly two exceptions, cursor-based SQL queries are a proper subset of all WTFs.
http://en.wikipedia.org/wiki/Subset
Select count(*)
from allWTFs as crap
where crap.samplecode like '% declare cursor %' ...
Admin
I can quite understand The Policy. I've seen policies like that being made because it was obvious to the managers that every time the thing was being maintained, it would be broken for a week or two, only to come back slower than ever before. C'mon, what's a non-programmer to think?
And speaking of cursors: A while ago, I was writing a stored proc that would update some of my tables and then triggers on those tables would handle the rest. Now, one of my coworkers had some tables that also needed updating. I told him I'd just update his main table when the proc fired, and he could use triggers like I did. No way, that couldn't be done because there were several rows and several related tables involved. All my sweet talking did no good, so he wrote another stored proc to do his cursor thing, which in turn meant that I was left with no option but to open a cursor myself and call his proc once for every row. So now there's a stored proc out there with my name on it that will just grow slower and slower :(
Why are they called cursors anyway? I'm the cursor, they are the cursees.
Admin
Admin
Actually, many flavors of SQL support this. For example, in SQL Server, the SIN function is considered "deterministic," and it will not be called for every row in a query if the arguments are the same. I believe that the values can even be stored and indexed in the case of a view. However, GETDATE is not deterministic, and needs to be called every time it is referenced.
If you are writing a function in another language (like C#), you have to state if the function is deterministic with the SqlFunction attribute. Weird and unpredictable things can happen if you lie about being deterministic, although it can be interesting to make your function log a message just to see how well SQL Server is able to optimize calls to a function.
It certainly would be cool if general-purpose languages supported marking a function as deterministic in order to avoid redundant execution. I mean, pretty much every Math function could be markes as such. It'd be similar (but not exactly like) marking a function "const" in C++.
Admin
Why the heck show the quoted post in the preview if you're not going to post it?
As much as I like .NET, people sure do use it to write horrible software. Most php-based forums are much more intuitive.
And WYSIWYG editors are worthless if they don't work. Give me HTML or BBCode any day.
Admin
Your comment finally forced me to devote time to understand "monads" in Haskell, which I have postponed so many times...
//of course it was clear to me that it is possible to carry the whole PC state with you through subsequent calls...but I didn't believe somebody was crazy enough to....
Admin
I'm not an SQL person, so I can't quite grasp the true sense of this WTF, although that code does look heinous.
What I don't understand is, WHY would this report take longer on same does than on others? Should it just always take a long time? And as more data is added, shouldn't it take even longer? What am I missing? Or are the tables of data per day?
Admin
By introducing such a large performance delta, our boy may have made people even more nervous about The Report. Nobody else fixed it, so why should we trust this new code. Surely there's a mistake somewhere...
He should have only made it 20% faster. That would've gotten him some kudos. Then, a month or so later, he makes it 20% faster again. More kudos. After a few months, The Report is purring along, everyone knows who's making it work better, and our boy is an SQL genius with a fat raise.
Damn, I'm going to write a book.
Admin
You mean like GCC's __attribute__((const))?
Admin
Me too.. I learned that anonymous should've said "pure functional language", since (in the article he referenced) "Not all functional programs or functional programming languages are purely functional."
Admin
Ok let us have a show of hands
howmany of you has forgoten a ; or } at the end of a line of code.
How many of you have accidently written an entire function on
one line because you forgot to press the return key.
Admin
I don't think so. You must have missed the part where they said it actually WORKED every other day!
Thanks
Admin
create table #JOBTEMP
(
ID smallint IDENTITY(1,1),
JobCD int,
JobID int
)
insert into #JOBTEMP
(
JobCD,
JobID
)
select top 5 cojobs.job_cd, job_id
from cojobs
inner join jobs on cojobs.job_cd = jobs.job_cd
where del_ind = 0
select JobCD, ID,JobID from #JOBTEMP
----
What do I win?
Note too that Alex does say this is a /small/ part of the function....I wonder what the parameter is for...
Admin
No no.. the select statement should be like this to complete the WTF:
insert into #JOBTEMP
(
JobCD,
JobID
)
select cojobs.job_cd, job_id
from cojobs
inner join jobs on cojobs.job_cd = jobs.job_cd
where del_ind = 0
select JobCD, ID,JobID from #JOBTEMP where ID <= 5
e.g. removed the select top 5, that is unknown to oracle users and since there isn't "where rownum < 5" the workaround is to copy the data to a temporary table adding an identity field and then select the range you need and voila - you got what you need - magic, eh? :-)
Admin
Haskell happens to use monads to represent input/output operations. This isn't the only possible way to represent I/O -- it's just the way that Haskell happens to use. The easiest way to think about I/O in Haskell is that a Haskell program takes no inputs and returns a special data structure that represents an imperative program, and that it's the imperative program that actually does the I/O. That data structure is of type IO (), where () is Haskell's equivalent of a void type. You can join IO objects together with the >>= operator, and the IO type will sort itself out for you.
Just for fun, this means that putStrLn does not actually print a line of text (as you can verify with "putStrLn "foo"
seq
True"). It doesn't do anything until the return value of putStrLn gets returned from main (or to the interactive interpreter).Admin
Ah, I so wish this site existed when I worked at one of my previous jobs. I'd fix shit like this all the time. I'd take jobs that took hours to run and made them run in seconds. The "lead" programmer didn't know what the hell an inner join was, so he'd use a plethora of temp tables to narrow down his result sets.
Admin
We have such a thing in Perl; it's called Memoize.pm. It would be trivial to do in Haskell for sure.
Admin
Re: Haskell, it also means getLine is not a function:
getLine :: IO String -- see? a simple constant.
Admin
Couldn't you avoid both temp variables and re-evaluations with:
<font face="Courier New">func2(x,y)
{
return x > y ? x : y;
}
func2(func(x),func(y);
</font>
Admin
Missed a close paren.
<font face="Courier New">func2(func(x),func(y));
</font>
There. Much better.
Admin
quiet a few, fortran springs to mind using the keyword pure, i think there are some C/C++ extensions and some scientific languages support this, too. (Notable maple, but they use the remember keyword, that is they just remeber the result, so it works even for non-pure functions.)
HTH,
mike
captch=enterprisey - not very fitting captcha this time
Admin
You do not get rid of the temp variables, they just get hidden. Also you add a whole function call/return to your programm. Speak about simplicity.
Then again, sometimes it makes sense, speak about for i.e. the java compare function.
Captcha=craptastic - now thats fits^^
Admin
As it turns out, SQL Server can analyze a function to figure out whether it is deterministic (i.e. given inputs always yield the same outputs). If it is deterministic, it will only have to be called once for each set of input states.
Admin
If it's an imperative language like C++, you can inline the new function; if it's functional, then adding function calls/returns is business as usual. There are indeed hidden temporary variables, but the whole point was not to have to declare/name them, I thought.
*shrug*
Admin
"There are many other ways of doing this. All are faster. I can't think of a slower way. Anyone?"
I guess we could have each join call a function that calls an extended procedure which calls a dll that calls a web service that gets hold of the first 5 rows from a photograph of an excel sheet (on a wooden table of course) as XML. Inserts the resultant parsed values (of course write own overelaborate funtion to parse XML and wood) into a temporary table....blah etc.
Enterprisey right?
Admin
One would assume most of their developers by the time Steven got to work. He probably did it out of fear for his life. :-)
Admin
Yup, it's trivial in Haskell. Heck, you can even do it in JavaScript: http://talideon.com/weblog/2005/07/javascript-memoization.cfm
Admin
Oracle's PL/SQL has even more detailed markers about a function, like
"doesn't read the database", "doesn't write to the database", "doesn't read package status", "doesn't write package status"
Admin
Yes, that's what branches do. What's retarded here is that the function will be ran an extra time when for one or other of the values <var>x</var> or <var>y</var>. But that's a completely different matter.
Hmmm... smells like trolling... but I'll bite.
Let's use some Python code as an example. I'll write all the different ways of expressing this code that I can think of below:
And your point again was..?
Admin
This looks like something that fits:
ORA-00320 cannot read file header from log string of thread string
Cause: The file is not available.
Admin
The joy of PHP:
<font face="Courier New">$customer = new Client();
$john = $customer;</font>
This PHP code create 3 objects.
If you arent not scared enough. You sould read that about leaks on Mozilla:
http://www.mozilla.org/scriptable/avoiding-leaks.html
A snip of code, this javascript code leak:
<font face="Courier New">
</font> --Tei
captcha: "knowhutimean" .. .heee..WTF?
Admin
UTL_FILE would create other errors, in the most cases it throws user-defined exceptions.
Admin
So... for something as trivial and minor as air traffic control they wouldn't let some know-it-all student that was there for a month or two rewrite part of this unimportant system? Personally I'd have told you to go play with it and do what you want. After all, it works as it is, and there's no more dedicated a worker than a part time student during his time off Uni!
I guess that that is the real WTF!
Admin
Except that's not actually true.
Even if we restrict ourselves to pure functional languages (Haskell, say), there are still side-effects. I/O is a side-effect, for example. Hence the use of monads.
Admin
Of course there are "FNF" messages... :)
Admin
Sorry for nitpicking, but I think you meant
return reduce(max, map(func, [x, y]))
. Nice and convoluted, too bad the functional features are getting removed...Admin
I think you mean LISP is not a pure functional language. Nor are most other functional languages, such as ML.
Admin
Actually, the guy asked about QUERY equivalents, which could even make the inquiry and/or answer slightly interesting. For if you query for a field value which isn't in the table, e.g.
<FONT face="Courier New">select NAME from SYS.USER$ where NAME = 'FileNotFound';</FONT>
that's not an error at all, SQL just returns an empty result set.
However, if we go back to today's WTF and define a cursor using that result set and try to FETCH records inside a loop, we might get something like OCI_NO_DATA (on Oracle).
Admin
I don't understand how he optimised it. Please EXPLAIN. ;)