Tag Info

Hot answers tagged

58

I prefer going a bit further, and, instead of writing code that writes code, writing code that generates objects, methods, functions. This can be achieved with Lisp macros or Ruby dynamic program modification capabilities for example. The little difference is that you don't end with source files that were automatically generated. Usually these files are not ...


37

In the Lisp world, it is quite common to see the code which writes code which writes code (and so on). So, any decently sized Lisp or Scheme project will serve as a good code example. I'd recommend looking at the Racket compiler and runtime sources, as well as Bigloo, their libraries are just brilliant. As for the productivity: I'm using metaprogramming as ...


18

Even better, use code that someone else wrote that writes your code for you. Code automation is generally good for ORMs and other database interaction code, and of course for repetitive but similar code building. Of course, if you're building out lots of similar looking classes, perhaps you could've accomplished the same thing in a dynamic language much ...


14

One of the classic examples is lex and yacc. Their primary purpose is to avoid the drudgery of writing any kind of parser. Along the way, they make it far faster to build complex parsers with many rules and states, and they also avoid all of surprise mistakes made by people rolling their own. This is also the idea behind c, which is a tool to write ...


11

Metaprogramming Metaprogramming is a controversial technique in many shops. The reason is, like any powerful tool, the magnitude of help or hurt is large. Pros More Expressive, less code to write and maintain(often by an order of magnitude or more) Consistency, more consistent behavior over the class of problems your solving with the code Productivity, ...


8

Most code writes code. For example php code helps write html. The php pdo library helps write SQL calls. The file I/O functions write code to communicate with the OS. Even a regular function call is a reference to another block of code which is executed. So your functions calls are writing code. In broad terms, we can think of computing as writing ...


6

You cannot practically build a parser for a real computer language (or DSL) by just showing it examples. Learning anything by just positive examples is pretty hard (as is well known by the machine learning community); you need at least some negative examples. Pretty much you want parsers to be able to accept alternatives. If you allow them, a simple ...


5

This depends on the complexity of the domain. If it mainly consists of structured data, a visual editor may be more intuitive to use, but if the domain contains significant amounts of logic, experience has shown time and time again that visual editors are a poor tool for that. Additionally, there are some very common, very useful tasks that are trivial with ...


5

Yes, that seems reasonable. If the intended audience does not understand the terms of your language or misinterprets them, then the usability for the enduser takes precedence. A program or content that a typical user does not understand is of little value. More so, if you only want to present them a simplified tip of an iceberg because they are not and ...


4

At our company we use some tools who actually generate C++ or C# classes with data downloaded from the internet. These classes are data containers and contain a large number of objects in lists.


4

How you do this varies depending on your requirments. Assuming you're using static code generation you could write all the infrastructure yourself, or you could use an existing generator such as CodeSmith or MyGeneration. Using these you just need to write the required templates. My last project involving this was some basic ASP.NET CRUD screens (code ...


4

There are two canonical examples of domain specific languages: Regular expressions. They are simple language, but language nevertheless and they are compiled to the actual search automaton, so definitely benefit from compiler handling them. Other parsing languages like BNF are similar case. SQL. While this is normally compiled in the database server and so ...


4

Metaprogramming has been part of programming for a long time. Consider not just tools like SWIG, or WYSIWYG designers, which create code, but also in-language tools like C's preprocessor, or even C++'s templates and C#/Java's generics- not to mention Reflection. In fact, you could argue that every compiler is just another metaprogram- they take in program ...


3

This is fine, but I don't want to have to code something like this each time a new rule is needed. I'd like to create something dynamic, ideally something that can be stored in a database. Well, judging from your requirements, you will have to code something. A new logic rule means a new implementation. How can you sanely solve that without ...


3

Here's a concrete example from my past. I was working at a site that had around 50MB of Delphi source code using the BDE for data access. They wanted to switch to using Direct Oracle Access to allow an Oracle upgrade past the highest version supported by the BDE (8i if I recall correctly). So, instead of getting a team of coders to work through every form ...


