Question Recommended method of passing an arbitrary number of parameters to a constructor?

Cecil

19+ years progress programming and still learning.
OOABL Question:
OpenEdge 11.3.2 WebSpeed Workshop

As part of instantiating a constructor of a class object, I would like to pass an arbitrary number of parameters.

For example:
Code:
classObj = NEW ClassObject(INPUT "param1=xyx, param2=abc, param3=345, param4=true" ).

I was thing of using a syntax similar to a Javascript's object notation: Example "parm1":"value-x", "param2":123, "param3":true

But would like to see if anything else has be done before I start to cut code.
 

tamhas

ProgressTalk.com Sponsor
My first inclination is to say that wanting to do this probably means you are doing too much in the constructor. You should never do anything in the constructor that can possibly fail since then you are going to be left with an invalid object. But, if the nature of the problem is such that you would have the same issue with an Initialize method, then I would consider using a parameter object so that you have one fixed parameter to pass and then you can set what you want and query it at the destination. I have recently been doing some code which has a similar kind of need and what I have done is to provide the class with a number of public properties. So, I new the class with a minimum constructor, set those properties whose default values I wish to override, and then call Initialize to do the error checking and setup.
 

GregTomkins

Active Member
I think this is a great idea and I have been wishing for a while now that we used this pattern. I mean, the idea of a minimal constructor, followed by setting properties, followed by validating the result before using it.

... but for sake of argument, does this not violate your rule of 'never doing anything that can possibly fail'? Well, it might not fail, but you could easily be left with an invalid object, obviously.
 

tamhas

ProgressTalk.com Sponsor
It is possible there is some miscommunication here. With the approach I describe, nothing happens in the constructor which can result in the object not being created, i.e., the NEWing program will have a valid object which it can ask questions of and which it can get signals from. In my case, the classes I have been working with lately have a significant number of possible properties, some of which have reasonable default values and some don't. So, prior to calling Initialize, I set what I want and need for this particular run. Depending on context, that may involve setting some of those with reasonable possible defaults. The Initialize() step makes sure that it has valid file names and the like to work with, i.e., that no invalid values have been set. If there was a small number of fixed things that need to be set, I would probably make those parameters on the Initialize() call. Then, Process() lights the fuse!
 

Cecil

19+ years progress programming and still learning.
I should really explain a bit more on what I trying achieve. Currently I am the process of duplicating some python code and some on the methods & constructors it allow a shorthand technique of supplying optional parameters in form of a string. Now these parameters can be set via a set of public facing methods, but it also allows the developer to inject options as part of the method call rather than having separate lines for each option. Does that makes sense?

If the construct fails to initialize and dones not return a valid object due to a badly constructed string, it would be my fault as the coder for not developing a strict and robust string parser and allowing the method/constructor to fail.

Code:
objFormat = NEW Format().
objFormat:set_bold().
objFormat:set_underline().
objFormat:set_font_size(14).
objFormat:set_font_colour('red').
vs.
Code:
objFormat = NEW Format(' "bold":true;"underline":true;"fontsize":14;"fontcolour":"red" ').
Or where calling methods passing parameters can be nested, rather than multiple lines.
Code:
objWorksheet:write_integer("B2", 123.56,  objWorkBook:add_format(' "bold":true;"underline":true;"fontsize":14;"fontcolour":"red" ') ).

Don't get me wrong if you think I'm stamping on coding methodologies, am not. I seriouse taking on-board what you are saying, but I'm not sure if it's going to fit how I envisage my class object library is going to work. May be I missing something a better solution.
 

tamhas

ProgressTalk.com Sponsor
There are a bunch of ways to do this, including the delimited string approach. Myself, I like the parameter object because it provides a clean unambiguous interface and very clear and simple setting and reading, whereas it seems like the string parsing gets in the way of the code readability. A lot may depend on specifics here. E.g., for me, setting the appropriate properties as part of the setup worked well because it was very clean, clear, and simple and only needed to be done once for the whole execution of the class. But, from your code example, it looks like you might be wanting a different setting for every call of some set of methods which were called multiple times and there a simple way to compose the string for each call might be desirable, i.e., precomposed strings for each style. Given what the second code sample suggests you are doing, you might look at http://docxfactory.com/
 
Top