Showing posts with label Formatting. Show all posts
Showing posts with label Formatting. Show all posts

Wednesday, June 1, 2016

Format Your Code: Part II

In my last post, I brought up some guidelines and tips to help you (and others) to more easily understand your code.  In that previous post (click here in case you haven't read that one already), I discussed Collapsible Code Regions.  In this post, I'll discuss Indentation, Line Breaks, and Empty Spaces.

Indentation

Indentation can have a tremendous impact on how quickly you can look at code and understand what it’s doing.  Consider the differences between the two code blocks below:



While the color coding that iLogic gives you does help differentiate functions and conditional statements from other data, it’s much easier to review the code on the right and easily see where each case ends.  This is not only more appealing to the eye, it’s a time saver when looking at code, especially code that someone else wrote.  A couple of quick TABs is all that is needed or you can use the Increase Indent and Decrease Indent commands that are on the Rules Editor Editing toolbar.  Hint:  If you use the Editing toolbar, you can have your cursor anywhere in the line or you can select multiple lines and use the TAB key.


Line Breaks

Line Breaks are used when a single line will wrap past the right edge of the Rules Editor window and are another tool that is used to make your code easier to follow.  Line Breaks are created by a single space and then an underscore:  _.  Care should be taken to ensure the break is placed at a spot in the line that is easily understood.  The next line should be indented so that it’s easily recognized as a continuation of the preceding line.




Line breaks keep the reader from having to scroll to the right to read all of the code.

Empty spaces


Empty spaces are another easy way to organize your code.  They are simply a few carriage returns to put some empty space between blocks of code.  Empty spaces are a visual way of telling the reader “This bit of code has nothing to do with that last bit of code”.   It sounds simple and it is simple but it goes a long way to making your code easier to follow.

"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." ~ Brian W. Kernighan

Thanks,
Randy

Tuesday, May 24, 2016

Format Your Code: Part I


Have you ever had to make changes to your iLogic code?  Or maybe you’ve had to review someone else’s code?  It can be a little overwhelming to open up an iLogic rule and find 500 lines that look like the image to the right…..

While there are no hard and fast rules about formatting code in the iLogic Rules Editor you’ll find that if you format your code following some basic guidelines and tips that you (and others) will be able to understand your code much easier, therefore saving time and money.

Over the next few blog posts I'll go over five different methods you can use to make your code more ‘user friendly’ in the iLogic Rules Editor.  Those five methods are Collapsible Code Regions, Indentation, Line Breaks, Empty Spaces and Commenting. 

Most of these methods are built in to programming environments such as the Visual Basic Integrated Development Environment.  Maybe some day we'll get them in the iLogic Rules Editor.

Collapsible Code Regions

Collapsible code regions allow you to group together bits of code which you can collapse and expand just as you do with features or components in the Inventor Model Browser.  This will help you and your reader navigate your code as well as clean up how it looks.

You define the beginning of a collapsible code region by inserting a single quote and a left square bracket: ‘[.  To define the end of the code region, insert a single quote followed by a right square bracket: ‘]. 

Notice the hierarchical outline tree that is to the left of the code.  You can also have nested collapsible code regions.

You can collapse and expand the code by clicking on the icon at the top of the region.  It will be a minus sign (-) if the code is expanded and a plus sign (+) if it’s collapsed.  A collapsed code region looks like this:

You can also collapse and expand the code by using the Outlining tools.  Place your mouse in the expanded code you wish to collapse, right click and choose Outlining > Toggle Outlining Expansion from the contextual menu.  This works the same as if you had clicked the (-) sign at the top of the code region.  The benefit here is that you don’t have to scroll the 200 lines to get to the top of the code region.  If this is done on collapsed code, it will expand.  Repeated use of the Outlining > Toggle Outlining Expansion will toggle back and forth between collapsed and expanded code.

Another wonderful Outlining tool that is right at your fingertip is the Toggle All Outlining option.  This will collapse and / or expand all collapsible code regions in the rule.  I recently had a rule what was approximately 5,000 lines and I would use the Toggle All Outlining and then expand just the section I wanted to work with.  I found it much easier than hiding a ‘here comment in my code and using the search command (I always seem to forget to delete those ‘here comments).

The other tools that are available on the Outlining fly-out are:

Stop Outlining:  This will expand all collapsed code regions and disable the ability to collapse code.  Once you’ve done this, you’ll then have a new option called Start Automatic Outlining

Collapse to Definitions:  In other programming, Collapse to Definitions will collapse your code down to the definitions in the procedure.  In iLogic, this function works the same as Toggle All Outlining.

You can also start and stop the outlining by using the button that is just above the vertical scroll bar.

"Good design adds value faster than it adds cost." ~ Thomas C. Gale

Thanks,
Randy