Method in my madness?


More like madness in my methods I suspect …

Let’s take a look at the code that acts on our object data in the Person class as it now stands:

method-id "new" public.
local-storage section.
procedure division using by value   personId      as binary-long
                                    lastName      as string
                                    firstName     as string
                                    middleNames   as string
                                    personTitle   as string
                                    birthDate     as type DateTime
                                    personAddress as type 
                                                     School.Data.Address
                                    ifMale        as condition-value.
                                    
    Set Person-Id       to personId.    
    Set Last-Name       to lastName.    
    Set First-Name      to firstName.   
    Set Middle-Names    to middleNames. 
    Set Person-Title    to personTitle. 
    Set Birth-Date      to birthDate.   
    Set Person-Address  to personAddress. 
    Set Person-Male     to ifMale.

    goback.
    
end method.

method-id ToString override.

procedure division returning returnString as string.

Set returnString to String::Concat(Last-Name::ToUpper() ", " 
                                   First-Name " " Middle-Names)

end method.    

We’ll ignore the property methods we discussed recently.

First off, for non-static classes (we’ll get to those soon) every class must have a new method to create new objects or new object instances. This method is and can only be executed once on object creation and is used commonly to initialise the object data and do setup that allows the object to be interacted with. You may have multiple methods called “new” that have different signatures, that is they must be called with different parameters. Thus we might have a new method that infers title from sex and age like:

method-id "new" public.
local-storage section.
procedure division using by value   personId      as binary-long
                                    lastName      as string
                                    firstName     as string
                                    middleNames   as string
                                    birthDate     as type DateTime
                                    personAddress as type 
                                                     School.Data.Address
                                    ifMale        as condition-value.
                                    
    Set Person-Id       to personId.    
    Set Last-Name       to lastName.    
    Set First-Name      to firstName.   
    Set Middle-Names    to middleNames. 
    Set Birth-Date      to birthDate.   
    Set Person-Address  to personAddress. 
    Set Person-Male     to ifMale.
    
    *> If adding 14 years to the person’s birth date is later 
    *> than todays date then the person is under 14 years old
    
    If  birthDate::AddYears(14) > type System.DateTime::Today
        If  Person-Male
            Set Person-Title to "Master"
        Else
            Set Person-Title to "Miss"               
        End-If
    Else
        If  Person-Male
            Set Person-Title to "Mr."
        Else
            Set Person-Title to "Ms."               
        End-If
    End-If.
    
    goback.
    
end method.

Now we can create a valid new instance of Person without supplying the person’s title. I could of course have made the Title set property visible as well but I’ve been fighting hard to make this an immutable class (even though from a design point of view it doesn’t really make sense) so I’m going to stick with it for the time being.

I’m going to go on with one of my bugbears here:

Always override the ToString method on your non-static classes!!!

 

You will notice I have an override in my both my Person and Address classes. If I comment out the ToString override in the Person class and fire up the debugger, let’s see what an instance of the Person class looks like:

ToString1

 

Huh??? “School.Data.Person??? Not very meaningful is it? Try debugging a complex class comprising over 10 other object instances and it becomes increasingly difficult to quickly identify what the hell is going on. You might also notice that my careful naming of my variable to aPerson has been converted to APERSON … COBOL by default is a case-insensitive language. That’s why I litter my public interface with  as “etc” everywhere. Mixed casing is just easier to understand. Full-stop. End-of-argument.

Look what the variable looks like when I un-comment out the code (lovely grammar that, isn’t it!):

ToString2

That’s just from hovering over the variable. If I do a Quickwatch, I get:

ToString3

That’s just a lot easier to quickly take in in a complicated debugging session, especially where you might have an array or collection of many Person instances. An added benefit from all of this is that when you used the good old COBOL DISPLAY verb on an object instance, the object’s ToString method is invoked by default:

ToString4

Look’s a let better than:

ToString5

 

Doesn’t it? The code to display the instances in both cases was just:

Display anAddress.
Display aPerson " and " anotherPerson.

Simple, isn’t it? We’ll get onto some of the other useful things you can do with the ToString method.

Another little aside on this commenting and uncommenting out code … Visual Studio 2010 has two really handy buttons for performing this with a couple of clicks. Select the code you want to comment or uncomment and just click the button:

CommentUncomment

Advertisements

2 responses to this post.

  1. Love the post. This is turning into an amazing blog!

    I am pretty sure you don’t need the quotes around the new as in

    method-id new.

    The parser automatically goes to quoteless parsing after type, method-id, class-id and several other key words.

    However – it is just a matter a style 🙂 I would not have put full stops at the end of the set statements either, but then I learned FORTRAN first – so what would I know….

    Again – this is just amazing stuff!

    Thanks – AJ

    Reply

  2. Thanks Alex – much appreciated! And yes, you’re right and I should get rid of them. I think you can even capitalise your New as well, which given how much (style-wise) I like mixing my cases, even when I don’t need to (it just looks pretty, OK!). Ah yes, the full-stops … I started coding COBOL in the OS/VS days when that was the only way you could end imperative statements. The guys who used to walk through my code in those days would just send it back if you didn’t have the full-stops. Old habits die hard!

    Michael

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: