Hello There, Guest!
View New Posts  |  View Today's Posts
Worst code I've worked with so far...

  • 0 Vote(s) - 0 Average


06-27-2017, 09:33 AM #1
Mazzn
ლ(ಠ益ಠლ)
*******
Administrators
Posts: 198 Threads:16 Joined: Sep 2013 Reputation: 19

Bug  Worst code I've worked with so far...
Working here is nice.

I like the people and I like programming. At least I did, but today I've found this... gem of coding genius. Suddenly I hate my job.

The program I am supposed to fix is a sales report that lists sales (duh) from 1993 until today. Except that it's not working correctly since the beginning of 2017: Only sales up to December 31st 2016 show up. A Y2K bug that's 17 years late?

The program promises lots of fun right from the start. I am greeted with the following code:
Code:
DATA: FROM1993 LIKE RMCB0-TODA VALUE '19930101'.
DATA: TO1993 LIKE RMCB0-TODA VALUE '19931231'.

DATA: FROM1994 LIKE RMCB0-TODA VALUE '19940101'.
DATA: TO1994 LIKE RMCB0-TODA VALUE '19941231'.

DATA: FROM1995 LIKE RMCB0-TODA VALUE '19950101'.
DATA: TO1995 LIKE RMCB0-TODA VALUE '19951231'.

DATA: FROM1996 LIKE RMCB0-TODA VALUE '19960101'.
DATA: TO1996 LIKE RMCB0-TODA VALUE '19961231'.

DATA: FROM1997 LIKE RMCB0-TODA VALUE '19970101'.
DATA: TO1997 LIKE RMCB0-TODA VALUE '19971231'.

DATA: FROM1998 LIKE RMCB0-TODA VALUE '19980101'.
DATA: TO1998 LIKE RMCB0-TODA VALUE '19981231'.

DATA: FROM1999 LIKE RMCB0-TODA VALUE '19990101'.
DATA: TO1999 LIKE RMCB0-TODA VALUE '19991231'.

DATA: FROM2000 LIKE RMCB0-TODA VALUE '20000101'.
DATA: TO2000 LIKE RMCB0-TODA VALUE '20001231'.

DATA: FROM2001 LIKE RMCB0-TODA VALUE '20010101'.
DATA: TO2001 LIKE RMCB0-TODA VALUE '20011231'.

DATA: FROM2002 LIKE RMCB0-TODA VALUE '20020101'.
DATA: TO2002 LIKE RMCB0-TODA VALUE '20021231'.

DATA: FROM2003 LIKE RMCB0-TODA VALUE '20030101'.
DATA: TO2003 LIKE RMCB0-TODA VALUE '20031231'.

DATA: FROM2004 LIKE RMCB0-TODA VALUE '20040101'.
DATA: TO2004 LIKE RMCB0-TODA VALUE '20041231'.

DATA: FROM2005 LIKE RMCB0-TODA VALUE '20050101'.
DATA: TO2005 LIKE RMCB0-TODA VALUE '20051231'.

DATA: FROM2006 LIKE RMCB0-TODA VALUE '20060101'.
DATA: TO2006 LIKE RMCB0-TODA VALUE '20061231'.

DATA: FROM2007 LIKE RMCB0-TODA VALUE '20070101'.
DATA: TO2007 LIKE RMCB0-TODA VALUE '20071231'.

DATA: FROM2008 LIKE RMCB0-TODA VALUE '20080101'.
DATA: TO2008 LIKE RMCB0-TODA VALUE '20081231'.

DATA: FROM2009 LIKE RMCB0-TODA VALUE '20090101'.
DATA: TO2009 LIKE RMCB0-TODA VALUE '20091231'.

DATA: FROM2010 LIKE RMCB0-TODA VALUE '20100101'.
DATA: TO2010 LIKE RMCB0-TODA VALUE '20101231'.

DATA: FROM2011 LIKE RMCB0-TODA VALUE '20110101'.
DATA: TO2011 LIKE RMCB0-TODA VALUE '20111231'.

