Click here to Skip to main content
15,887,175 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: Since when does "optional" mean "required"? PinPopular
OriginalGriff21-Nov-14 8:11
mveOriginalGriff21-Nov-14 8:11 
GeneralRe: Since when does "optional" mean "required"? Pin
PIEBALDconsult21-Nov-14 8:19
mvePIEBALDconsult21-Nov-14 8:19 
GeneralRe: Since when does "optional" mean "required"? Pin
OriginalGriff21-Nov-14 8:38
mveOriginalGriff21-Nov-14 8:38 
GeneralRe: Since when does "optional" mean "required"? Pin
newton.saber21-Nov-14 8:52
newton.saber21-Nov-14 8:52 
GeneralRe: Since when does "optional" mean "required"? Pin
OriginalGriff21-Nov-14 9:04
mveOriginalGriff21-Nov-14 9:04 
GeneralRe: Since when does "optional" mean "required"? Pin
dan!sh 29-Nov-14 20:31
professional dan!sh 29-Nov-14 20:31 
GeneralRe: Since when does "optional" mean "required"? Pin
User 110609796-Dec-14 4:34
User 110609796-Dec-14 4:34 
GeneralNeither weird nor beatyful PinPopular
Lutosław18-Nov-14 12:31
Lutosław18-Nov-14 12:31 
A true horror. There were elections in Poland recently... and a software which was supposed to help in counting votes hasn't been working. It came out, that one day BEFORE elections it's source code was released to public on GitHub. It is full of security holes... In an availible manual there is a screenshot with actual login info and basically anyone can log in and manipulate results. The code looks like it was written by a single student who learns c#.
... and it doesn't work, also. It is a few days after elections and still no results. Here are some samples:
a constructor of an apparently important class ProtocolForm
C#
		public ProtocolForm(ProtocolsList form, XmlDocument header, string protocolDefinition, string candidates, string committee, string validateDefinition, string save, string OU, string licensePath, string version)
	this.InitializeComponent();
	this.tooltipErrors = new ToolTip();
	this.isKLKCan = true;
	this.isKLK = true;
	this.isKLKPro = true;
	this.isKLKWali = true;
...
	string[] p = protocolDefinition.Split(new char[]
	{
		'\\'
	});
	this.protocolDefinitionName = p[p.Length - 1].Replace('_', '/').Replace(".xml", "");
	string[] p2 = candidates.Split(new char[]
	{
		'\\'
	});
	this.candidatesName = p2[p2.Length - 1].Replace('_', '/').Replace(".xml", "");
	string[] p3 = committee.Split(new char[]
	{
		'\\'
	});

... (~100 lines)

		foreach (XmlNode xObwod in headerRoot)
		{
			if (xObwod.Attributes["nr"].InnerText == obwod)
			{
				foreach (XmlNode xInst in xObwod)
				{
					if (xInst.Attributes["kod"].InnerText == inst)
					{
						foreach (XmlNode xobw in xInst)
						{
							if (xobw.Attributes["nr"].InnerText == okreg && System.Convert.ToInt32(xInst.Attributes["inst_jns"].InnerText) == System.Convert.ToInt32(this.instJNS))
							{
								organNazwa = xInst.Attributes["organNazwa"].InnerText;
								break;
							}
						}
					}
				}
			}
		}
