Tag Archives: Tips

DNS – Tweaks

DNS speed can really affect your home router either for the better or worse..

I use a open source bit of software called namebench to periodically verify that I am using the most performant/reliable DNS server.

Take a peek, it really does help… http://code.google.com/p/namebench/

Method Chaining

Creating objects with a complex constructor can be a bit of a pain in any language. One technique I have used is method chaining. It is not applicable to every type of class but it can be useful.

Method chain can help simply the use class and allows more complex object initialisation without having to worry about the order of the parameters and be done inline.

Consider the use of an “Account” class that takes Name, Address, Telephone and Country. All of which are strings, the constructor with four strings would not be a great constructor.

So you could have a simple constructor and four properties/methods.. however so set up the object would mean you have spread the setup over multiple lines.

Using method chain you can overcome this and even space in the “value” area of the object in your storage area.

set x to new type Account::Name("xx")::Address("yy") ::Telephone("yy")
     ::Country("zz")::World("Earth")

The trick of the pattern is to provide methods that always return this/self, so we can change the invokes together… For example:

This technique could even be used to build up a series of items required, for example, the preparation and execution of a sql statement comes to mind… in a similar fashion to Linq.

So… lets look at an example:

$set ilusing"System.Collections.Generic"
program-id. Program1 as "MethodChaining1.Program1".
data division.
working-storage section.
01 accounts type List[type Account] value new type List[type Account].      
01 jAccount type Account value
   new type Account::Name("Mr Johnson")::Address("Somewhere, some place")
    ::Telephone("+44 1234 4321").
01 sAccount type Account value
   new type Account::Name("Mr Smith")::Address("Nowhere place")
     ::Telephone("+44 1234 4321")::Country("Wales").                
01 lAccount type Account.
procedure division.
 invoke accounts::Add(jAccount)
 invoke accounts::Add(sAccount)
 
 perform varying lAccount through accounts
  display lAccount
 end-perform

 goback.
end program.

WIth the class being:

class-id Account.

working-storage section.
01 wName       string property as "Name".
01 wAddres     string property as "Address".
01 wCountry    string property as "Country".
01 wTelephone  string property as "Telephone".
01 wEmail      string property as "Email".

method-id New.
local-storage section.
procedure division.
  set wCountry to type System.Globalization.RegionInfo::CurrentRegion::DisplayName
end method.

method-id Name public.
procedure division using uName as string
 returning ret as type Account.
  set self::Name to uName
  set ret to self
end method.

method-id Address public.
procedure division using uAddress as string
 returning ret as type Account.
  set self::Address to uAddress
  set ret to self
end method.

method-id Telephone public.
procedure division using uTelephone as string
 returning ret as type Account.
  set self::Address to uTelephone
  set ret to self
end method.

method-id Email public.
procedure division using uEmail as string
 returning ret as type Account.
  set self::Address to uEmail
  set ret to self
end method.

method-id Country public.
procedure division using uCountry as string
 returning ret as type Account.
  set self::Country to uCountry
  set ret to self
end method.

method-id ToString public override.
procedure division returning ret as string.
set ret to String::Format("Name:{0}, Address:{1}, Telephone:{2}, Email:{3}, Country:{4}",
     self::Name, self::Address, self::Telephone,
     self::Email, self::Country)
end method.
end class.

Which when run with Visual COBOL gives:

Name:Mr Johnson, Address:+44 1234 4321, Telephone:, Email:, Country:United Kingdom
Name:Mr Smith, Address:+44 1234 4321, Telephone:, Email:, Country:Wales

12 Tips for using Micro Focus COBOL

12 Tips for using Micro Focus COBOL

As it is close to Christmas and I wanted to do something with 12 in the title, I’ve thrown together some useful if not un-ordered list of 12 things tips using Micro Focus COBOL.

Remember I am just a developer and just want to help fellow developers out :-) so…

Merry Christmas!

and here is my list…

  1. Use DLLs/Shared objects rather than .int code
  2. DLLs/Shared objects can be preloaded using:
    01 my-ptr procedure-pointer.
    set my-ptr to entry "sodllname"

    Note; sodllname does not have the extension… let the runtime choose it for you!

  3. Use a good development environment such as Visual Studio or on Unix use Eclipse offering if you have a workstation, otherwise I personally use vim/gvim if terminal access is the only choce.
  4. Remember you have a great choice of platforms from Windows to Linux to a load Unix platforms, so choice is king. (did I say that…)
  5. Try using managed code (.Net) if you want to create WPF/WinForms or use WCF
  6. Interopability is great, so if you see a Java Class, .Net Class, COM object, ‘C’ function or a Web Service… use it! You can even mix unmanaged and managed applications..
  7. Avoid using the following if performance is key, as this really does help our code generator:
    alter
    perform thru
    next sentence
    segments
    go to section-name
    go to paragraph outside the current section
  8. Try to end your routine with a “stop run [returning..]” or “exit program [returning..]” or “goback [returning..]” as this gives our code generators a defined end to the routine, which really does help our code analysis/generator
  9. Use comp-5, integers for speed in calculations or native .Net types in managed
  10. Use comp-x for portability (file formats etc..)
  11. Make your application more robust, use tracing in production systems, either our own CTF tracing or your own or third party… it will save your life one day! Use runtime routines to catch error and log them (CBL_ERROR_PROC), ensure your programs shutdown cleanly always use CBL_EXIT_PROC.
  12. Use the generator directive nocheck if you are happy your code is solid and want to a little more performance. ie: it turns off runtime bounds checking

Further reading:

  • Coding for speed, size and portability
  • Eclipse/COBOL on Unix
  • Newsletter about CTF
  • For CTF_ APIs see CBL_CTF_ see library routines
  • Yes.. its a bit of a mixed list.. but hey it may be of help!

    Java process id via java.lang.management

    While working on a project recently I need to find out the current process of the active running Java process (for tracing/auditing), however I never found a 100% perfect solution but did come across an acceptable solution to use the management classes to query its name, which happens to have encoding in it, so here is the quick solution:

    import java.lang.management.ManagementFactory;

    public class getpid
    {
      public static void main(String args[]) throws Exception
      {
          System.out.println("Process id : "+getProcessId());
      }

      public static long getProcessId()
      {
        String name = ManagementFactory.getRuntimeMXBean().getName();
        String[] nameBits = name.split("@");
       
        return nameBits == null ? -1 :  Long.valueOf(nameBits[0]);
      }
    }
    $ java getpid
    Process id : 377

    El Parador and the Choocolate Line

    Dear Webblog Diary

    Last friday night we spent the night in London and we found a really good little Spanish Tapas restaurant and I thought I would share it… just because we enjoyed it soo much..

    The place is called : El Parador – 245 Eversholt Street, London NW1 1BA.

    The next day we headed for Brugge via the Eurostar and true to form Maria (my partner) found the the best Chocolate shop in the country, which we believe is ‘The Chocolate Line‘.

    foodies enjoy!