DATA: FROM2012 LIKE RMCB0-TODA VALUE '20120101'.
DATA: TO2012 LIKE RMCB0-TODA VALUE '20121231'.

DATA: FROM2013 LIKE RMCB0-TODA VALUE '20130101'.
DATA: TO2013 LIKE RMCB0-TODA VALUE '20131231'.

DATA: FROM2014 LIKE RMCB0-TODA VALUE '20140101'.
DATA: TO2014 LIKE RMCB0-TODA VALUE '20141231'.

DATA: FROM2015 LIKE RMCB0-TODA VALUE '20150101'.
DATA: TO2015 LIKE RMCB0-TODA VALUE '20151231'.

DATA: FROM2016 LIKE RMCB0-TODA VALUE '20160101'.
DATA: TO2016 LIKE RMCB0-TODA VALUE '20161231'.

Alright, quite the adventure... A variable pair for each year's beginning and end. One might say it's easy to program this dynamically, but beware of the dreaded year in which January 2nd follows December 33rd! Better hard code it. I see why 2017 might not work for this program though.

As I scroll down, I notice something odd: the scrollbar is way too tiny for this kind of program. Behold, this report has 2700 lines of code! Almighty, what have I gotten into?


Turns out they at least knew how to use functions. There's quite a few in there!
Note: "FORM" is the ABAP Equivalent of a function, or rather subroutine
Code:
FORM SALES_1993.
[...]
FORM SALES_1994.
[...]
FORM SALES_1995.
[...]
FORM SALES_1996.
[...]
FORM SALES_1997.
[...]
...and so on, up to 2016. I'm starting to see a pattern and now really understand why it worked over the past years but doesn't anymore. Oh, did I say they knew how to use functions? My apologies. Apparently they really did not. Neither did they know about function parameters or for-loops, which could've saved around, oh, maybe 2600 lines of code here. Whatever.

Then I read the actual implementations. In disbelief I see: each of these functions just calls another function. This time it has parameters: The previously hard coded date variables. All the functions are identical otherwise. Why are they there?

I continue to scroll over lines of identical code, but suddenly stumble upon something else: FORM SALES_1993_1. FORM SALES_1994_1. And so on. All these functions I just read aren't just copies of each other. They're duplicated twice each, with a single parameter changed for the function call within. And in the main program it's not even an if-else construct around all of it. Or a variable to set which one to use. Oh no. It's this, over and over, in pairs of two years each for some reason:
"PERFORM" calls a subroutine, as you've likely guessed. "NE" means "Not Equal" (also <> in ABAP)
Code:
IF SOME-VALUE = 'P'.
 PERFORM SALES_1993_1. "Sales for 1993
 WRITE: '1993'.
 WRITE: SALES DECIMALS 0. "Print line
 PERFORM SALES_1994_1. "Sales for 1994
 WRITE: '1994'.
 WRITE: SALES DECIMALS 0. "Print line
ENDIF.
IF SOME-VALUE NE 'P'.
 PERFORM SALES_1993 "Sales for 1993
 WRITE: '1993'.
 WRITE: SALES DECIMALS 0. "Print line
 PERFORM SALES_1994. "Sales for 1994
 WRITE: '1994'.
 WRITE: SALES DECIMALS 0. "Print line
ENDIF.

IF SOME-VALUE = 'P'.
 PERFORM SALES_1995_1. "Sales for 1995
 WRITE: '1995'.
 WRITE: SALES DECIMALS 0. "Print line
 PERFORM SALES_1996_1. "Sales for 1996
 WRITE: '1996'.
 WRITE: SALES DECIMALS 0. "Print line
ENDIF.
IF SOME-VALUE NE 'P'.
 PERFORM SALES_1995 "Sales for 1995
 WRITE: '1995'.
 WRITE: SALES DECIMALS 0. "Print line
 PERFORM SALES_1996. "Sales for 1996
 WRITE: '1996'.
 WRITE: SALES DECIMALS 0. "Print line
ENDIF.

