Comment On The New TODO and More

While wading through hundreds of disallowed implicit casts and such from turning option strict on for a large project, Tevildo came across this little gem. [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: The New TODO and More

2012-11-28 08:04 • by HAL (unregistered)
Return "Need to fix this"
I'm sorry, Dave. I'm afraid I can't do that.

Re: The New TODO and More

2012-11-28 08:09 • by Larry (unregistered)
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.

Re: The New TODO and More

2012-11-28 08:14 • by Smug Unix User (unregistered)
If you comment your code you can get a good idea of what the function should be doing. If you didn't get the arcane syntax of a regular expression just right then the next developer will at least know what you are trying to do and make the appropriate adjustments. Sometimes code need to have the bugs removed and the same is true of regular expression. A lot of developers will simply look at the expression guess at what it was trying to do and either replace it with something like this example or somehow work around the problem. Writing code is a lot easier than reading it.

Re: The New TODO and More

2012-11-28 08:19 • by Herman (unregistered)
I'm actually interested in the execution path of the searchstring when the database server is able to parallelize the unions and each of the searchfields are indexed.

Re: The New TODO and More

2012-11-28 08:21 • by VB_adict (unregistered)
"turning option strict on for a large project"

Ha! There is the TRWTF.

Or maybe its Visual Basic (someone had to say this)

Re: The New TODO and More

2012-11-28 08:25 • by asdf (unregistered)
> the existence of the OR operator

Some people learn what OR operator does (on certain databases) with indices sooner, some later. Union still has a place in SQL as a poor's man OR.

Re: The New TODO and More

2012-11-28 08:33 • by Quosek (unregistered)
in same cases using unions instead of OR really makes a sense - sometimes it's better from performance point of view use unions ....

Re: The New TODO and More

2012-11-28 08:35 • by planB (unregistered)
Nonody noticed the order by only has an efect on the last select of the unions. i guess that's not the effect the developer was going for

Re: The New TODO and More

2012-11-28 08:36 • by recently departed colleague (unregistered)
Hey Chris,
sorry for all the injections, but please think a bit and may be you will realize that on old systems UNION worked faster than OR.

captcha valid
lol

Re: The New TODO and More

2012-11-28 08:37 • by planB (unregistered)
395675 in reply to 395673
Nonody = Nobody

Re: The New TODO and More

2012-11-28 08:42 • by lanmind
395676 in reply to 395669
Herman:
I'm actually interested in the execution path of the searchstring when the database server is able to parallelize the unions and each of the searchfields are indexed.


I think, in the case of SQL Server, that the optimizer really only looks at the first query in UNIONs to do its magic, so I gotta believe that ORs would make for a better query plan - unless the vast majority of the results are caught by the first query. It may be unavoidable, but likely the bigger problem is the LIKE operators.

Re: The New TODO and More

2012-11-28 08:45 • by lanmind
395677 in reply to 395673
planB:
Nonody noticed the order by only has an efect on the last select of the unions. i guess that's not the effect the developer was going for


Not on SQL Server:

SELECT Field01 = 1

UNION
SELECT 2
UNION
SELECT 0
UNION
SELECT 4
ORDER BY Field01

Field01
0
1
2
4

Re: The New TODO and More

2012-11-28 08:49 • by Ralph (unregistered)
Most databases have these things called query optimizers these days. Writing an arcane overly complex query to make the computer's work easier is premature optimization, like trying to outsmart your compiler. Don't forget that 99.9% of the ongoing costs are developer maintenance time, not CPU time.

Oh, this is MS SQL?

Never mind.

Re: The New TODO and More

2012-11-28 08:50 • by Bob (unregistered)
395679 in reply to 395667
Larry:
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.


Ha!

Re: The New TODO and More

2012-11-28 08:52 • by n9ds
395680 in reply to 395667
"The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own. "

That sounds like the "Not Invented Here" argument to me.

Re: The New TODO and More

2012-11-28 08:57 • by Cantabrigian
"regex scares me a little, OK, alot actually"

Aaaaargh! It's "a lot" - two words. You wouldn't write "afew", would you? Then again, it's probably better than writing "allot" (when meaning "a lot", not "share out"). </petpeeve>

Re: The New TODO and More

2012-11-28 08:59 • by Peteris (unregistered)
The OR solution would have different results in cases where a record matches multiple search criteria - in that case, the current solution would include duplicate rows.

I'm not entirely sure why returning duplicate rows would be desired, but that's a different WTF.

Re: The New TODO and More

2012-11-28 09:03 • by Quango
395683 in reply to 395667
Larry:
The problem with regexe is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.


So by this logic the entire application framework and operating system would have to be replaced. And the hardware, and the drivers.

Or you could write some unit tests on the regex expression that validate what it's doing.

Re: The New TODO and More

2012-11-28 09:07 • by MySQL geek (unregistered)
Actually, here UNION is the same as OR, but in the MySQL point of view, presents a better performance ;)

