Click here to Skip to main content
15,896,359 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: See the writing on the wall... Pin
BobJanova3-May-12 0:59
BobJanova3-May-12 0:59 
GeneralRe: See the writing on the wall... Pin
BillW334-May-12 2:45
professionalBillW334-May-12 2:45 
GeneralRe: See the writing on the wall... Pin
Manfred Rudolf Bihy3-May-12 2:56
professionalManfred Rudolf Bihy3-May-12 2:56 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 4:03
CDP18023-May-12 4:03 
GeneralRe: See the writing on the wall... Pin
OriginalGriff3-May-12 6:01
mveOriginalGriff3-May-12 6:01 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:36
CDP18023-May-12 12:36 
GeneralRe: See the writing on the wall... Pin
Brisingr Aerowing3-May-12 6:36
professionalBrisingr Aerowing3-May-12 6:36 
GeneralRe: See the writing on the wall... Pin
spotsknight3-May-12 8:43
spotsknight3-May-12 8:43 
ok, this makes me question some of my code. I am pretty much self taught and don't have a lot of resources available other than online and within the help features. I comment quite a bit and try to use easy to understand variables (because otherwise a few months down the road when I need to add an update, I need to look back and know what I was doing).

However, I do use empty catch blocks fairly often. Typically what I will do is set a variable to a default, then use a DB query to populate the variable with in a try/catch block (if the query returns no record it throws an exception). Then after the catch block, I'll check if the variable is the default value or not and execute code accordingly.

Is there a better way or a standard practice that would be better? (see my sample snippet below, I use the prefix lowercase L to indicate the scope of variables that I reuse often)NOTE: This is a Windows Form app not a web site

C#
int HCID = -1;
try
{
    lSQL = "Select HCID from HistoricalConversion where CoNum = '" + HCCoNum.Text + "'";
    lAccessCmd.CommandText = lSQL;
    HCID = (int)lAccessCmd.ExecuteScalar();
}
catch (Exception ex)
{//Ignore error if no record exists
}

//Only run this code if there is an active historical conversion for this company
if (HCID > 0)
{
   //My code to run a stored Procedure if the record exists
}

GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:26
CDP18023-May-12 12:26 
GeneralRe: See the writing on the wall... Pin
spotsknight4-May-12 4:37
spotsknight4-May-12 4:37 
GeneralRe: See the writing on the wall... Pin
Jörgen Andersson4-May-12 10:31
professionalJörgen Andersson4-May-12 10:31 
GeneralRe: See the writing on the wall... Pin
spotsknight4-May-12 12:31
spotsknight4-May-12 12:31 
GeneralRe: See the writing on the wall... Pin
Chris Ross 28-May-12 5:51
Chris Ross 28-May-12 5:51 
JokeRe: See the writing on the wall... Pin
Tom Delany3-May-12 9:51
Tom Delany3-May-12 9:51 
GeneralRe: See the writing on the wall... Pin
CDP18023-May-12 12:33
CDP18023-May-12 12:33 
GeneralRe: See the writing on the wall... Pin
RobEpworth3-May-12 18:47
RobEpworth3-May-12 18:47 
GeneralRe: See the writing on the wall... Pin
Matthys Terblanche3-May-12 21:44
Matthys Terblanche3-May-12 21:44 
GeneralRe: See the writing on the wall... Pin
Jeremy Hutchinson4-May-12 1:32
professionalJeremy Hutchinson4-May-12 1:32 
GeneralRe: See the writing on the wall... Pin
CDP18024-May-12 2:24
CDP18024-May-12 2:24 
GeneralRe: See the writing on the wall... Pin
Jeremy Hutchinson4-May-12 2:29
professionalJeremy Hutchinson4-May-12 2:29 
GeneralRe: See the writing on the wall... Pin
Dan Neely4-May-12 2:37
Dan Neely4-May-12 2:37 
GeneralRe: See the writing on the wall... Pin
CDP18024-May-12 2:41
CDP18024-May-12 2:41 
GeneralRe: See the writing on the wall... Pin
JackDingler4-May-12 4:27
JackDingler4-May-12 4:27 
GeneralRe: See the writing on the wall... Pin
bkebamc4-May-12 5:47
bkebamc4-May-12 5:47 
GeneralRe: See the writing on the wall... Pin
KP Lee5-May-12 9:50
KP Lee5-May-12 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.