Results 1 to 5 of 5

Thread: Code Generation and you, how to win friends and influence people...

  1. #1

    Code Generation and you, how to win friends and influence people...

    This is a tutorial that provides programmers with a tasty morsel of information straight from the mind of BD. It explains how repititious code can be easily created, with much lower cost of developer resources.

    Yes, I'm referring to programs that write programs...

    As I sit here this evening a few things have crossed my mind. One of which is a programming problem that is all to frequent and easily fixed. When writing applications we frequently have to write repititious code. For example you are working on an application that takes advantage of beans to store data during a session of the application. A lot of small similar programs are required to fully complete the task at hand for example you may have the following:

    Code:
    public class Foo {
        protected int foo;
        protected String bar;
        protected double quux;
    
        public foo() {
             //do some stuff
        }
        
         public void setFoo(int foo) {
             this.foo = foo;
         }
     
         public int getFoo() {
             return foo;
         }
    
         ...
    and each of the classes (or objects) you have to create is very similar to this. There is an easier way to do this than writing each one of the classes.
    Create a code generator.
    This makes your life much easier because instead of writing several (extremely) repitiitous programs, you can just write one. It should look something like the following:

    Code:
    public class BeanMaker {
    
          protected String filePath = "/path/to/some/directory/";
          protected String fileExtension = ".java";
          protected String fileName = "";
          protected PrintWriter out;
          
          public BeanMaker(String fileName) {
              this.fileName = fileName;
              try {
                  out = new PrintWriter(new FileWriter(new File(filePath+fileName+fileExtension)));
                  out.println("package  foo.bar;");
                  out.println("import some.packages.*;"); 
                  out.println("/**this object generated on "+new Date()+" by BigDick*/");
                  out.println("public class "+fileName+" {");
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
    
          public  void generateAccessor(String fieldType, String fieldName) {
              try {
                  out.println("\tpublic void get"+fieldName+"("+fieldType+" value) {");
                  out.println("\t\t this."+fieldName+" = value");
                  out.println("\t}");
             } catch (IOException e) {
                 e.printStackTrace();
             }
          }
    
          public void generateMutator(String fieldName, String fieldType) {
              try {
                  out.println("\tpublic "+fieldType+" get"+fieldName+"() {");
                  out.println("\t\treturn this."+fieldName+";");
                  out.println("\t}");
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
     
          public void closeObject() {
              try {
                  out.println("}");
                  out.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
    }
    then all you have to do is create a main that gets the necessary information (can be from a flat file or a database, db can be much easier because of the metadata held by the system) and calls each of the methods/functions for each object that needs to be created. Then you can go in and hand code any additional methods that are necessary (or create in this program one that does so for you) but include involved logic.

    I said in the topic that this is how to win friends and influence people, I don't know about the friends part, but it certainly can influence a co-worker/boss/professor when your productivity has sky rocketted. As I come up with more advanced methodologies of implementing an idea such as this, I'll keep the tutorial updated and I welcome any questions, comments, etc.

    Also of note: the example here is Java, but this can be done with whatever language you need it for or in.

    Okay, fixed the errors in the code sample.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

  2. #2
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    I never dived really deep into java and its (endless?) possibilities,
    such as Java generics[1,2], but allow me to take c++ as an example
    of other languages.

    Is it possible that what you try to perform here, can be achieved by
    using <templates>? Or even simpler - I can define precompiler
    macros which generates the code as I want it? This still allows
    me to extend specific code if one of the objects is slightly different than
    the others.

    Do you know Java generics and could you comment on them? Did I
    misunderstand what you presented? If so, I am sorry - it was a quite a day

    Cheers

    [1] http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
    [2] http://c2.com/cgi/wiki?TemplatesSmell (<-- )

    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  3. #3
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    you forgot a semi-colon on your import line .

  4. #4
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    I'm not really a java programmer and never really delved into OO. But I always thought you should create some base class and use inheritence (subclasses) to get the 'common' parts. Perhaps overloading methods you need changed. At least that's what I understood on the "code reusability" subject of OO.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  5. #5
    You can use inheritance, but this is for a bunch of objects that while similar do not necissarily inherit from anything. Beans if you will. Which all have fields that need to be accessed and manipulated, you can include inheritance and things such as that, if the need should arise, but if you don't require inheritance, that being that none of the objects have a commonality of fields, then why use it. For example, I used this to create a bunch of beans built off of a database that has a low level of normalization (hardly any at all) and db conventions where I work prevent the ability to use these beans with inheritance, but that being said, we generated objects like this using inheritance at the last place I worked. I should have stated that this should be used when little if any inheritance is possible. From what I know of generics, they could be used but you would still have to code the lines to create them, because they seem to be used to create
    improved readability and robustness
    which is to say they help to remove casts and things like that, which also are a repititious part of coding. They do not, however, fit into what I envision this to be. Partly because they seem to serve as wrappers for other objects (such as the one used in the pdf [link above] where they use it as a wrapper to prevent casting from an object to an Integer automatically). But especially since their introduction was in Java 1.5, which in industrial programming, will likely be infrequently used for some time (this prevents essential pieces from being depracated or having compiler errors) until integration and migration from 1.4.X to 1.5.

    On the other hand, if you use a macro language (C++, Common LiSP, et al.) by all means use those to create your code, I've never used C++ macros, but I know that the ones from LiSP allow you to create extremely powerful code extremely easily, such as defun, which creates the code that is compiled into a function. Java on the other hand does not have macros...that I know of or care to.(see note at bottom of post)

    Note: I think that java should NOT be used for anything outside of academia, especially since James Gosling (CTO for Sun Microsystems) said that it[java] was designed for the average programmer, and I want to be much more than that. Which is why if I have my choice, I use LiSP or PERL or something similar and don't have to use stuff like this...

    I also forgot to wrap the print statements in try/catch blocks.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •