Question
· Jan 11, 2018

ClassMethods with a private keyword tags

My questions are regarding ClassMethods with a private keyword tags.

When we use a wizard to add a new method, we are given a options to
select private checkbox to make it a private method as well as 
Class Method checkbox in the same time. 

1. What would be the reason and the case to select and use them both?   
2. Secondly, if there is such a case in already developed application is it
safe to remove the private tag from ClassMethod? Obviously if it's not a 
ClassMethod private tag cannot be removed as easily.

Thank you,

Discussion (11)2
Log in or sign up to continue

The two concepts are orthogonal. A class method doesn't need an instance; a private method is only visible from within the class. As a contrived example, consider a public hello() class method that calls a private greet() class method.

Is it safe to change a class method from private to public? In the immediate term, sure, but now it's a public part of the class's interface with all the maintenance responsibilities that entails. I don't see how that's obviously any different than an instance method.

They're not orthogonal.

Consider a Person class:

Person
    Method AmISmarterThenTheRest() {
        Set AvgIQ=..GetIQ(..HairColor)
    }
    
    ClassMethod GetIQ(HairColor) [Private] {
      ; determine the average IQ by HairColor
    }

The GetIQ method is obviously a class method, but you might not want to expose it to the outside world.

There's where one needs (wants) a private class method

Private is not important in that case - it only affects from where method could be called. Private methods can be invoked only by methods of this class or its subclasses.

 

The difference between class methods and instance methods is that instance methods can access object context. For example you can access current object property from instance methods. Use instance methods only when you need access to object context. In all other cases use class methods.

Here's a sample code, highlighting the difference:

 

Class Test.Person Extends %RegisteredObject {

Property Name As %String;

Method PrintName() {
  Write ..Name
}

ClassMethod PrintText(text) {
  Write text
}

}

Thank you everybody for your answers. My question here would be, why use class method instead of regular method in this case? 

If you don't want it to have exposure to outside world, simply make it a regular method within your class, which is going to be private (I believe) by default and use as many as you need privately, while still have an outside entry point if needed with non private class method.

I guess what would be the benefit to use class method vs regular method here?  

I see myself small benefit to make something originally class method [private] vs regular method - in case in some point in the future of your application, you need to change the ability to get to the method from the outside and remove the private tag.   

Alex, if you code it as an instance method (private or public) then you won't be able to call it from a classmethod (private or public) unless the caller has (or creates) an instance of its class. In other words, the decision about whether to code an instance method or a classmethod is surely independent of the decision about whether to mark that method private or public, no?

1) The reasons for using both together are purely based on your application's need and design. Some design patterns specify desired visibility of methods for example.

2) Safe depends on the context but it's usually easier to move from a restricted mode to a non-restricted one. Moving methods from private to public shouldn't have any impact on your application (functionality wise). However, the opposite is not true and will require you to modify your code to fix the parts that were accessing previously public methods that are private now. This will also impact clients of your code (API) as they'll face the same challenges with the new restrictions.