views:

306

answers:

5

I don't have good name for this style of programming where the syntax is more succinct because of not having to pass the context into a function or call the functions off of a context object.

For example, some random OpenGL C code:

 glBegin(GL_QUADS);
 glNormal3fv(&n[i][0]);
 glVertex3fv(&v[faces[i][0]][0]);
 glVertex3fv(&v[faces[i][1]][0]);
 glVertex3fv(&v[faces[i][2]][0]);
 glVertex3fv(&v[faces[i][3]][0]);
 glEnd();

But you could set the context in the "begin" call and release it in the "end" call. I have seen styles like this in C#, Java, and Ruby. Does it have a name?

+1  A: 

Reference oriented programming?

George Jempty
Is that a real name? I kind of like it. maybe Implied Reference or Implied Context. +1
tyndall
I just made it up
George Jempty
+1  A: 

If you assume there is a "this" in front of the statements you could consider it a Fluent interface: http://en.wikipedia.org/wiki/Fluent%5Finterface

Otherwise, it appears very much like a Stack-Oriented language such as PostScript:

http://en.wikipedia.org/wiki/Stack-oriented%5Fprogramming%5Flanguage

Nissan Fan
Not really, a fluent interface is about being able to directly continue the operation by invoking a method of the result of the previous method. This snippet of code does do something entirely different.
Dykam
Yes, when you assume that there is a "this" in front of these calls.
Nissan Fan
@Nissan Then it just looks like bad Java :-)
pst
Good answer and references. +1 I would agree that it is sort of like a twist on the Fluent idea. And I should have thought about the Stack. Good 1.
tyndall
+1  A: 

It seems very similar to a Builder

Mik Lernout
+5  A: 

"Procedural with global-state side-effects"?

(While OGL does use a stack to maintain various state, it is not used in this example and thus omitted from my reply.)

pst
I like where you are going on this +1. Is this some of the popMatrix / pushMatrix stuff is doing in OpenGL? What do you think about my Ruby comment at the question level? It uses a closure.
tyndall
+1  A: 

This looks sorta like a builder. What you have there is openGL calls and you are basically constructing a triangle (that is rendered). Your example rewritten in oo/builder terms:

TriangleBuilder b = new TriangleBuilder();
b.AddVertex(normal, faces[0]);
b.AddVertex(normal, faces[1]);
b.AddVertex(normal, faces[2]);
Triangle t = b.Build();
yngvedh
+1. I see what you are talking about... it's like a Builder but is hiding the "b" (or current context). This pattern could be used for things other than Building things... so I'm not sure Builder would always convey the right idea.
tyndall
@ B. Tyndall: I agree, that's why I wrote "This looks sorta like ..." instead of "This is ..."
yngvedh