Please save me, I have entered spaghetti code hell. There's so much wrong with all of this. Everything is just wrong. This code is not just bad. It's evil. Maybe this is the price I have to pay for my sins in life...
Visit me at mazzn.net & blog.mazzn.net!
//This is very important :)

Self.KeepImproving(true);


07-01-2017, 02:33 PM #2
AceInfinity
Developer
*******
Administrators
Posts: 9,731 Threads:1,024 Joined: Jun 2011 Reputation: 76

RE: Worst code I've worked with so far...
They hardcoded every year up until 2016? lol. Let me guess the guy probably got fired last year and decided to truncate that down for someone else to maintain?

Unless you have time to completely re-write it, I guess you'll have to hardcode using what they've already got.

Code:
DATA: FROM2017 LIKE RMCB0-TODA VALUE '20170101'.
DATA: TO2017 LIKE RMCB0-TODA VALUE '20171231'.

IF SOME-VALUE = 'P'.
PERFORM SALES_2017_1.             "Sales for 2017
WRITE: '1993', SALES DECIMALS 0.
PERFORM SALES_2018_1.             "Sales for 2018
WRITE: '1994', SALES DECIMALS 0.
ELSE.
PERFORM SALES_2017                "Sales for 2017
WRITE: '1993', SALES DECIMALS 0.
PERFORM SALES_2018.               "Sales for 2018
WRITE: '1994', SALES DECIMALS 0.
ENDIF.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲

07-05-2017, 02:18 PM #3
Mazzn
ლ(ಠ益ಠლ)
*******
Administrators
Posts: 198 Threads:16 Joined: Sep 2013 Reputation: 19

RE: Worst code I've worked with so far...
The guy didn't get fired, he went into early retirement, but let me just say that everyone likes me more than they liked him.

I'm pretty sure a rewrite will take about an hour (didn't get to it yet) - even if I'm lazy I can at least write a loop and just call the function (the one with the parameters) instead of, you know... This.

But yeah, he really did hard code every year since 1993. At some point even another dev added a year. They probably decided "wtf I'm not dealing with this" and left it. I think my predecessor barely knew any programm, so it's impressive that he got as far as he did and our material management works as well as it does!

Found some code from him once that was like this, but harmless in comparison...
Code:
CASE group.          "= switch(group)
WHEN '001' OR '002'. "= case "001"; case "002"; ... break;
"Do stuff
WHEN '003' OR '004' OR '005'.
"Do other stuff.
ENDCASE.
Looks fine? Yeah, until you notice that OR doesn't work with CASE and it actually only checked the part before the OR, then failed and dropped into the next case. So here group 002, 004 and 005 will always drop into the default case. Nobody ever noticed because by chance groups 2, 4 and 5 needed the same coding as the default case... Still, this should not happen, ever! To be fair, it was a printing form and you can't properly debug them. It's a big hassle and there's no syntax check. At least when writing form creating using SAPScript, a technology from like the 80's...
Visit me at mazzn.net & blog.mazzn.net!
//This is very important :)

Self.KeepImproving(true);


07-07-2017, 09:00 PM #4
AceInfinity
Developer
*******
Administrators
Posts: 9,731 Threads:1,024 Joined: Jun 2011 Reputation: 76

RE: Worst code I've worked with so far...
Yeah, doesn't matter what you learn you'll always find terrible things out in the real world. I've seen code that shows many people shouldn't have become programmers to some extent.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
  Sonic-Pi - Music as code AceInfinity 2 1,712 04-28-2016, 02:32 AM
Last Post: AceInfinity
  Compile Code Online cjohnsonky 5 2,687 06-25-2015, 10:40 AM
Last Post: Nuno Brito
  Why SublimeText2 is one of the best code editors AceInfinity 3 2,976 03-18-2013, 06:18 PM
Last Post: AceInfinity
   TLF Code Vault KoBE 14 5,646 04-30-2012, 02:10 PM
Last Post: KoBE
   TLF Code Vault KoBE 47 14,075 11-25-2011, 10:59 PM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)