Re: The New TODO and More

2012-11-28 09:09 • by @Deprecated
395685 in reply to 395667
Larry:
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.


In many cases it is safest NOT to write your own! EG., encryption. Or did I just win a "whooosh"?

Did he really have to use charAt for two linear ranges?
I think that if (c >= 'a' && c <= 'z') is less error-prone than "abcdefghijklmnpqrstuvwxyz".chartAt(c) != -1
Quick, spot the error!

Re: The New TODO and More

2012-11-28 09:11 • by Brillant (unregistered)
TRWTF is to say that regular expressions are tidy and save processing power :-)))

Re: The New TODO and More

2012-11-28 09:20 • by TGV
395687 in reply to 395667
Larry:
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.

Since you write "regexen", as in one VAX, two VAXen, I'm assuming you're trolling. Congratulations!

Re: The New TODO and More

2012-11-28 09:24 • by Mike Woodhouse (unregistered)
Need also to fix incorrect use of non-existent words.

http://www.grammar-monster.com/easily_confused/alot_a_lot_allot.htm

Re: The New TODO and More

2012-11-28 09:25 • by anonymouse (unregistered)
395689 in reply to 395667
Larry:
The problem with regexen is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.

10/10 would rage again

Re: The New TODO and More

2012-11-28 09:32 • by the beholder (unregistered)
395690 in reply to 395685
@Deprecated:


In many cases it is safest NOT to write your own! EG., encryption. Or did I just win a "whooosh"?
I'm pretty sure you did. Congratulations!

@Deprecated:
Did he really have to use charAt for two linear ranges?
I think that if (c >= 'a' && c <= 'z') is less error-prone than "abcdefghijklmnpqrstuvwxyz".chartAt(c) != -1
Quick, spot the error!
You mean other than the absence of 'o', or the fact the original did account for uppercase letters while you didn't bother going that far?

Re: The New TODO and More

2012-11-28 09:37 • by mozbo
Dave's not here, man.

Re: The New TODO and More

2012-11-28 09:47 • by Larry (unregistered)
395694 in reply to 395683
Quango:
Larry:
The problem with regexe is a lot like the problem with library functions: you can never be entirely sure what they're doing. So, in cases like this where the results are important, it is safest to write your own.


So by this logic the entire application framework and operating system would have to be replaced. And the hardware, and the drivers.
Well frankly I've never cared for the leading OS options all that much, so yeah. And drivers, aren't they the things that create fatal crashes all the time?

As for hardware, I suppose you're still buying yours from China? Hmmm?

If you're in a situation where results matter, safest bet is to make your own. If you want it done right, do it yourself.

You know how free trade is always destroying local economies? Buy from your own town, not online? Well get more local. Only trade with people on your own block. In your own house. Your own bedroom. See where this is going?

Re: The New TODO and More

2012-11-28 09:48 • by Herr Otto Flick (unregistered)
TRWTF is Whitesmiths braces, amiright. Allman or bust, that's what I say.

Re: The New TODO and More

2012-11-28 09:53 • by Bernie The Bernie (unregistered)
How does it come that you do not know what's the num variable for? Has following function been deleted:
function checkNum(sStr)

