Showing posts with label line. Show all posts
Showing posts with label line. Show all posts

Wednesday, 19 September 2012

Splitting long lines of code

In javascript, for example, white space is pretty much ignored by the interpretor.  You can spread your code over as many lines as you think makes it more readable, just don't forget to indicate the end of the line with a semi-colon.  However, Uniface doesn't allow this, it uses the carriage return as the end of line character.  So what do you do if you have a really long line of code?

Well, for me, I tend to just leave it as a very long line of code.  If it gets too long, I'll ask my boss for an even bigger monitor :) 

I suffer a little with this practice when I'm developing on my laptop though, as I was yesterday.  This reminded me about something that I've seen used before, and that's using a Uniface feature that allows you to split a single line of code over multiple lines, like this...


  if ( value != "1" & value != "2" & value != "3" & %\
       value != "4" & value != "5" & value != "6" & %\
       value != "7" & value != "8" & value != "9" )
    value = "0"
  endif


I'm sure this is something that's documented, but what do you search for to find it?  I certainly couldn't find anything.  So here's a little tip for you, if you prefer to keep those lines a little shorter.  

One situation I have used it in before is with a long SQL command or selectdb statement, as it can help to format it in a similar way to how you might write the SQL in your favourite database tool.  For example...


  selectdb max(field6) from "entity" u_where ( %\
    (field1 = value1) & %\
    (field2 = value2) & %\
    (field3 = value3) & %\
    (field4 = value4) & %\
    (field5 = value5)) to (value6)


EDIT:  Uniface calls this the "Line Continuation Marker" and it is documented as such. 
If anyone knows where this is in the manuals, please leave a comment and let me know.

Summary: It is possible to split a single line of code over multiple lines in Uniface, but you have to do it explicitly.  

Wednesday, 30 May 2012

Undocumented feature - error stack

This is something which has previously never been documented by Compuware.  In the Uniface 9.5 manuals it is used in a code example, but isn't explained at all.   We first found it in 2008, when we were on Uniface 9.2.  I've called it an "error stack", because that's the context we use it in, but really it's accessing the process stack, so you can determine where you are within the code at runtime.  And it's as simple as this...

stack = $proccontext("STACK")

I could just leave it there, but that seems a little mean, so I'll continue :)

At this point the "stack" variable is populated with a Uniface list.  Each item in this list contains another list, which contains the following items...

  • TYPE - The type of item on the stack ("TRG" for trigger, for example)
  • INSNAM - The instance name of the form running (or application shell)
  • TRIG - The trigger of the form running ("execute", for example)
  • ENTITY - The entity name of the item (if you're in an entity or field trigger)
  • FIELD - The field name of the item (if you're in a field trigger)
  • MODEL - The model name of the item (if you're in an entity or field trigger)
  • MODNAM - The name of the item (the entry or global procedure name, for example)
  • LNR - The line number within the item
  • LIN - The line of code itself

So you can loop through this list (forwards if you want it like a stack, or backwards if you want it to be more readable) to determine how you managed to end up where you currently are, down to the line number.  This is really useful for outputting when errors occur, especially in global procedures, as you can see what form was running at the time, right back to the application shell.

Thanks to Mark R for telling me about this one!

Summary: It is possible to determine exactly where you are within code at runtime, which is really useful for error messaging.