... (lines lines...)
		if (inst == "RDA")
		{
			if (jns.Length < 6)
			{
				while (jns.Length < 6)
				{
					jns = "0" + jns;
				}
			}
			if (jns[2] == '7' || jns[2] == '6')
			{
				if (jns.Substring(0, 4) == "1465" && organNazwa == "m.st.")
				{
					this.protocolDefinition = new XmlDocument();


... (~200 lines)
	}
	catch (XmlException e)
	{
		MessageBox.Show("Nieprawidłowy XML: " + e.Message, "Błąd");
	}
... code code...
}

There are three methods: bool saves(int step), string generateSaves(int step) and void saves(int step, string errors). Each has 600 lines and it's copy&paste with some subtle changes. And they say that they lose data...

And a 800-line void getHeader(). Yep, it has a "get" in name and is a void...
C#
{
	this.wait.setWaitPanel("Trwa ładowanie nagłówka protokołu", "Proszę czekać");
	this.wait.setVisible(true);
	this.headerField = new System.Collections.Generic.List<string>();
	string title = "Protokół dla ";
	try
	{
		XmlNode nodesList = this.protocolDefinition.SelectSingleNode("/protokol_info");
		XmlNode headerRoot = this.header.SelectSingleNode("/akcja_wyborcza/jns");
		int x = 0;
		int y = 0;
		int fullWidth = this.protocolHeader.Size.Width - 20;
		XmlNode updateData = this.header.SelectSingleNode("/akcja_wyborcza").Attributes.GetNamedItem("data-ost-aktualizacji");
		string[] partfilepath = this.savePath.Split(new char[]
		{
			'\\'
		});
		string[] dataPath = partfilepath[partfilepath.Length - 1].Split(new char[]
		{
			'-'
		});
		this.jns = dataPath[1].Replace("Jns", "");
		this.obwod = dataPath[2].Replace("Obw", "");
		this.inst = dataPath[3].Replace("Inst", "");
		this.okreg = dataPath[5].Replace("Okr", "");
		string[] okreg = this.okreg.Split(new char[]
		{
			' '
		});
		this.okreg = okreg[0].Replace(".xml", "");

(lines lines lines)

		foreach (XmlNode node in nodesList)
		{
			XmlNode type = node.Attributes.GetNamedItem("type");
			if (!(type.Value == "header"))
			{
				break;
			}
			foreach (XmlNode box in node)
			{
				foreach (XmlNode item in box)
				{
					if (item.Name == "title")
					{
						x = 0;
						XmlNode bold = item.Attributes.GetNamedItem("bold");
						Label lab = new Label();
						lab.Text = item.InnerText;
						lab.AutoSize = true;
						lab.MaximumSize = new System.Drawing.Size(fullWidth, 0);
						lab.Font = new System.Drawing.Font(this.myfont, 10f);
						lab.Padding = new Padding(10, 0, 10, 0);
						if (bold.Value == "true")
						{
							lab.Font = new System.Drawing.Font(this.myfont, 10f, System.Drawing.FontStyle.Bold);
						}
						lab.Location = new System.Drawing.Point(x, y);
						this.protocolHeader.Controls.Add(lab);
						y += lab.Height + 30;
					}
					if (item.Name == "row")
					{
						x = 0;


... and after 400 lines, when nesting reaches 16 levels:


										if (valueName.Value == "algorytmOKW_R")
										{
											foreach (XmlNode obw in headerRoot)
											{
												if (obw.Name == "obw" && obw.Attributes.GetNamedItem("nr") != null && obw.Attributes.GetNamedItem("nr").Value == this.obwod)
												{
													foreach (XmlNode institutions in obw)
													{
														if (institutions.Name == "inst" && institutions.Attributes.GetNamedItem("kod") != null && institutions.Attributes.GetNamedItem("kod").Value == this.inst && institutions.Attributes.GetNamedItem("inst_jns") != null && institutions.Attributes.GetNamedItem("inst_jns").Value == this.instJNS)
														{
															foreach (XmlNode okr in institutions)
															{
																if (okr.Name == "okr" && okr.Attributes.GetNamedItem("nr") != null && okr.Attributes.GetNamedItem("nr").Value == this.okreg)
																{
																	if (okr.Attributes.GetNamedItem("siedzibaR") != null)
																	{
																		Input.Text = okr.Attributes.GetNamedItem("siedzibaR").Value;
																	}
																}
															}
														}
													}
												}
											}
										}

A random gem found by people around (as I said, it was released to public):
C#
public bool isActiveLicense(string license)
{
bool response = false;
try
{
System.Security.Cryptography.X509Certificates.X509Certificate certtmp = new System.Security.Cryptography.X509Certificates.X509Certificate(license);
System.DateTime a = new System.DateTime(1, 1, 1, 0, 0, 0);
System.DateTime fromDate = System.Convert.ToDateTime(certtmp.GetEffectiveDateString());
if (fromDate == a)
{
System.Threading.Thread.Sleep(1000); // this time it will work...
fromDate = System.Convert.ToDateTime(certtmp.GetEffectiveDateString());
}
System.DateTime toDate = System.Convert.ToDateTime(certtmp.GetExpirationDateString());
if (toDate == a)
{
System.Threading.Thread.Sleep(1000);
toDate = System.Convert.ToDateTime(certtmp.GetEffectiveDateString());
}
int result = System.DateTime.Compare(fromDate, System.DateTime.Now);
int result2 = System.DateTime.Compare(System.DateTime.Now, toDate);
if (result <= 0 && result2 <= 0) // it is enough checking, right?
{
response = true;
}
}
catch (System.Security.Cryptography.CryptographicException)
{
}
return response;
}

And so on... Oh, and I have a bunch of license codes for paid libraries.
PS. I apologise for a long post.

modified 18-Nov-14 18:46pm.

GeneralRe: Neither weird nor beatyful Pin
Tom Clement18-Nov-14 13:18
professionalTom Clement18-Nov-14 13:18 
GeneralRe: Neither weird nor beatyful Pin
Lutosław18-Nov-14 13:30
Lutosław18-Nov-14 13:30 
GeneralRe: Neither weird nor beatyful Pin
Andy Brummer18-Nov-14 13:33
sitebuilderAndy Brummer18-Nov-14 13:33 
GeneralRe: Neither weird nor beatyful Pin
StM0n18-Nov-14 20:07
StM0n18-Nov-14 20:07 
GeneralRe: Neither weird nor beatyful Pin
Bernhard Hiller18-Nov-14 21:25
Bernhard Hiller18-Nov-14 21:25 
GeneralRe: Neither weird nor beatyful Pin
Lutosław19-Nov-14 1:16
Lutosław19-Nov-14 1:16 
GeneralRe: Neither weird nor beatyful Pin
James Jensen20-Nov-14 8:47
professionalJames Jensen20-Nov-14 8:47 
GeneralRe: Neither weird nor beatyful Pin
Richard Deeming19-Nov-14 4:06
mveRichard Deeming19-Nov-14 4:06 
GeneralRe: Neither weird nor beatyful Pin
Lutosław19-Nov-14 10:12
Lutosław19-Nov-14 10:12 
GeneralRe: Neither weird nor beatyful PinPopular
Jörgen Andersson19-Nov-14 22:52
professionalJörgen Andersson19-Nov-14 22:52 
GeneralRe: Neither weird nor beatyful Pin
Agent__00720-Nov-14 21:10
professionalAgent__00720-Nov-14 21:10 
GeneralRe: Neither weird nor beatyful Pin
SortaCore19-Nov-14 22:39
SortaCore19-Nov-14 22:39 
GeneralRe: Neither weird nor beatyful Pin
MehGerbil20-Nov-14 2:18
MehGerbil20-Nov-14 2:18 
GeneralRe: Neither weird nor beatyful Pin
Lutosław20-Nov-14 10:06
Lutosław20-Nov-14 10:06 
GeneralRe: Neither weird nor beatyful Pin
Tomz_KV20-Nov-14 3:34
Tomz_KV20-Nov-14 3:34 
GeneralRe: Neither weird nor beatyful Pin
BillWoodruff20-Nov-14 8:47
professionalBillWoodruff20-Nov-14 8:47 
GeneralRe: Neither weird nor beatyful Pin
Lutosław20-Nov-14 9:58
Lutosław20-Nov-14 9: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.