{
var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
var num = '0123456789';
var Char;
var isNum = true;
for (var i=0;(i<sStr.length) && (isNum==true);i++)
{
Char = sStr.charAt(i);
if (num.indexOf(Char)==-1)
{
isNum=false;
}
}
return isNum;
} //end function

Please add it back to the code base!

Re: The New TODO and More

2012-11-28 09:54 • by Jack (unregistered)
395698 in reply to 395683
Quango:
So by this logic the entire application framework and operating system would have to be replaced. And the hardware, and the drivers.
True story: My first computer was from Atari, and I had to write my own modem driver and printer driver. I even separated the print formatting commands from the document formatting codes so that I could buy a different printer someday and not have to modify all my documents.

At least it came with a disk driver. But I had to add a layer on top of that so that a single file could be larger than a whole floppy disk.

It's not that hard. You can do it if you want.

Re: The New TODO and More

2012-11-28 09:55 • by Cyberzombie (unregistered)
395699 in reply to 395667
"^\w*$"

Simple enough.

Re: The New TODO and More

2012-11-28 09:55 • by Captcha:sino (unregistered)

Re: The New TODO and More

2012-11-28 09:59 • by Tom (unregistered)
395702 in reply to 395699
Cyberzombie:
"^\w*$"

Simple enough.
What if you're using a language where $" is a built-in variable, and double-quoted strings interpolate $Vars? Oh, and newline doesn't necessarily mean end of statement?

It's never as simple as it looks.

Re: The New TODO and More

2012-11-28 10:00 • by Delve
395703 in reply to 395667
Because there will be a time when it's a good idea to write more code for simple tasks that have been done a million times over like confirming a string is composed only of the basic Latin alphabet.

When results are important you go with what works rather than ginning up your own solutions with all the inherent human error that new code implies.

Re: The New TODO and More

2012-11-28 10:03 • by Cyberzombie (unregistered)
395704 in reply to 395702
Tom:
Cyberzombie:
"^\w*$"

Simple enough.
What if you're using a language where $" is a built-in variable, and double-quoted strings interpolate $Vars? Oh, and newline doesn't necessarily mean end of statement?

It's never as simple as it looks.

If somebody knows how to use a regex, then they better know how to program in that language - though this is TDWTF...so ignore what I typed :)

Re: The New TODO and More

2012-11-28 10:27 • by DCRoss
395705 in reply to 395702
Tom:
Cyberzombie:
"^\w*$"

Simple enough.
What if you're using a language where $" is a built-in variable, and double-quoted strings interpolate $Vars? Oh, and newline doesn't necessarily mean end of statement?

It's never as simple as it looks.

And what if your compiler only produces eight bit code and both of your legs are broken? Then how would you solve the problem?

Re: The New TODO and More

2012-11-28 10:32 • by chris (unregistered)
395706 in reply to 395682
Peteris:
The OR solution would have different results in cases where a record matches multiple search criteria - in that case, the current solution would include duplicate rows.

I don't think it would. Assuming that SQL Server and the w3schools page I just read follow standard practice, UNION ALL concatenates the results of the queries, including duplicates, but UNION returns a unique set.

Re: The New TODO and More

2012-11-28 11:00 • by Geoff (unregistered)
395707 in reply to 395672
That is problem with query optimize though. If your application *needs* the performance right now okay, sometimes you have to code around the ugly. In general though I'd say if your database is not stressed and performance is alright, I'd rather right clean code, at least when it ought to be possible for an optimizer to deal with well, than stick in some ugly workaround.

If you have to use multiple selects and UNION to get reasonable execution plans for *simple* OR conditions, the problem is your database software.

Re: The New TODO and More

2012-11-28 11:00 • by Leander (unregistered)
String searchString="select * from ProjectAndTask_view";
if (searchWhere !=""){
searchString=searchString + " where ProjectCode like '" + searchWhere + "%' union " ;
searchString=searchString + "select * from ProjectAndTask_view where ProjectDesc like '" + searchWhere + "%' union " ;
searchString=searchString + "select * from ProjectAndTask_view where TaskCode like '" + searchWhere + "%' union " ;
searchString=searchString + "select * from ProjectAndTask_view where TaskDesc like '" + searchWhere + "%'" ;
}
searchString = searchString + " ORDER BY sortval";


