Thursday, February 9, 2017

2E Code Review - pet hates - part I

Today I thought I'd get a few things off my chest. 

Whenever I review code, look at old code or have the job of maintaining code (not my favourite part of the job by a long shot) I am often left dumbfounded.  I see the same old mistakes being made regardless of where I have worked. You will often hear me saying,"Whoever wrote this should be shot!!".

The most annoying part is that in many places the developers agree about the best way to code but quickly use the excuse of "I don't have enough time", or "Well it's done now, so don't worry" to not code properly.  My experience tells me it is often the most experienced developer(s) who are the hardest to convince of solid development standards with the terms "Old dog" and "new tricks" preeminent in my mind....

IMHO the difference between a run of the mill programmer (I've worked with plenty of these) and a good one (fewer but they are out there) is adherence to the little details that make long term maintainability of your code as easy as it should.  This is especially true considering the inevitable change and enhancement that is required during an applications growth and evolution.  I have espoused the standard development quote that 90% of an applications life cycle is maintenance numerous times before.

Anyhow, my post today is a list of pet hates that I see when people are "unprofessional", "selfish","crude", "Lazy" with their 2E coding.

1. Legacy commented out code.

I recently worked on some maintenance and had to work through some action diagram code.  Using the Find Services option within 2E I started to quickly get frustrated that the majority of the usages that were commented out.

What was worse still is that there were developer comments (from 2002) stating that the code was commented out and I know that this area has been enhanced many times since.  There is simply no reason to keep such old code.  The developer concerned is normally quite good but has a couple of really bad habits like this one.

Here is a snippet of code from an old function (screen print kindly shown with permission).  Note, I have redacted any client or developer or brand comments (hence some empty white space).


I am all for commenting out code as part of debugging, rapid prototyping, unit testing and quick wins (hot fixes) where you are not quite sure of the change you are making, but please annotate when and why.  However, I am aware that some of this code was moved to their own program(s) so IMHO should just have been removed.

Associated hates:
  • Having to weed through the chaff to get to the code to change.
  • Any old code will not have been maintained so will not (often) be fit for purpose (if you decided to uncomment it) so why leave it there.
  • Extra impact analysis (especially for internal functions). 
  • Confusion with the impact analysis usage.  A future post is planned for this.
  • Better to take a version.
  • Commenting out code doesn't change the timestamp for the AD line so we don't know when it was commented out.
Tip: Comment out code sparingly and preferably not at all.  Be confident with your code and solution.  After you have completed the work (and tested it), if you are 100% happy with the results revisit the action diagram and remove commented out code.  Commented out code is a maintenance burden and others will not understand why.

Here is the same code with the bad commented out code and legacy comments removed.

Still not perfectly structured but a whole lot more readable.  Perhaps CA can add a hide/show commented out code option for the action diagrammer.

2. "GET All fields" RTVOBJ not doing as described.

Every 2E developer has written the standard RTVOBJ that returns the full record. Yes, we all have forgotten the *MOVE ALL, but my biggest annoyance is when people don't visit these functions when the underlying file structure changes.

Imagine the "*GET", "GET Record", "Get All", "RTV All Fields" function without the new fields as parameter.

The next developer comes along and low and behold, there are a few fields missing.  Then they create a new one called Get All (New) and probably flag the other one with some kind of encoding system like DNU for Do Not Use etc.

Associated hates:

Numerous versions of the same type of function.  Too many choices.
Description vs reality can be misleading and frustrating.

Tip: If you have to change the file, you may as well change the "Full Record" type function as you have to regenerate up everything anyhow.

3. Using all 9 parameter definition lines for functions parameters.

In the old days when we only had *FIELD, Access Path(s) or Structure files for defining parameters it could sometimes get a little busy and we'd fill the 9 lines leaving limited options for the next developer.  We shouldn't create structure files just for the sake of it, after all, their primary purposes was for consistency of repeating data groups like audit stamps etc not for easier passing of parameters.

Since version 4.0 (over 20 years) we've been able to define parameter arrays.  There really is no excuse now for taking up the last line of a functions parameter block unless "I don't have time" or "I'll do it when we get to 10" is a valid reason.

Tip: When maintaining a function and say 6 or more parameter block lines are used consider refactoring with a parameter array.

I still think that even using parameter arrays as being a bastardised solution for this problem and that 2E should just have had more that 9 lines....but it is the lesser or two evils.

This is the tip of the iceberg.  Plenty more to follow I am sure.

Thanks for reading.

Tuesday, January 24, 2017

Don’t forget the *return code

When we are developing in 2E we often reach the 50 file limit if we are generating for RPG.  RPG ILE (RP4) is more resilient as it allows more files to be opened.  I’ve preached before regarding proper function construction. 

Do you really need 50 or more files open for any given process? 
Can your function be better constructed or to be accurate………deconstructed?

What are the options?

1.       The easiest method to get over these limits is to change to RP4.
2.       The best is probably to re-architect your function properly and switch to RP4.
3.       The next best approach is to probably hive off a chunk of the processing into a new function to reduce the open files.
4.       The worst thing to do, is to externalise a single RTV in order to get under the limit.  As this can cause all sort of issues.
o   The 50 file limit will be breached on the next maintenance (most likely) leaving the potential for a string of EXT/RTV type functions.
o   If called in a loop or RTVOBJ then an external (if not set to Close Down ‘No’) may impact performance.

Another item to add the list of don’ts (above) is the preservation of the *Return Code.  An Execute External Function does not automatically return a *Return Code.  By default, standard processing is to pass back *Normal or an empty *Return Code.

If you are relying on a *Return Code for your now externalised RTVOBJ it will always come back as empty (*Normal)

unless you explicitly pass the return code back with the *EXIT PROGRAM built in function.

Another trap for young players but something that catches even seasoned developers out.  Had the RP4, re-architect or block of code to EEF option been taken the issue was likely to not have manifested. 

Good programming practice is to always be conscious of the *return code and more importantly (where required) test for it.
Thanks for reading.

Tuesday, January 17, 2017

Condition of the timestamp field type


Recently we had a small issue in our office with a program not providing the expected results when a case statement was created to see if the timestamp was entered or not.

Here is a sample program that I mocked up in another model to demonstrate the point.

The example below is a simple EEF (Execute External Function) that compares a couple of timestamp fields.  The first LCL.Lee's Timestamp is initialised with a value of JOB.*System timestamp.  In the second part field LCL.Lee's Timestamp 2 is left uninitialised so this will be NULL so in synon 2E terms that means '0001-01-01-'.

As one field is initialised and the other isn't you expect that the code to be performed will generate two messages in the joblog and these would say 'Timestamp is entered' and 'Timestamp not entered".

However, as you can see from below the messages are not as expected.

Let us take a little look as to the potential reasons why this has happened. First of all let's take a look at the generated code (RPG) for the comparison conditions.

As you can see these are pretty standard apart from the fact that they are comparing a CONSTANT value rather than a field or a hardcoded value like 'A' which you often see for status fields etc.

In our case we created a condition called 'Entered' for the timestamp field.  We left it blank (upon creation) and 2E conveniently defaulted it to the NULL value '0001-01-01-'

Now, we know that 2E generates an array for the constants used by a program and references them in the code.  The array is 25 long(see above) and the value(s) have been correctly placed in the program source as below.

The condition value is stored as a reference via file YCNDDTARFP.  You will see that this condition references to another surrogate within 2E.

The clue is given in the source code snippet above where it refers to long constants.  Taking a look at the file YCONDTARFP for 1003038 we see the value that is used by the source.

 You can also see that this is 25 long in the file and as per the array declaration above.

Herein lies the issue....  The code is looking to compare the timestamp value vs a constant value.  The timestamp field is 26 long and has 6 decimal places of precision for the millisecond element.  If we place the program in debug and interrogate the value in LCL.Lee's Timestamp 2 we see that our field as below

  But, this is being compared to below.  These two are NOT the same.....

Now we understand the problem we need to consider how we can fix the issue.  Fortunately there are several workarounds for this...

  1. Move the value (LCL.Lee's Timestamp 2) into a shorter field 25 long (truncating the last 0).  We can then compare a 25 long field with a 25 long constant.  As we are really only ever interested in EQ, NE, GT or LT comparison operators this will be okay.
  2. We can modify the source (highlighted above) to set the constant array to 25 and add a 0 to the CN value.  But, we use a code generator so this is only recommended if you use source modifier programs and the pre-processor.
  3. Our chosen option was to NOT compare to a condition for 'Entered' or 'Not Entered' etc and instead compare to an empty field.  We created a NULL Timestamp field and referred to its LCL context for a comparison (LCL so that it is always initialised within it function bounds) and not compromised like the WRK context.
The code below is a simple example of how to implement option 3.

 The results are now as expected.

I've raised the issue with CA and expect a response soon.  Perhaps it is fixed, or on a list.  But considering it will mean a generator change and a file change for YCONDTARFP the workarounds above may be a better option so I hope that you all find this useful.

Thanks for reading.

EDIT - I have heard from CA and the workaround option is the current recommend method. I agree with them that the scope and size of the change is quite high. Pick one of the three options above that works for your shop.  Happy to hear if there are other options.  LEe. 23/01/2017.