A true confession: I absolutely cannot successfully edit a crontab
file without spending a lot of time reading docs on what all the little date/time/interval flags mean. Partially, it's just that I don't do it very often, but mainly the information flies right out of my head once I've done it. I can absolutely understand why someone might want to write a little helper program to help themselves manage their crontab
.
I just can't understand why they'd write this one, sent to us Beorn. We'll have to take this one in chunks, because it's 18,905 lines.
We'll start with the function dayofweek
, so you can get a sense of the pattern that gets used. Here's the first if
condition for its core logic:
if (this.myObject.chkBoxMonday.Checked)
{
k = 0;
pauto_day_of_week = k.ToString();
serie = true;
if (this.myObject.chkBoxTuesday.Checked)
{
ktransit = 1;
serie = true;
}
else
{
k = 2;
serie = false;
}
if (this.myObject.chkBoxWednesday.Checked)
{
if (serie)
{
ktransit = 2;
}
else
{
pauto_day_of_week = pauto_day_of_week + "," + k.ToString();
k = 3;
serie = true;
}
}
else
{
if ((serie) & (k <= ktransit)) pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
serie = false;
k = 3;
}
if (this.myObject.chkBoxThursday.Checked)
{
if (serie)
{
ktransit = 3;
}
else
{
pauto_day_of_week = pauto_day_of_week + "," + k.ToString();
k = 4;
serie = true;
}
}
else
{
if ((serie) & (k <= ktransit)) pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
serie = false;
k = 4;
}
if (this.myObject.chkBoxFriday.Checked)
{
if (serie)
{
ktransit = 4;
}
else
{
pauto_day_of_week = pauto_day_of_week + "," + k.ToString();
k = 5;
serie = true;
}
}
else
{
if ((serie) & (k <= ktransit)) pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
serie = false;
k = 5;
}
if (this.myObject.chkBoxSaturday.Checked)
{
if (serie)
{
ktransit = 5;
}
else
{
pauto_day_of_week = pauto_day_of_week + "," + k.ToString();
k = 6;
serie = true;
}
}
else
{
if ((serie) & (k <= ktransit)) pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
serie = false;
k = 6;
}
if (this.myObject.chkBoxSunday.Checked)
{
if (serie)
{
ktransit = 6;
pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
}
else
{
pauto_day_of_week = pauto_day_of_week + "," + k.ToString();
k = 7;
serie = true;
}
}
else
{
if ((serie) & (k <= ktransit)) pauto_day_of_week = pauto_day_of_week + "-" + ktransit.ToString();
k = 7;
serie = false;
}
}
If "Monday" is checked, then we check each successive day of the week, compiling our pauto_day_of_week
string. The purpose of this block is to determine if we have a contiguous series of days, or run on independent days.
And this is the logic for Monday. Similar logic is repeated for every other day of the week.
At this point you say to yourself: "Sure, this is an absolutely bonkers way to write this, but it's only seven days, that's gonna be what, 513 lines of code? Where's the rest of that 18kLoC coming from?"
My friends, let me introduce you to dayofmonth
:
public void dayofmonth()
{
Int32 k, ktransit = 0;
bool serie = false;
if (this.myObject.chkBoxDay1.Checked)
{
k = 0;
pauto_day_of_month = k.ToString();
serie = true;
if (this.myObject.chkBoxDay2.Checked)
{
ktransit = 1;
serie = true;
}
else
{
k = 2;
serie = false;
}
// … snip
Once again, we take each individual day of the month, and create an if-branch to compare it against each other day of the month.
You should be unsurprised that there's also a month
function:
public void month()
{
Int32 k, ktransit = 0;
bool serie = false;
if (this.myObject.chkBoxJanuar.Checked)
{
k = 0;
pauto_month = k.ToString();
serie = true;
if (this.myObject.chkBoxFebruar.Checked)
{
ktransit = 1;
serie = true;
}
else
{
k = 2;
serie = false;
}
//… snip
And an hours
, and minutes
. Which thank the gods of bad code, they only operate in 10 minute increments.
Someone wrote this. I imagine they used a lot of copy/paste- I hope they used a lot of copy/paste, but someone built this monstrosity, looked at that closing bracket on like 18,905, and said: "Yes, this is good. I'm going to use this."
There is so much code, we cannot include the entire snippet here. To spare your browsers, we'll just link to a text version of the code.