Code style related questions are very difficult as there tends to always be edge cases. If you use something multiple times for example, I think storing the property values in a separate variable always makes more sense so you have a single place where you're retrieving the value in the script. Another thing you have to think of is error handling. If you try and retrieve a property that doesn't exist, and then take an action on it (such as split), will your script fail? I don't remember if a non-existent property returns an empty string or a null but if it's the latter and you call split on it then it will fail. In those cases it's sometimes easier to say:
publicProperty = service.getProperty("cgDivision.FlowControlled")if publicProperty and mbo.getString("CGDIVISION") in publicProperty.split(","):
I have a lot of coding preferences for automation scripts but this one isn't a significant factor for me. Readability is critical, but to me I view that more as avoiding longer scripts inside a single code block (IE 75+ lines in a single IF statement), whether you have well defined variables and relationship names that make sense, you have comments where appropriate (and avoid comments that are self explanatory), avoid unnecessary logic (if your launch point is on WORKORDER.STATUS don't waste time checking that your MBO is WORKORDER and that the status attribute has changed unless you expect to use the same script in multiple places), etc.
------------------------------
Steven Shull
------------------------------
Original Message:
Sent: Fri October 08, 2021 09:58 AM
From: User1971
Subject: Coding style: short scripts vs. long scripts
It seems to me that Jython scripts can be written the long way (possibly easier to read for non-experts):
publicProperty = service.getProperty("cgDivision.FlowControlled")
propertyVals = publicProperty.split(',') #propertyVals is a Python list
woDivision = mbo.getString("CGDIVISION")
if woDivision in propertyVals:
mbo.setValue("FLOWCONTROLLED",1)
Or, the short way (possibly harder to read):
if mbo.getString("CGDIVISION") in service.getProperty("cgDivision.FlowControlled").split(','):
mbo.setValue("FLOWCONTROLLED",1)
Although, the argument could be made that shorter scripts are ultimately easier to read, since there's less to look at and it's easier to take it all in -- in one glance.
Question:
Which coding style has worked out best for you in the long run? Does one way work out better for you when it comes to other people reading and editing your code?
I assume, even in much more complicated scenarios, that there wouldn't be a performance difference between the two.
#Maximo
#AssetandFacilitiesManagement