|
That's a mighty nice turd you found there
|
|
|
|
|
I'm converting an MS Access app to C# & SQL Server. Here's part of an Access query I just came across:
IIf(TX.TX_TYPE="R",
IIf(TX.TX_MOSTYPE="P",
IIf(TX.TX_DEST_STATUS="A","PO Receipt",
IIf(TX.TX_DEST_STATUS="U","PO Receipt Hold","Other")),
IIf(TX.TX_DEST_STATUS="A","WIP Receipt",
IIf(TX.TX_DEST_STATUS="U","WIP Hold","Other"))),
IIf(TX.TX_TYPE="I",IIf(ISNULL(RTRIM(TX.TX_MOSJOB)) Or RTRIM(TX.TX_MOSJOB)="","Shipment Return","Reverse Issue"),
IIf(TX.TX_ORIGIN_STATUS=TX.TX_DEST_STATUS,"Loc Transfer",
IIf(TX.TX_ORIGIN="SCRAPPED","Reverse Scrap",
IIf(TX.TX_ORIGIN_STATUS="J","Count Adj Up",
IIf(TX.TX_ORIGIN_STATUS="D","PN Change To",
IIf(TX.TX_ORIGIN_STATUS="I","Reverse Unplanned Issue",
IIf(TX.TX_ORIGIN_STATUS="A" And TX.TX_DEST_STATUS="U","Hold",
IIf(TX.TX_ORIGIN_STATUS="U" And TX.TX_DEST_STATUS="A","Off Hold","Other"))))))))) AS [Transaction Type]
You'll notice that
TX.TX_DEST_STATUS="A" is being evaluated more than once.
Go figure
Everything makes sense in someone's mind
moved on Thursday, January 28, 2010 7:54 AM
|
|
|
|
|
I think step 1 is to delete it and then ask what the original intention was.
|
|
|
|
|
This is where you write a bank of unit tests, based on the input/output of this clause.
Then refactor ensuring all tests keep passing.
|
|
|
|
|
With a bit of formatting, it makes 'more' sense:
IIf
(TX.TX_TYPE="R",
IIf
(TX.TX_MOSTYPE="P",
IIf
(TX.TX_DEST_STATUS="A",
"PO Receipt",
IIf
(TX.TX_DEST_STATUS="U",
"PO Receipt Hold",
"Other"
)
),
IIf
(TX.TX_DEST_STATUS="A",
"WIP Receipt",
IIf
(TX.TX_DEST_STATUS="U",
"WIP Hold",
"Other"
)
)
),
IIf
(TX.TX_TYPE="I",
IIf
(ISNULL(RTRIM(TX.TX_MOSJOB)) Or RTRIM(TX.TX_MOSJOB)="",
"Shipment Return",
"Reverse Issue"
),
IIf
(TX.TX_ORIGIN_STATUS=TX.TX_DEST_STATUS,
"Loc Transfer",
IIf(TX.TX_ORIGIN="SCRAPPED",
"Reverse Scrap",
IIf
(TX.TX_ORIGIN_STATUS="J",
"Count Adj Up",
IIf
(TX.TX_ORIGIN_STATUS="D",
"PN Change To",
IIf
(TX.TX_ORIGIN_STATUS="I",
"Reverse Unplanned Issue",
IIf
(TX.TX_ORIGIN_STATUS="A" And TX.TX_DEST_STATUS="U",
"Hold",
IIf
(TX.TX_ORIGIN_STATUS="U" And TX.TX_DEST_STATUS="A",
"Off Hold",
"Other"
)
)
)
)
)
)
)
)
) AS [Transaction Type]
Granted, nesting like this just makes it impossible to read without a gratuitous use of formatting. The original query was just not very consistent in its usage of formatting it. The above was the best I could come up with to be able to read it effectively....
|
|
|
|
|
Btw, this looks more like a coding horror than 'wicked code'.
|
|
|
|
|
It's in the coding horrors section. Or was the topic moved later with all the replies? I didn't think it was possible to do so in codeproject.
There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal
|
|
|
|
|
|
I was able to clean it up a bit more, although it took awhile.
CASE WHEN TX.TX_TYPE = 'R' AND TX.TX_MOSTYPE = 'P' AND TX.TX_DEST_STATUS = 'A' THEN 'PO Receipt'
WHEN TX.TX_TYPE = 'R' AND TX.TX_MOSTYPE = 'P' AND TX.TX_DEST_STATUS = 'U' THEN 'PO Receipt Hold'
WHEN TX.TX_TYPE = 'R' AND TX.TX_MOSTYPE = 'P' THEN 'Other'
WHEN TX.TX_TYPE = 'R' AND TX.TX_DEST_STATUS = 'A' THEN 'WIP Receipt'
WHEN TX.TX_TYPE = 'R' AND TX.TX_DEST_STATUS = 'U' THEN 'WIP Hold'
WHEN TX.TX_TYPE = 'R' THEN 'Other'
WHEN TX.TX_TYPE = 'I' AND ISNULL(RTRIM(TX.TX_MOSJOB),'') = '' THEN 'Shipment Return'
WHEN TX.TX_TYPE = 'I' THEN 'Reverse Issue'
WHEN TX.TX_ORIGIN_STATUS = TX.TX_DEST_STATUS THEN 'Loc Transfer'
WHEN TX.TX_ORIGIN = 'SCRAPPED' THEN 'Reverse Scrap'
WHEN TX.TX_ORIGIN_STATUS = 'J' THEN 'Count Adj Up'
WHEN TX.TX_ORIGIN_STATUS = 'D' THEN 'PN Change To'
WHEN TX.TX_ORIGIN_STATUS = 'I' THEN 'Reverse Unplanned Issue'
WHEN TX.TX_ORIGIN_STATUS = 'A' AND TX.TX_DEST_STATUS = 'U' THEN 'Hold'
WHEN TX.TX_ORIGIN_STATUS = 'U' AND TX.TX_DEST_STATUS = 'A' THEN 'Off Hold' ELSE 'Other'
END AS Transaction_Type
Everything makes sense in someone's mind
|
|
|
|
|
Nice to see people with some extra time on their hands
Learning without thought is labor lost; thought without learning is perilous. (Confucius)
|
|
|
|
|
It looks like Linq, pre-Linq.
Marc
|
|
|
|
|
I've seen Garbage like this before, it usually happens when engineers pretend to be programmers...
I laugh at inappropriate times
|
|
|
|
|
As you wrote: Everything makes sense in someone's mind
-muneeb
A thing of beauty is the joy forever.
|
|
|
|
|
Looks to me like a repeated event of technically challenged boss entering the room of a stressed halfwit programmer shouting: We need to add this NOW!
|
|
|
|
|
Holy sh*t...thats just horrific man. as long as I don't encounter that ever!
|
|
|
|
|
This dude is clearly a Lisp-genius, stuck with the wrong programming languge. What a pity!
........................
Life is too shor
|
|
|
|
|
I think he meant to make a Ladder; Only that it's lying on the wrong wall
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|
|
So I am not sure if this is my "worst practices" or MS'. If you start a process (console app) which makes a call to Console.WindowWidth from C# (3.5) Process object, it crashes. I guess, I'm removing my fancy console screen formatting--for automated system testing, at least.
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "DummyTest.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
namespace DummyTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world! My window is this wide:");
Console.WriteLine(Console.WindowWidth.ToString());
}
}
}
|
|
|
|
|
That makes sense because you're piping its IO (.RedirectStandardXxx = true ), so it is not connected to the console, just your starting program.
I have confirmed this by replicating your exception (in VB.NET) and then only changing to
.RedirectStandardOutput = False
and there is then no error, but it may of course not be the functionality you need.
Regards,
Mark Hurd, B.Sc.(Ma.) (Hons.)
|
|
|
|
|
Ahh... and it is by design. A little further reading indicates that when redirecting io the underlying io and error streams Console object has NOTHING to do with the console screen that might be displayed albeit blank. This was lost on me initially.
Console class members that work normally when the underlying stream is directed to a console might throw an exception if the stream is redirected, for example, to a file. Consequently, program your application to catch System.IO..::.IOException if you redirect a standard stream.
http://msdn.microsoft.com/en-us/library/system.console.aspx[^]
|
|
|
|
|
Console has a LargestWindowWidth property, you could try to check what value has when RedirectStandardOutput is true.
|
|
|
|
|
string dateStr = DateTime.Now.Day.ToString("00") + DateTime.Now.Month.ToString("00") + DateTime.Now.Year.ToString();
string timeStr = DateTime.Now.TimeOfDay.Hours.ToString("00") + DateTime.Now.TimeOfDay.Minutes.ToString("00") + DateTime.Now.TimeOfDay.Seconds.ToString("00");
string path = FileOutputDir + GetProviderName() + "-Summary-" + dateStr + "-" + timeStr + ".csv";
Another gem, from the gem of a project I've inherited. Now:
string path = string.Format("{0}{1}-Summary-{2}", FileOutputDir, GetProviderName(), DateTime.Now.ToString("ddMMyyyy-HHmmss") + ".csv");
[Edit]
I've now just discovered the method containing this code never gets called. Its a good job wwe don't have any unit tests, or I'de have to remove those too.....
CCC solved so far: 2 (including a Hard One!)
37!?!! - Randall, Clerks
|
|
|
|
|
Given the number of posts about DateTime and its formats posted here, I am totally unsurprised.
|
|
|
|
|
i get that a lot from ppl that "think" they know how to code... rather than taking the shorter quicker way to do it they take the longer, "Look at all the code ive wrote to look cool" way of doing it in the off chance it baffles with obsufacation...
View my CodePlex Projects here -> http://www.codeplex.com/site/users/view/john_crocker
|
|
|
|
|
string.Format("{0}{1}-Summary-{2:ddMMyyyy-HHmmss}.csv", ...
But please use an ISO 8601 compliant format: yyyyMMdd
|
|
|
|
|