• Old School Batch Programming and Code Generation

    Recently I had the opportunity to do some old school Windows batch file programming at work. At first I was not too interested in using a language (is that even the correct word for it?) as old as DOS, but after I got into the project it was actually rather fun.

    First, a little back story about the project. The company I work for uses many mainframe systems today that it has used for decades. But, like most big companies, it has many modern server systems as well. To use these systems together we often have to move files containing large amounts of data between servers and mainframes. To accomplish this task, a piece of software called Connect:Direct is used.

    Because of the way we used this software, the setup and maintenance was really a pain. First, to setup a file to be moved to/from a mainframe and server, a Connect:Direct “process” has to be setup which consists of a file containing some propriety code to instruct Connect:Direct on what the process should do and how it should run. The file was just text containing some code with a CDP extension. In addition to the CDP file, we had two more simple files per process. One simple batch file so the users of the process can run it simply by double clicking on an icon and a simple text file to be used by the batch file which contains some instructions on which CDP file to use and some variables to use. What that meant was three files per process and a maintenance nightmare. Why a maintenance nightmare you ask? Because a server was recently done away with so every single of our 300 files had to have some details changed.

    Since my coworkers and I knew we would have to change many files we decided to streamline the process a bit. Initially we decided to just combine the batch files into one and have a simple menu so the users could choose the process they wanted from there. Since I was the one writing the batch file I got to work.

    After a bit of work though, I realized that more could be done than merely combining the batch files – we could actually do some code generation. That’s right, code generation with a batch program. Now obviously it couldn’t be too complex seeing how this would be done in a batch program, but a rudimentary form of code generation is possible.

    What I ended up doing was generating the text and CDP files on the fly, executing the Connect:Direct process with them, and then deleting the files afterwards. All the configuring is done in the batch file by setting few variables containing the changing data. What this resulted in was the elimination of 300 files and and extremely easy way to modify processes as well as an easy way to add new processes in the future.

    Before this project I had done a very limited amount of batch file work before. Perhaps what we did could have been done in some other “better” way, but I doubt in the time it took to get this up and running. Batch files might not be very sophisticated, but don’t underestimate there power. Under the right circumstances, they can really prove to be invaluable.

  • Getters and Setters in Scala

    In object oriented programming, getters and setters are something that most would agree have some real world benefits. Unfortunately, they can sometimes be annoying to write. They usually do not consist of a lot of code, but when you have the write the same thing over and over and over it gets old really fast. In my experience, most getters and setters are very much alike so it stands to reason that there must be a “better” way to accomplish the same result.

    In some languages there is support of some kind of “missing_method” syntax that allows the language to dynamically create getters and setters just by looking at the name of the method that doesn’t exist in the object. For example, in ColdFusion, there exists an “onMissingMethod” method that, when included, automatically executes when a method does not exist. This onMissingMethod gets passed the method name that was trying to be accessed and any arguments passed in. Then, by inspecting the method name (lets say, setAge) the method type and property can be determined. From “setAge” it can be determined that the method is a setter and that it should set the property “age”.

    Recently though I’ve done a lot of reading about and playing with Scala and I’ve found that it does things quite a bit different than ColdFusion, Java, and most other languages I’ve seen.

    As a first example, lets see an example of a basic class definition in Scala.

    class Person() {
     var name = ""
     var age = 0

    At first glance you might think that this class only allows you to instantiate a person object but not anything more. But actually, once instantiated, you can set and get the properties by doing the following:

    // Instantiate a person object
    person = new Person()
    // Print the object's age and name properties

    To set the properties to different values you would do the following:

    // Set the properties to different values
    person.age = 34
    person.name = "Dustin Martin"

    Pretty simple so far. You might be thinking to yourself the same thing I thought when I saw an example like this: these aren’t getters and setters! And you are right, that is not what this is. This is simply retrieving the public properties directly from the object. In most languages this would be a very bad idea. What happens when you need to validate the age is in a certain range before it is set? What happens if you want to format the name in a particular way before returning it? In Java or other languages you would be in trouble right about now and probably kicking yourself for not taking the time to write getters and setters. If you wanted to switch from directly accessing the public properties to getters and setters you would either have to find some strange work-around or implement the getters and setters and change any code that access those properties directly to use the getters and setters instead.

    In Scala though, it is no big deal to directly access public properties. You could easily change to using getters and setters by making a couple simple changes to your class.

    class Person() {
     // Private age variable, renamed to _age
     private var _age = 0
     var name = ""
     // Getter
     def age = _age
     // Setter
     def age_= (value:Int):Unit = _age = value

    What this code is doing is pretty simple. First, the variable “age” is renamed to “_age” and made private with the private keyword. Next, the getter is added by the line:

    def age = _age

    This code simply defines a method called “age” and returns the “_age” variable. Scala doesn’t require the return keyword but it would just have easily be written as:

    def age = return _age

    In addition to not requiring the return keyword, Scala doesn’t mandate the use of curly braces around a method body if the body consists of a single expression.

    Next, a setter had to be added to set the new private variable.

    def age_= (value:Int):Unit = _age = value

    This line is a bit more tricky but I’ll explain. First, the method name is “age_=“. The underscore is a special character in Scala and in this case, allows for a space in the method name which essentially makes the name “age =”. The parentheses and contents dictate the value and type that needs to be passed in. The “:Unit” code is equivalent to returning void. The remaining code is setting the “_age” variable to “value”. These things allow the method to be used in the same way as directly accessing the public property.

    person.age = 99

    But wait! Where are the parentheses in this supposed method call?! In Scala, parentheses are usually optional. The previous line could just as easily been written as

    person.age =(99)
    // OR

    As you can see, Scala has some unconventional ideas that provide a solid, well thought out, and an abbreviated syntax to access object properties.

  • Learning a language after CFML

    I have been using CFML professionally for three or so years now and I can honestly say I’m pretty comfortable and probably pretty skilled when using it. I’m not saying that I have nothing else to learn, but just that I at least know what I’m doing. Other languages I’ve used with include Java, Ruby, C, and C#.

    But of the above languages, CFML is really the only one that I know extremely well. Java, C, and C# I “learned” in college and Ruby (or maybe more just Rails) is something I taught myself for my final capstone project in school. CFML is the only one I’ve used professionally. So suffice to say I’m no software development guru. Because of this I’ve decided that I wanted to take a stab at actually learning a new language. I want to learn a language in the effort to broaden my knowledge, make myself a better programmer, and become a more marketable programmer.

    CFML is something of an odd language (keep in my that it is a language that I love). Its syntax is a bit odd in that it is tag based, has some odd datatypes (is a list a string or a datatype?), and is really just different from any other language that I’ve seen. Because of that it seems prudent for me to learn a new language. Learning any additional programming languages is good, but I want to learn something that will really stretch me and force me to think differently that I do with CFML.

    There are a few options that I thought of.

    1. Java - While it is very different and very popular, Java’s syntax is just too verbose for me. Plus, I’ve already used some Java while I was in college so I feel I have at least of little understanding of it. I’m really looking for something knew though.
    2. Ruby - While it is different from CFML, there are really a lot of similarities in my mind since it is also a dynamically typed language. In my opinion, learning the syntax would be the hardest part…something I REALLY struggled with while experimenting with it in college.
    3. Groovy - Groovy seems really intriguing. I like the syntax and that it is dynamically typed. It seems to have a lot of cool features and seems to fit perfectly in the web realm. But, I still want something to push me a little harder I think. This might be a language to learn though.
    4. Scala - A very interesting language. An object oriented functional language. It reminds me somewhat of Javascript in its functional aspects. It is statically typed but using something called “type inferencing” allows you to leave off a lot of the type annotations that you’d have to include in Java. It almost seems like a hybrid dynamically typed / statically type language.

    Overall, Scala seems like a language that would teach me a lot. I’ve not really done much with statically typed languages and it definitely would be difficult to get used to. And it seems to have so many more types than CFML. Array, List, Int, String, Map, Set, HashMap, etc. Then there is the whole subject a mutability. Some types are mutable, some are immutable. There is really a lot different with it.

    Regardless though of how well I learn it, I think it will be a great experience and can only make me a better programmer. I think the most difficult part in this will be figuring out how to go about learning the new language. Since the language would have to be learned in my spare time outside of work, the trick will be coming up with fun/interesting projects to use the language in.

    Any ideas? What are some ways you go about learning new languages?

  • Types of Domain Specific Languages

    Domain Specific Languages are something that first appeared on my radar some time ago – I believe via Peter Bell’s excellent blog. I read a good amount on the subject at other websites as well including Martin Fowler’s DSL site. While the ideas were very interesting, they always seemed a bit “over my head”. But the idea of DSLs recently cropped back up when I decided to start tinkering with building my own application generator in ColdFusion.

    The nice thing about using a DSL with a application generator is that you can program all your app details in this DSL and then run a generator to expand your DSL code into a full application. And then, if you need to change the application later, you can simply change your DSL code and regenerate your application. It is basically a configuration file that defines part or all of your application.

    One difficult thing about getting started with making my own DSL though was there are very few good concrete examples of DSL use within ColdFusion. However, I identified three main categories of DSLs I was interested in – XML based, ColdFusion syntax based, and my own made up language not based on any other. Martin Fowler describes these concepts and internal DSLs and external DSLs. Internal are those that rely on a parent language (like ColdFusion, Java, Ruby, etc.) and its syntax. They usually just use a very small subset of the parent language. An external DSL is one which is one which is completely independent of any other language’s syntax. It can be completely “made up” by its creator and isn’t restricted to any of the rules of a parent language. I would highly recommend spending some time reading Fowlers website if you are interesting in learning more about this.

    The problem, as with many things, is getting started. I know I was a bit perplexed as to where to start. But, to help others that might be wondering about this topic and to get some feedback myself I thought it would be good to post a couple DSL examples that I’ve been playing with as I work on a ColdFusion code generator. All the examples present the same thing, but do so with different syntax.

    XML Based

    This first example is XML based. On one hand I do like that it would be fairly easy to parse and is well understood by many people. On the other hand it is very verbose. This example is based on an example that Peter Bell gave in one of his presentations.

    <entity name="user">
        <property title="UserID" name="userID" column="user_id" dataType="integer" primaryKey="true" />
        <property title="UserName" name="username" column="username" dataType="string" maxLength="15" minLength="4" required="true" />
        <property title="Password" name="password" column="password" dataType="string" maxLength="50" minLength="8" required="true" />

    CFML Based

    Here is a version based on ColdFusion and it is accomplished using method chaining. One nice thing about this method is that there is no need to parse the code because ColdFusion takes care of that for you. It is simply methods chained together which ColdFusion executes and throws and error is there are any syntax problems. Like XML though, it can be very verbose.


    External DSL

    This final version is not based on any other languages syntax. This allows for tremendous freedom to have full creative freedom to design the language however you like. However, you must be able to parse the language which means you’ll need to build a parser which can be quite difficult depending on the complexity of your language.

    Entity user
      property name="userID" title="UserID" column="user_id" dataType="integer" primaryKey="true";
      property name="username" title="Username" column="username" dataType="string" maxLength="15" minLength="4" required="true";
      property name="password" title="Password" column="password" dataType="string" maxLength="50" minLength="4" required="true";

    So what are your thoughts on these three DSLs? What are you using and why?