Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Paul Gibson
  Where is Paul Gibson?
 Durham
 United Kingdom
 Paul Gibson
 To: Cetin Basoz
  Where is Cetin Basoz?
 Izmir
 Turkey
 Cetin Basoz
 Tags
Subject: RE: Bad code I've seen
Thread ID: 138614 Message ID: 139098 # Views: 66 # Ratings: 0
Version: Not Applicable Category: Off-topic
Date: Friday, July 20, 2007 11:36:26 AM         
   


> Well to say the truth it took me less than a few seconds to see what the code was doing, it was perfectly readable, so I classify this one as good not bad. If one can see what the code is doing at a glance, I don't think it should matter to split into multiple lines (and actually he did, doing the operation in 2 steps).
> Anyway it shows 'bad and good' code is also a relative thing and personal preference at times. OTOH I also classify as bad for not having mdot.
>

You are correct, good code/bad code is subjective because I would still say that it is bad code due to the style it has been written in. I think I should be able to look at a line of code and work out instantly what it is doing and if not then some comments would be nice. I think everyone should be able to work out what this code is doing in a few seconds but it really shouldn't take that long for such a simple operation.

I used to work with someone (he was my boss at the time but not anymore) who prided himself on being able to write really complicated code in one line, where people would take multiple lines for the same operation. He believed that this achievement made him better than the other coders when in fact everyone else hated debugging his code because it was so atrociously written. Never mind everything being bunched up, he also liked single letter variable names and PUBLIC variables abound and it made his code possibly the most hated in the company.

The code in question executes the
AT(",",fstr,6)
part 3 times and the
AT(",",fstr,5)
part twice in just two lines. The coder did split the calculation up into two operations but I think that may have slightly slowed it down without really making it more readable because the code could also have been written as:

reccnt= VAL(SUBSTR(fstr,AT(",",fstr,6)+1,AT(",",fstr,7)-AT(",",fstr,6)-1)) + ;
 VAL(SUBSTR(fstr,AT(",",fstr,5)+1,AT(",",fstr,6)-AT(",",fstr,5)-1)) 


which would still split it up for readability but also remove an operation. IMO splitting the code up into even more lines would not only have made it more readable but it may have made it execute slightly faster by not executing repeated code but maybe I am wrong about that second part.

So I have a question. Would

lnFifthComma = AT(",", fstr, 5)
lnSixthComma = AT(",", fstr, 6)

reccnt = VAL(SUBSTR(fstr, lnSixthComma + 1, AT(",", fstr, 7) - lnSixthComma - 1)) + ;
 VAL(SUBSTR(fstr, lnFifthComma + 1, lnSixthComma - lnFifthComma - 1)) 


actually be slightly faster than the original code because of the removal of the repeated calls? I am genuinely asking that question as I would be interested to know.

Of course once GETWORDNUM became available to coders the snippet could have been rewritten as

reccnt = VAL(GETWORDNUM(fstr, 7, ",")) + VAL(GETWORDNUM(fstr, 6, ","))


Much more readable AND fewer lines proving that improvements in VFP do allow us to improve ourselves.



