Rules Formatting - Please Format Your Rules!!

PFugereCSO
Contributor

General Formatting

One of the most important things you can do, to make you rules readable and helpful, is making sure they are properly formatted. There are some simple rules you should probably follow all the time:

  1. Proper Case – Function names are not case-sensitive; for example, api.data.calculate, Api.Data.Calculate, or API.DATA.CALCULATE, are treated as the same. Still, it is a best practice of VB style to be consistent and to capitalize judiciously. Proper case throughout the rules file makes it much easier to read.
  2. Always Comment – Add comments to most lines of code explaining what you are doing and why. Use the apostrophe at the beginning of the comment to make sure it is not interpreted as part of the rules. If this is done in a VB editor, it should turn green by default.
  3. Indentation is critical for readability – This is especially true when using any nested statements or conditionals. Those are explained below. Indentation should be done for any scripting, even scripting objects in Business Rules Editor.
  4. Variable and Constants properly named – All variables should be given useful names.

Line Continuations

Properly formatting rules will typically also mean using the underscore ‘_’ and colon ‘:’ symbols.

When using space + underscore, you are telling the script that the command continues on the next line. For example:

 

api.data.Calculate("F#[Bad Debt]:A#[EBITDAVar] = " & _
    "(A#54100:S#Budget:F#None-A#54100:S#Actual:F#None)")​

 

The colon allows you to combine two lines. For example:

 

strSalesAccount = “A#7999.UD1#Sales” : strMktAccount = “A#7999.UD1#Marketing”

 

Comments

I think it is important to emphasize the importance of comments.

You will not remember in a year or two why something is quite the way it is, so comments will help you from making the same mistake again. It can help provide a new administrator detailed information, such as what needs updating or regular maintenance, if you add a new cash flow account for example. Finally it can help remind you what needs to be considered for an upgrade, or rebuild.

Variables & Constants

Variables and constants are used to hold values or expressions.

Think back to your 9th grade algebra class. In 2+y = x, y is the variable. (See, your teacher was right, this may prove useful yet...) Variables can have any name; but while ‘y’ and ‘x’ are valid names, they don't tell you anything. Names should be something that makes sense. Consider which of the following is easier to follow: 2 + y = x or 2 + strVariablePercent = strPercentMarkUp. I would say you can understand more form the second line of rules than the first, even without knowing the context. Add a line of comments, and note the proper case, and you are on your way to well formatted descriptive rules.

A Variable is a value that changes depending on parameters and when it is used; whereas a Constant will not change, regardless of when it is used or changes in the application. You will want to declare constants at the beginning of rules files. They can be available to all procedures at all times. Apart from that, they are used just like variables.

You should have some guidelines when writing rules; one of the simplest things to do, to keep yourself organized, is to have a naming convention. I like to use a prefix. The prefix is something that helps me remember what is in the variable. I might use ‘str’ or ‘s’ for a string, or ‘bln’ or ‘b’ for a Boolean (true or false), and ‘nbr’ or ‘n’ for number. Then using proper case I use a descriptive label for my variable. So, for a number from Net Income, my variable might be called ‘nbrNetIncome’. I can see that variable name anywhere in my file and know what the variable is for and what it is. Compare that with ‘x’; if I just see x, who knows what it is for.

It also helps to know what you are going to use the variable for. We have two names for variables; Replacement Variables and Execution Variables. Replacement Variables are typically used for constants like static strings (for example topUD1=“.UD1#TopUserDefined1”). This variable might change, but it is replacing some part of a string. Execution Variables are typically used for situations in which variable is populated or reset during some condition or rule (for example sPOVEntity = api.POV.Entity.Name). The point of view changes constantly and what would be written in the variable would be updated accordingly.

There are some rules for variable names that you just must follow, to write valid VB.Net. They must always begin with a letter. They cannot contain a period. You should avoid keywords such as “OneStream”, “Entity”, “Account”, when naming variables; they tend to be reserved by OneStream and could cause problems if shadowed.

VB.Net requires you to declare variables before using them. Since variables will require what type they can hold, you need to make sure you avoid letting the variable use a type that Rules Engine is not expecting for that member. For example, if you write a rule checking if the year is 2010, OneStream could see that as something different than “2010”. By using the quotes and declaring it As String, the number 2010 becomes a String of text, “2010”. Otherwise you might get a Type Mismatch error in some situations; if you do get this error, double check that the variable you are testing is correctly declared.
PFugereCSO_0-1645809383539.png

2 REPLIES 2

Wikus
New Contributor III

Do you think it is worthwhile to start using C# now that it's available in 7.1?

JackLacava
Community Manager
Community Manager

Absolutely. However, consider carefully where it can be used (unlike VB.Net, there are some restrictions) and who might have to later change your code - since C# might be harder to read for non-programmers.