Actually, this is very possible.
I had todo a similar things for performance issues.
A union sometimes will perform aster then combining multiple ORs.

In my case though the query was created at runtime, and it could not be predicted at coding time. The ORsolution was sometimes fast, sometimes very slow.
Using the UNION alternative was "not to slow, not to fast" (almost) all of the time.

Re: The New TODO and More

2012-11-28 11:03 • by me (unregistered)
395709 in reply to 395684
It is also faster in MSSQL

Re: The New TODO and More

2012-11-28 11:04 • by Neil (unregistered)
395710 in reply to 395699
Cyberzombie:
"^\w*$"

Simple enough.
1. Inside a double quoted string, \ is typically an escape character. Even if there is no special meaning for \w you will still lose the \.

2. Inside a regex, \w typically matches underscores too.

Re: The New TODO and More

2012-11-28 11:06 • by Geoff (unregistered)
395711 in reply to 395686
They usually do save processing over say a mess of split(),join(),mid$(),left$(),right$(),For i = 0 to len(str); if str[i] = .... EndFor

stuff

Re: The New TODO and More

2012-11-28 11:33 • by Tristram (unregistered)
TRWTF with the last one is using a boolean variable to indicate that it's time to break out of the loop, instead of using GOTO, like God intended.

Re: The New TODO and More

2012-11-28 11:47 • by Jim (unregistered)
395715 in reply to 395705
DCRoss:
And what if your compiler only produces eight bit code and both of your legs are broken? Then how would you solve the problem?
I think at that point I'd have to interview at another employer, so thank you for your time.

Re: The New TODO and More

2012-11-28 11:47 • by Unicorn #2816 (unregistered)
395716 in reply to 395713
Tristram:

goto TRWTF
FIX:
use GOTO, like God intended.
goto DONE
TRWTF:
using a boolean variable to indicate that it's time to break out of the loop
goto FIX
DONE:

FTFY

Re: The New TODO and More

2012-11-28 11:49 • by Frak (unregistered)
395717 in reply to 395707
Geoff:
I'd rather right clean code
and dirty English. Or did you left your brain home today?

Re: The New TODO and More

2012-11-28 12:00 • by Will (unregistered)
395718 in reply to 395705
DCRoss:
And what if your compiler only produces eight bit code
If you're as good as you think you are, you ought to be able to get the complier to output the desired 16-bit instruction by tricking it into emitting two unrelated 8-bit instructions.

Re: The New TODO and More

2012-11-28 12:09 • by Steve The Cynic
395719 in reply to 395717
Frak:
Geoff:
I'd rather right clean code
and dirty English. Or did you left your brain home today?

It's perfectly good English. To right something means to set it upright after it has fallen on one side (or even upside down). The RNLI (the UK's lifeboat-operating organisation, about the only UK charity I'd consider giving money to) has a stock of long-range shore-to-ship lifeboats that can right themselves.

Re: The New TODO and More

2012-11-28 12:11 • by Bob (unregistered)
395720 in reply to 395718
Will:
DCRoss:
And what if your compiler only produces eight bit code
If you're as good as you think you are, you ought to be able to get the complier to output the desired 16-bit instruction by tricking it into emitting two unrelated 8-bit instructions.
This could be automated. You could develop a table of those "unrelated 8-bit instructions" that produce the required 16 bit codes. Then, as you write what you want in 16-bit, this utility pulls together the table entries and assembles them for you.

You could call it an assembler.

Re: The New TODO and More

2012-11-28 12:12 • by Yank (unregistered)
395721 in reply to 395719
Steve The Cynic:
Frak:
Geoff:
I'd rather right clean code
and dirty English. Or did you left your brain home today?

It's perfectly good English. To right something means to set it upright after it has fallen on one side (or even upside down). The RNLI (the UK's lifeboat-operating organisation, about the only UK charity I'd consider giving money to) has a stock of long-range shore-to-ship lifeboats that can right themselves.
So you're one of those who likes to think the UK knows anything about good English?
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment