Click here to Skip to main content
15,883,705 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: Familiarity : Trimming leading and trailing spaces from a string... Pin
Brady Kelly29-Sep-12 6:39
Brady Kelly29-Sep-12 6:39 
GeneralRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Jamercee28-Sep-12 4:23
Jamercee28-Sep-12 4:23 
GeneralRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Blake Miller28-Sep-12 4:52
Blake Miller28-Sep-12 4:52 
GeneralRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Martin081528-Sep-12 4:24
professionalMartin081528-Sep-12 4:24 
GeneralRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Chris Boss28-Sep-12 4:35
professionalChris Boss28-Sep-12 4:35 
QuestionRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Julien Villers2-Oct-12 23:57
professionalJulien Villers2-Oct-12 23:57 
GeneralRe: Familiarity : Trimming leading and trailing spaces from a string... Pin
Joe_Dert20-Oct-12 21:09
Joe_Dert20-Oct-12 21:09 
JokeParsing string to Integer with try and catch??? Pin
Marco Bertschi25-Sep-12 1:27
protectorMarco Bertschi25-Sep-12 1:27 
[Edit]
I know it is not a crime, but I had to fight against inconsistent variable names and bit of spaghetti code.
But the most important thing is: This piece of code has to parse a huge amount of data (up to 500000 rows with parameters and so on) so you remember if it needs 50 or 4 seconds to parse the parameter values (overall time).
[Edit end]


I found this piece of code in a project which I had to expand. Notice the guy who wrote the code was at the end of his apprenticeship with already 4 years experience in coding. D'Oh! | :doh: WTF | :WTF:

C#
public void LogFileBreakDown(string channel, string channelnumber)
{
   int channelnumber = 0;
   
   try
   {
       channelnumber = Int32.Parse(channelnumber);
   }
   catch(Exception ex)
   {
       channelnumber = 0;
   }
   
   //Removed some specific code

   return;
}


I replaced Parse with TryParse -> and the log file was loading about 30 times faster then before.

[EDIT: Added the new source as it is now]
C#
public string Aufschluesselung_sys(String logFileLineText, String channelNumber, String channelName, bool showWarnings,  ref string warnings, CultureInfo messageLanguage)
        {
            string sName, sID, sSplit = logFileLineText;
            string[] mySplit = sSplit.Split(new Char[] { '>', '<', '"' });
            string ctrlCommonSpecification = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), ConfigurationManager.AppSettings["PathToCtrlCommonSpecificationXml"]);
            string cryptoModuleSpecification = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), ConfigurationManager.AppSettings["PathToCryptoModuleSpecificationXml"]);
            string ctrlDcSlaveSpecificationXml = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), ConfigurationManager.AppSettings["PathToCtrlDcSlaveSpecificationXml"]);
            string ctrlIseSpecification = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), ConfigurationManager.AppSettings["PathToCtrlISESpecificationXml"]);

            XmlDocument XmlAufschluesselung = new XmlDocument();

            int iID;
            if (mySplit.Count() >= 2)
            {
                if (Int32.TryParse((mySplit[1]), out iID))
                {
                    //XML File öffnen welches der gewünschte Wert enthält
                    if (Suche.Suchen(logFileLineText, (channelNumber + ">")) || Suche.Suchen(logFileLineText, (channelNumber + "<")))
                    {
                        if (iID >= 1 && iID <= 102)
                        {
                            XmlAufschluesselung.Load(ctrlCommonSpecification);
                        }
                        else
                        {
                            if (iID == 257)
                            {
                                XmlAufschluesselung.Load(cryptoModuleSpecification);
                            }
                            else
                            {
                                if (iID >= 1001 && iID <= 4228)
                                {
                                    XmlAufschluesselung.Load(ctrlDcSlaveSpecificationXml);
                                }
                                else
                                {
                                    if (iID >= 4288 && iID <= 4565)
                                    {
                                        XmlAufschluesselung.Load(ctrlIseSpecification);
                                    }
                                }
                            }
                        }

                        

                        XmlNode controllerNode = XmlAufschluesselung.SelectSingleNode("Controller");
                        if (controllerNode != null)
                        {
                            //XML File durchsuchen nach der gewünschten ID und den Namen dazu
                            foreach (XmlNode xmlNode in controllerNode.ChildNodes)
                            {
                                if (xmlNode.Name.Equals("Command"))
                                {
                                    
                                    sName = xmlNode.Attributes["name"].Value.ToString();
                                    sID = xmlNode.Attributes["id"].Value.ToString();
                                    if (sName == "RebootICSoftware")
                                    {
                                        Console.WriteLine("FOUND");
                                    }
                                    if (iID.ToString() == sID)
                                    {
                                        mySplit[1] = mySplit[1].Replace(iID.ToString(), sName);
                                    }
                                }
                            }
                        }
                        else
                        {
                            ResourceManager translator = new ResourceManager("LogFileInterpreter.resource_language", typeof(LogfileBreakDown_C111).Assembly);
                            string warningMessage = translator.GetString("msgNotAbleToResolveCommandNameFromXmlByCommandId", messageLanguage);
                            WriteWarningLog(translator.GetString("msgNotAbleToResolveCommandNameFromXmlByCommandId", new CultureInfo("EN")) + ";" + logFileLineText);
                            warnings = "WARNING:\n" + warningMessage + "\nLine text: " + logFileLineText + "\n\n" + warnings;
                            if (showWarnings)
                            {
                                ShowMessageForm showMessageForm = new ShowMessageForm(MessageType.Warning, "WARNING:\n" + warningMessage + "\n\n " + logFileLineText);
                                showMessageForm.ShowDialog();
                            }
                        }

                        logFileLineText = logFileLineText./*Substring(0, 68).*/Replace(channelNumber, channelName) + mySplit[1] + '"' + mySplit[2] + '"';
                    }
                }
            }
            return logFileLineText;
        }

Intelligence is not like IntelliSense


modified 1-Oct-12 3:05am.

GeneralRe: Parsing string to Integer with try and catch??? PinPopular
Pete O'Hanlon25-Sep-12 2:04
mvePete O'Hanlon25-Sep-12 2:04 
GeneralRe: Parsing string to Integer with try and catch??? Pin
Marco Bertschi25-Sep-12 2:57
protectorMarco Bertschi25-Sep-12 2:57 
GeneralRe: Parsing string to Integer with try and catch??? Pin
dojohansen2-Oct-12 3:34
dojohansen2-Oct-12 3:34 
GeneralRe: Parsing string to Integer with try and catch??? Pin
OriginalGriff25-Sep-12 3:32
mveOriginalGriff25-Sep-12 3:32 
GeneralRe: Parsing string to Integer with try and catch??? Pin
Marco Bertschi25-Sep-12 3:36
protectorMarco Bertschi25-Sep-12 3:36 
GeneralRe: Parsing string to Integer with try and catch??? Pin
KP Lee28-Sep-12 22:17
KP Lee28-Sep-12 22:17 
AnswerRe: Parsing string to Integer with try and catch??? Pin
Marco Bertschi30-Sep-12 21:01
protectorMarco Bertschi30-Sep-12 21:01 
GeneralRe: Parsing string to Integer with try and catch??? Pin
dojohansen2-Oct-12 3:48
dojohansen2-Oct-12 3:48 
GeneralRe: Parsing string to Integer with try and catch??? PinPopular
BobJanova25-Sep-12 3:53
BobJanova25-Sep-12 3:53 
GeneralRe: Parsing string to Integer with try and catch??? Pin
Marco Bertschi25-Sep-12 4:05
protectorMarco Bertschi25-Sep-12 4:05 
AnswerRe: Parsing string to Integer with try and catch??? Pin
Julien Villers25-Sep-12 23:45
professionalJulien Villers25-Sep-12 23:45 
GeneralRe: Parsing string to Integer with try and catch??? Pin
BobJanova26-Sep-12 0:12
BobJanova26-Sep-12 0:12 
GeneralRe: Parsing string to Integer with try and catch??? Pin
Julien Villers26-Sep-12 1:15
professionalJulien Villers26-Sep-12 1:15 
GeneralRe: Parsing string to Integer with try and catch??? Pin
dojohansen2-Oct-12 3:51
dojohansen2-Oct-12 3:51 
GeneralRe: Parsing string to Integer with try and catch??? Pin
agolddog28-Sep-12 3:44
agolddog28-Sep-12 3:44 
AnswerRe: Parsing string to Integer with try and catch??? Pin
Marco Bertschi28-Sep-12 4:04
protectorMarco Bertschi28-Sep-12 4:04 
GeneralRe: Parsing string to Integer with try and catch??? Pin
zenwalker19859-Oct-12 17:58
zenwalker19859-Oct-12 17:58 

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.