If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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:

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:

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.

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 &lt;templates&gt;? Or even simpler - I can define precompilermacros 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

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.

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...