in

Kodigo

An all purpose Powerbuilder framework

Common, lightweight services changed to overloaded global functions

Last post 03-16-2008 20:04 by yeyi. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 03-12-2008 19:53

    • yeyi
    • Top 10 Contributor
    • Joined on 02-01-2008
    • Singapore
    • Posts 127

    Common, lightweight services changed to overloaded global functions

    I decided to change some of the most common services into global functions with overloads. The topic can be found here. As long as it is only edited and saved in source view, it should be OK. Opening it via painter will only show you one function.

    Here are some of the functions:

    • SizeOf - get's the size, in bytes, for primitive types and structures.
    • IsNull - same as MS SQL IsNull function
    • IIF - same as datawindow if() function
    • IsEmptyOrNull - checks for empty or null for all datatypes

    The purpose of these functions is to make code more readable while reducing footprint.

    Before:

    n_svc_mgr lnv_svc; n_svc_isempty lnv_check; n_svc_sizeof lnv_size

    //load services

    lnv_svc.of_LoadSvc(lnv_check, CSvc.ISEMPTY);  lnv_svc.of_LoadSvc(lnv_size, CSvc.SIZEOF)

    bitmapinfoheader lstr 

    long ll_size; string ls_text 

    ll_size = lnv_size.SizeOf(lstr)

    IF lnv_check.of_IsEmpty(ls_text) THEN  ....

    After: 

    long ll_size; string ls_text 

    ll_size = SizeOf(lstr)

    IF IsEmptyOrNull(ls_text) THEN  ....

    The code is shorter now, but we have to live with these functions staying in memory. The old way ensures that the services will be destroyed as soon as the service manager goes out of scope. But since these services are more like code functions, I changed them.

    I would rather focus on functions that will get used a lot. Can you think of any other candidates?

  • 03-13-2008 7:30 In reply to

    Re: Common, lightweight services changed to overloaded global functions

    I think it is a good idea to do the more common functions this way. How about a ReplaceAll function? Here is the code I use.

    public function string of_replaceall (string as_oldstring, string as_findstr, string as_replace);String ls_newstring
    Long ll_findstr, ll_replace, ll_pos

    // get length of strings
    ll_findstr = Len(as_findstr)
    ll_replace = Len(as_replace)

    // find first occurrence
    ls_newstring = as_oldstring
    ll_pos = Pos(ls_newstring, as_findstr)

    Do While ll_pos > 0
     // replace old with new
     ls_newstring = Replace(ls_newstring, ll_pos, ll_findstr, as_replace)
     // find next occurrence
     ll_pos = Pos(ls_newstring, as_findstr, (ll_pos + ll_replace))
    Loop

    Return ls_newstring

    end function

  • 03-13-2008 22:54 In reply to

    • yeyi
    • Top 10 Contributor
    • Joined on 02-01-2008
    • Singapore
    • Posts 127

    Re: Common, lightweight services changed to overloaded global functions

    Function has been added (w/ overload for ignoreCase). Incidentally, this function is also present in StringService class but I will keep the function in the service and probably point it to the global one.

  • 03-14-2008 17:28 In reply to

    • mkramer
    • Top 50 Contributor
    • Joined on 03-15-2008
    • Denmark
    • Posts 1

    Re: Common, lightweight services changed to overloaded global functions

    As far as I remember (sorry for any blur in my memory), global functions do not stay in memory. They are by definition only referenced when running. At every call, you risk that the function is:

    1. Not found in memory
    2. Loaded from disk
    3. Executed - that's really what you wanted in the first place
    4. Marked as garbage since "usage counter" is now zero again
    5. Removed from memory at next garbage collection

    So what you gain in readability during development and maintenance, your users will pay continously during execution. I'd prefer a shortnamed global utilities class that I instantiate at application startup to lock that code block into memory for duration of my application. Just haul all your commonly used cleverly named methods into that gnv_utils class.

     And why not throw all of your generally used constants into that class as well?

    Myself? I have a gnv_gui class for all of my GUI related utilities and a gnv_util for everything else like DataLength (MSSQL inspired SizeOf(*))

    /MicKr-

    /MicKr-
    Michael Kramer, Denmark
    Certified PB Professional, PB3-PB9
    Certified ScrumMaster
  • 03-16-2008 12:55 In reply to

    • chriss
    • Top 25 Contributor
    • Joined on 03-16-2008
    • Posts 3

    Re: Common, lightweight services changed to overloaded global functions

     Actually I like the "service" idea as a collection of functions.

    But i prefer to use it this way:

     n_svc_string str

     str = create n_svc_string

     This we can do globally or not.

    My little contribution to what i also use often:

    f_gettokenfromlist //Get a specific value from a string like "aaa#bbb#ccc"

    f_getnumberoftokens //Count the number of values in "aa#bbbbb#ccc"

    f_populate_lb_from_db //Populate a listbox from database table 

     http://rapidshare.com/files/100055780/stringfunctions.zip.zip.html 

  • 03-16-2008 20:04 In reply to

    • yeyi
    • Top 10 Contributor
    • Joined on 02-01-2008
    • Singapore
    • Posts 127

    Re: Common, lightweight services changed to overloaded global functions

    mkramer:

    1. Not found in memory
    2. Loaded from disk
    3. Executed - that's really what you wanted in the first place
    4. Marked as garbage since "usage counter" is now zero again
    5. Removed from memory at next garbage collection

     

    Interesting, I always though of global functions as static, but what you mention does make sense since global functions are inherited from type function_object instead of having some static identifier. Thanks for pointing this out :)

    If this is the case, I will have to overload them so they still use a ServiceManager internally and have the option of calling the overload to pass an instance of ServiceManager to circumvent performance issues.  I'll try this out first and let you know how it goes. What I want to achieve is both readbility and performance. Worse case is we fall back to the original Kodigo approach where you will have to use ServiceManager to request a service (e.g. StringService) then call the appropriate function (stringService.ReplaceAll(...))

    I would want to use zero global variables as much as possible when it comes to the kodigo core classes, the ServiceManager approach allows static-like services in memory across calls and auto clean up if usage is zero or can be pooled to remain in memory throughout the lifetime of the aplication, similar to global variables. It has worked well for the previous version and will still carry over for the new one. I do agree that to a certain extent, global variables will be used for extended frameworks built on top of Kodigo. I have one that has an application manager (ala PFC) declared globally only for the purpose of routing application events, but no driect access to the global variable was made, the instance is always passed as a reference to the required class.

Page 1 of 1 (6 items)
Gabriel Abulencia dela Torre 2008
Powered by Community Server (Non-Commercial Edition), by Telligent Systems