2

I am just working on such a tool. In our particular case we generate the VB.NET code based for the Data Layer on the signatures of the functions in the database. Starting to work on and with code generation is difficult at first since you have no idea how the code should be generated, but once you have an established set of rules, and the code that has to ...


2

Take a look to CL (Common Lips) Macros. In my opinion that is exactly what you want. Lips is perfect in metaprogramming. Also I suggest Nemerle if you want to have .NET powers with perfect Metaprogramming support (including macros) But if you want a true code-generation engine take a look on Apache thrift


2

I have developed neat meta-programming solutions with Prolog. Where the main application (in C++ say) translates an abstract definition of a problem into a Prolog application at runtime, which is then delegated to. Often writing equivalent functionality in C++ would take forever. I think this scenario is an excellent case in favour of the code-writing-code ...


2

I have a PHP module which outputs a web page containing JavaScript code which generates HTML. That's three layers right there. Boy was that hard to read! In a programming class, we had to write a program that would take a formula string from the user and parse it and display the value. The most impressive solver simply took the user input, wrapped it in ...


2

What do you think about the subject? Metaprogramming is most commonly associated with non-dynamic languages, since there's an harder time in achieving certain behaviours (such as implementing an ORM) without lots of non-productive and non-intelligent lines of code. But even in more dynamic languages such as PHP, code generation can be a really life-saver ...


2

I can't speak specifically of Converge, but I regularly use the D Programming Language which has very strong meta-programming features. Here's an example of D library for units of measurement. The programmer can write: auto dist1 = Meters(5.2); auto velocity = dist1 / Seconds(2.1); auto kmPerSecond = cast(Kilometers)velocity; By using the type-system to ...


2

You ask for examples.... When working with SQL, you shouldn't be changing the database directly, but instead are supposed to be executing scripts that make whatever changes you want, including structural changes to the database (adding tables, columns, primary keys, constraints and so forth). Quite frequently you will need to do take the same action ...


2

I will be the contrarian and point out that DSLs are often recommended inappropriately, because they are a more natural and flexible interface for programmers, and programmers like to create systems that are easy for themselves to use. That means DSLs are best suited for features designed for programmers or highly trained and specialized users like IT ...


2

A form restricts the possible expressions to just one type. In natural language terms, you can write anything you can imagine in English. What you can express with a form is much more limited: think Mad Libs for example.


1

Since the DSL is text, I can use text-based tools. Most notably I can generate code for the DSL with a shell script via metaprogramming. As others have said, the DSL doesn't prevent having an RIA as well.


1

While many answers here refer to what is commonly known as meta programming, there was in fact a field associated to AI known as automatic programming that was about programs understanding or synthesizing programs [1]. Any compiler (or meta-program, code generator, translator, macro system, ...) work with transformations, generating an output from an input ...


1

A metaprogramming example I have a Ruby authorization library called Authority. It lets developers ask questions in their app with methods like current_user.can_read?(@post) and @post.readable_by?(current_user). These questions are answered by centralized authorizer classes. This is the crucial part: Authority doesn't know which methods to define until it ...


1

In or around 2001 I started working on a project which was making extensive use of business objects and data objects. I was to be building the front-end website, but was hung up twiddling my thumbs because the business layer and data access layer weren't fully developed. After a couple of weeks of that, I started to take a hard look at what those layers ...


1

See Philip Greenspun's problem set 4 from MIT course 6.916: Software Engineering of Innovative Web Services (http://philip.greenspun.com/teaching/psets/ps4/ps4.adp). Its objective says, "Teach students the virtues of metadata. More specifically, they learn how to formally represent the requirements of a Web service and then build a computer program to ...


1

OP asks for resources. You might found our DMS Software Reengineering Toolkit interesting. It is a pure metaprogramming tool, intended to let one build custom program analysis and transformation tools. [To follow a comment to OP's question, when used to build a specific transformation tool, DMS is a product line that writes code, that writes code :] DMS ...



Only top voted, non community-wiki answers of a minimum length are eligible