COMPLETE THREAD
Bad code I've seen Posted by Paul Gibson @ 7/17/2007 11:33:31 AM
RE: Bad code I've seen Posted by Boudewijn Lutgerink @ 7/17/2007 12:14:16 PM
RE: Bad code I've seen Posted by Ken Murphy @ 7/19/2007 2:27:44 AM
RE: Bad code I've seen Posted by Christian Tabligan @ 7/19/2007 3:12:26 AM
RE: Bad code I've seen Posted by Cetin Basoz @ 7/17/2007 12:24:11 PM
RE: Bad code I've seen Posted by Brian Walsh @ 9/7/2008 10:44:34 PM
RE: Bad code I've seen Posted by Cetin Basoz @ 9/8/2008 12:42:39 PM
RE: Bad code I've seen Posted by Ilya Rabyy @ 9/8/2008 6:11:29 PM
RE: Bad code I've seen Posted by Brad Schulz @ 9/8/2008 6:16:37 PM
RE: Bad code I've seen Posted by Ilya Rabyy @ 9/8/2008 6:32:11 PM
RE: Bad code I've seen Posted by Mike Yearwood @ 9/9/2008 1:07:25 AM
RE: Bad code I've seen Posted by Cetin Basoz @ 9/9/2008 1:07:04 AM
RE: Bad code I've seen Posted by Mike Yearwood @ 9/9/2008 3:38:23 PM
RE: Bad code I've seen Posted by Ilya Rabyy @ 9/9/2008 4:55:21 PM
RE: Bad code I've seen Posted by Mike Yearwood @ 9/10/2008 10:08:37 PM
RE: Bad code I've seen Posted by Ilya Rabyy @ 9/9/2008 4:43:28 PM
RE: Bad code I've seen Posted by Bernard Bout @ 9/12/2008 8:33:55 AM
RE: Bad code I've seen Posted by Cetin Basoz @ 9/12/2008 3:09:16 PM
RE: Bad code I've seen Posted by Mike Yearwood @ 9/12/2008 4:00:48 PM
RE: Bad code I've seen Posted by Bernard Bout @ 9/13/2008 6:01:30 AM
RE: Bad code I've seen Posted by Andy Kramek @ 7/17/2007 12:55:31 PM
RE: Bad code I've seen Posted by Thomas Bähr @ 7/17/2007 1:05:35 PM
RE: Bad code I've seen Posted by Andy Kramek @ 7/17/2007 11:52:45 PM
RE: Bad code I've seen Posted by Paul Gibson @ 7/18/2007 10:31:47 AM
RE: Bad code I've seen Posted by Anders Altberg @ 9/8/2008 1:29:58 PM
RE: Bad code I've seen Posted by Andy Kramek @ 9/8/2008 2:36:10 PM
RE: Bad code I've seen Posted by Brad Schulz @ 9/8/2008 3:52:38 PM
RE: Bad code I've seen Posted by Andy Kramek @ 9/9/2008 7:05:38 AM
RE: Bad code I've seen Posted by Jim Booth @ 9/9/2008 4:29:05 PM
RE: Bad code I've seen Posted by Andy Kramek @ 9/10/2008 1:33:51 PM
RE: Bad code I've seen Posted by Brad Schulz @ 9/9/2008 6:19:29 PM
RE: Bad code I've seen Posted by Jim Booth @ 9/9/2008 7:23:14 PM
RE: Bad code I've seen Posted by Andy Kramek @ 9/10/2008 1:39:51 PM
RE: Bad code I've seen Posted by Thomas Bähr @ 7/17/2007 1:02:29 PM
RE: Bad code I've seen Posted by Eric den Doop @ 7/18/2007 11:41:02 AM
RE: Bad code I've seen Posted by Tamar Granor @ 7/18/2007 10:19:47 PM
RE: Bad code I've seen Posted by Andy Kramek @ 7/19/2007 2:28:35 PM
RE: Bad code I've seen Posted by Barbara Peisch @ 7/19/2007 5:54:40 AM
RE: Bad code I've seen Posted by Eric den Doop @ 7/19/2007 7:00:56 AM
RE: Bad code I've seen Posted by Barbara Peisch @ 7/19/2007 8:00:59 AM
RE: Bad code I've seen Posted by tushar @ 7/19/2007 2:36:45 PM
RE: Bad code I've seen Posted by Eric den Doop @ 7/19/2007 2:57:49 PM
RE: Bad code I've seen Posted by Christian Pano @ 7/19/2007 11:34:31 AM
RE: Bad code I've seen Posted by Cetin Basoz @ 7/19/2007 12:12:56 PM
RE: Bad code I've seen Posted by Paul Gibson @ 7/19/2007 12:21:32 PM
RE: Bad code I've seen Posted by Cetin Basoz @ 7/19/2007 12:42:34 PM
RE: Bad code I've seen Posted by Paul Gibson @ 7/20/2007 11:36:26 AM
RE: Bad code I've seen Posted by Cetin Basoz @ 7/20/2007 3:13:48 PM
RE: Bad code I've seen Posted by Christian Pano @ 7/23/2007 11:10:22 AM
RE: Bad code I've seen Posted by John Peart @ 7/19/2007 12:18:54 PM
RE: Bad code I've seen Posted by Ken Murphy @ 7/19/2007 2:09:01 PM
RE: Bad code I've seen Posted by tushar @ 7/19/2007 2:24:51 PM
RE: Bad code I've seen Posted by Cetin Basoz @ 7/19/2007 2:44:42 PM
RE: Bad code I've seen Posted by tushar @ 7/19/2007 3:42:47 PM
RE: Bad code I've seen Posted by Ken Murphy @ 7/19/2007 8:22:04 PM
RE: Bad code I've seen Posted by Barbara Peisch @ 7/19/2007 8:34:36 PM
RE: Bad code I've seen Posted by Ken Murphy @ 7/19/2007 10:19:29 PM
RE: Bad code I've seen Posted by Barbara Peisch @ 7/19/2007 10:26:38 PM
RE: Bad code I've seen Posted by Thomas Bähr @ 7/20/2007 11:11:53 AM
RE: Bad code I've seen Posted by Ronan Masangcay @ 7/20/2007 4:48:50 AM
RE: Bad code I've seen Posted by Mike Yearwood @ 7/21/2007 2:35:23 AM
RE: Bad code I've seen Posted by Kurt Westerlund @ 9/12/2008 4:57:50 PM