FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   sexy syntax
« Previous topic | Next topic »

Pages: 1 2 
   Author  Topic: sexy syntax  (Read 1101 times)
dek


sexy syntax
« on: Jun 11th, 2004, 7:52pm »

just what is sexy syntax? well, besides being an extremely catchy title for a topic, it's what i like to call a semiphilosophical approach at code writing.
 
sexy is everything along the line of neat, slick, fast, clever, compact, smooth, etc. basically meaning well written code. of course everybody has a different opinion on well written code, so let me try to explain:
 
 
ugly:   a = a + 1
 
sexy:   a++
 
another example (doesn't matter if it makes sense or not):
 
ugly:
 
int[] name = new int[10];
name[0] = 2;
name[1] = 2;
name[2] = 2;
name[3] = 2;
name[4] = 2;
name[5] = 2;
name[6] = 2;
name[7] = 2;
name[8] = 2;
name[9] = 2;
 
 
sexy:
 
int[] name = new int[10];
for(int i=0; i<10; i++) {
  name[i] = 2;
}
 
 
i hope it's clearer now what i mean with sexy syntax.
why am i bringing up the whole subject? well, i've come across various posts discussing the way code should be written. some say there should be a standard that everybody should follow, others say everyone should follow their own style and that standards block creativity. basically i'm for the latter, but sometimes i wish to have known a more sofisticated way to write my code, which could have saved me several unnecessary lines, or could have sped up the whole script.
 
being able to write sexy syntax is a matter of learning and experience, picking out clever lines of code from all the open sources, or just having a talent for finding the best way (if there actually is a best way).
 
i've put together a couple of questions, for everyone who's interested on this topic, or anyone who wants to take his mind of  wathever's bothering him at the moment:
 
does anyone have more examples of sexy syntax?
should/can there be guiding-lines for newbies (like me) when learning any new programming language?
should there be a forum group on the topic, where people can drop their sexy syntax, newbies can learn, veterans can discuss?
should there be a discussion on this topic at all?
 
bonus question 1:
should processing, alongside beautify, have a sexify option?
(i'd like to see that, just for the sake of it)
 
bonus question 2:
are you tired of the word sexy yet?
 
remember that the whole post is enclosed in <></> tags, meaning it's not to be taken too serious. but still i'm interestad to know what all you fellow processors think about sexy syntax.
there can only come good of it.
 
pace,
dek
« Last Edit: Jun 12th, 2004, 10:56am by dek »  
Quasimondo

WWW Email
Re: sexy syntax
« Reply #1 on: Jun 11th, 2004, 10:23pm »

Well, for a start let me sex up your second example a bit:
 
int[] name = new int[10];  
for(int i=10;--i>=0;name[i] = 2){}
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
dek


Re: sexy syntax
« Reply #2 on: Jun 11th, 2004, 10:46pm »

now that's what i'm talking about!
 
that would be the 'flaming hot syntax'.
 
justo


Re: sexy syntax
« Reply #3 on: Jun 12th, 2004, 12:14am »

yes, but then we might as well all use C and *dst++ = *src++ ourselves to hell.
 
1. beyond a few simple examples, though, a sexify option would be very difficult to write, on the order of an optimizing compiler...which is still an active and ongoing branch of research. and, it should be noted, even though individual clock cycles arent as important as they once were, your original, "ugly," version of the name array intialization is actually faster than even Quasi's optimized-assembly-like approach.
 
besides, sexy is in the eye of the beholder.
 
2. i am not currently tired of the word sexy, but we'll see depending on how long this thread gets
 
sinhro


Re: sexy syntax
« Reply #4 on: Jun 12th, 2004, 3:25am »

why quasimodo goes 'backwards' through the array? is it faster or just sexy?
 
fry


WWW
Re: sexy syntax
« Reply #5 on: Jun 12th, 2004, 4:11am »

int name[] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 };
no loops, no variables.  
 
ah how i miss *dst++ = *src++
but not for a teaching/sketching language. oy.
 
kevinP

Email
Re: sexy syntax
« Reply #6 on: Jun 12th, 2004, 10:36am »

on Jun 12th, 2004, 4:11am, fry wrote:
ah how i miss *dst++ = *src++
but not for a teaching/sketching language. oy.

 
"Evaluate src, then increment it, then assign new value to dst, then increment dst" What do the asterisks do I see something like this often in C such as in this signature:
 
Code:
int InsidePolygon(Point *polygon,int N,Point p)

 
 
 

Kevin Pfeiffer
amoeba

WWW
Re: sexy syntax
« Reply #7 on: Jun 12th, 2004, 8:14pm »

on Jun 12th, 2004, 10:36am, kevinP wrote:
What do the asterisks do I see something like this often in C such as in this signature:
 
Code:
int InsidePolygon(Point *polygon,int N,Point p)

The asterisks mean that it's a pointer to an object of that type. So your example means "function InsidePolygon, which returns an int and takes three parameters: polygon which is a Point object and is passed by reference, N which is an int and passed by value and p which is a Point object passed by value. So if InsidePolygon modifies the variable "polygon" that object will change elsewhere as well, whereas "p" is a local variable which is lost outside the scope of InsidePolygon. Of course, "polygon" could also be an array of Points, since it's just a pointer.
 
Not having to worry about pointers is Java's greatest gift, altho it makes it a lot less "sexy". Leave the obfuscation to the C hackers...
 

marius watz // amoeba
http://processing.unlekker.net/
Quasimondo

WWW Email
Re: sexy syntax
« Reply #8 on: Jun 12th, 2004, 8:19pm »

on Jun 12th, 2004, 3:25am, sinhro wrote:
why quasimodo goes 'backwards' through the array is it faster or just sexy

 
In this special case it's just a bit "tighter" aka sexier, but in real life the execution can be slightly faster (at least that's what I read) because you spare one comparison with a variable per iteration:
 
Classic:
int elementCount=10000; //or whatever
for (int i=0;i<elementCount;i++){
... blah ...
}
 
Sexy:
int elementCount=10000; //or whatever
for (int i=elementCount;--i>=0; ){
... blah ...
}
 
But I think that the processing compiler doesn't like the empty ; ) part even though it's perfectly legal.  
« Last Edit: Jun 12th, 2004, 8:20pm by Quasimondo »  

Mario Klingemann | Quasimondo | Incubator | côdeazur
REAS


WWW
Re: sexy syntax
« Reply #9 on: Jun 15th, 2004, 9:13am »

just a clarification based on quasimondo's last comment. there is no "processing compiler", but a parser which converts the syntax into java which is then compiled using jikes, the ibm java compiler. our parser, which uses antlr, can be finicky, yes.
 
Koenie

170825270170825270koeniedesign WWW Email
Re: sexy syntax
« Reply #10 on: Jun 16th, 2004, 8:34pm »

I think I like to code 'sexy', because it makes code shorter. Shorter code means less scrolling means more order (I think). However, if you make the code so 'sexy' it's not comprehensible anymore, it loses it's sexyness. I think most of my 'growth' code is pretty sexy: http://koeniedesign.com/software/growth .
 
Koenie
 

http://koeniedesign.com
Quasimondo

WWW Email
Re: sexy syntax
« Reply #11 on: Jun 17th, 2004, 1:56am »

PARENTAL ADVISORY: EXPLICIT LYRICS BELOW - THIS IS NOT INTENDED FOR MINORS!
 
 
Hi Koenie,
 
if we are talkin' sexy here I hope you don't mind if I undress your code a bit.  
 
Just for comparison here is your original code:
 
Code:

int[][] world;
int count;
void setup() {
  size(500, 300);
  world = new int[width+2][height+2];
  count = 3;
  for (int i = 0; i < count; i++) world[(int)random(width)][(int)random(height)] = 1;
  background(32, 32, 0);
}
void loop() {
  for (int i = 0; i < width; i++) for (int j = 0; j < height; j++)  
  switch(world[i][j]) {
    case 0: if (pixels[j*width+i] != #202000) pixels[j*width+i] = #202000; break;
    case 1: if (pixels[j*width+i] != #FF0000) pixels[j*width+i] = #FF0000; break;
    case 2: if (pixels[j*width+i] != #FF6600) pixels[j*width+i] = #FF6600; break;
    case 3: if (pixels[j*width+i] != #FFBB00) pixels[j*width+i] = #FFBB00; break;
    case 4: if (pixels[j*width+i] != #FFFF00) pixels[j*width+i] = #FFFF00; break;
  }
  for (int i = 1; i < width+2; i++) for (int j = 1; j < height+2; j++)
  if (world[i][j] > 0 && !(world[i][j] == 4 &&  
 world[i-1][j] == 4 && world[i+1][j] == 4 && world[i][j-1] == 4 && world[i][j+1] == 4)) {
    int r1 = i+(int)random(3)-1;
    int r2 = j+(int)random(3)-1;
    if (r1 < 0) r1 = 0; if (r1 >= width) r1 = width-1;
    if (r2 < 0) r2 = 0; if (r2 >= height) r2 = height-1;
    if (world[r1][r2] < 4) {world[r1][r2]++; count++;}
  } if (count >= width*height*4-4) setup();
}

 
I'm sorry if that switch statement looks a bit wrinkled by the forum's formatting - it looks way sexier on your page - but well, it may look nice, but if it comes to "sexy" that switch block is the equivalent of a steam powered penis pump: way too bulky and slow. You can replace it completely by a lookup-table which among us sexy programmers is also called the "see-thru lingerie of code".
 
Together with that block we also get rid of those 10 nasty sins in the form of "j*width+i": Repetitive multiplication syndrome! You could have precalculated this just once, but why multiply at all? We'll get to that in a moment.  
 
The decision to use two- or one-dimensonal arrays is like the question if to put on a condom or not: two-dimensional is definitely safer if you are into experimentation, but for full performance one-dimensional is the way to go. Switching from two to one dimension saves us a complete loop and the multiplication, as we can now traverse the array from the end to the beginning and simply ignore the cells position in screen space.
 
But do we need that first loop at all? Not really. It's actually just some foreplay that you can skip if you are in a long-term relationship with your program. You will achieve the same effect if you move the part that sets the pixels to the inside.
 
Let's get to the spill - those pixels at the border that would end outside the frame and give you those nasty errors. Your method of adding a one pixel wide internal frame around the screen is fine, I prefer to wrap those pixels around and use for this purpose one of my favorite tools: the modulo operator: % - it will take care that none of my read or write attemps will end outside the set bounds.
 
I haven't measured it, but from looking at the time it takes to fill the screen I'd say that this code is a few inches faster:
 
Code:

int[] world;
int[] colors;
int count;
int maxCount;
int maxIdx;
void setup() {
  size(500, 300);
  maxIdx=width*height;
  world = new int[maxIdx];
  colors =  new int[]{#202000,#FF0000,#FF6600,#FFBB00,#FFFF00};
  maxCount=maxIdx*(colors.length-1);
  count = 3;
  for (int i = 0; i < count; i++) world[((int)random(maxIdx))%maxIdx] = 1;
  background(32, 32, 0);
}
void loop() {
  for (int idx = maxIdx; --idx>=0;) {
    if (world[idx] > 0 && !(
   world[idx] == 4 &&  
   world[(idx+1)%maxIdx] == 4 &&  
   world[(idx+width)%maxIdx] == 4 &&
   world[(idx-1+maxIdx)%maxIdx] == 4  &&  
   world[(idx-width+maxIdx)%maxIdx] == 4  
   )) {
   int r = (idx+(int)random(3)-1+width*((int)random(3)-1)+maxIdx)%maxIdx;
   if (world[r] < 4) {
     count++;  
     pixels[r]=colors[++world[r]];
   }
    }  
  }
  if (count >= maxCount) setup();
}

 
Theoretically it might be possible to speed this up even more by using a linked list instead of a static array, but that's maybe something for out next epsiode of "sexy syntax"...
 
Cheers
Mario
 

Mario Klingemann | Quasimondo | Incubator | côdeazur
arielm

WWW
Re: sexy syntax
« Reply #12 on: Jun 17th, 2004, 12:20pm »

don't know if it's sexy but...
 
when working with cellular automatas, it can save a lot of headaches to stick with a 2d array.
 
there's a trick that can make a 2d array as fast as a 1d, e.g.:
 
Code:
int[] world_x;
for (int x = 0; x < W; x++)
{
  world_x = world[x];
  for (int y = 0; y < H; y++)
  {
    world_x[y] = 0;
  }
}

in addition, what can make a cellular automata run hundred time faster is more in the "high level" optimisation ground.
 
the 2 domains to consider:
 
1- screen-redrawing
all this is not very relevant within processing, because it redraws the whole screen at each frame, but, within a framework that gives more control: it's possible to redraw (based on a "tile" abstraction) only those areas that have changed.
 
2- lattice (aka grid) scanning & processing
here again based on a "tile" abstraction (e.g. 16x16), it's possible to treat only those parts of the cellular automata that are "active".
 
depending on the kind of CA and its current state (i.e. not too "active"), it's possible to run huge lattices (say 1000x1000) at supersonic framerates.
 

Ariel Malka | www.chronotext.org
mattgilbert

tangramkid WWW Email
Re: sexy syntax
« Reply #13 on: Jun 17th, 2004, 6:06pm »

on Jun 17th, 2004, 1:56am, Quasimondo wrote:
Theoretically it might be possible to speed this up even more by using a linked list instead of a static array, but that's maybe something for out next epsiode of "sexy syntax"...

I don't mean to move too fast, but i'm curious about the linked lists. I remember these things being superfast when I learned Scheme back in Programming 1, and never knew how to use them in Java/Processing. (Anyone) care to explain Got a good link on the subject It's come up in a program I'm working on that linked lists might help me out a lot.
 
matt
 
 
mflux

fluxhavoc WWW
Re: sexy syntax
« Reply #14 on: Jun 19th, 2004, 1:04pm »

Hey Matt. Lemme see if I can help you hijack the thread.
 
My computer science is shabby, so the pros will have to spank me with a nerd stick after they're through with me. Here goes:
 
Linked lists can be thought of as objects with reference pointers to the next object in the array of the same type. Whew.. that was a long sentence. Let me clarify:
 
Code:

class cell
{
  cell next;
  cell()
  {
    next=null;
  }
}

 
to construct a linked list of these objects, you would constantly construct new objects and make the previous "next" equal to the next object on the list, therefore creating a 'linked' list.  
 
Code:

int num=200;
cell first=new cell();
cell temp=first;
for(int i=0;i<num;i++)
{
  temp=temp.next;
  temp=new cell();
}

 
 
You can also trick it out and make the list point backwards... or sideways....
 

 
and.. to cycle through this list is super easy:
 
Code:

cell temp=firstCell; //firstcell is = to the first in the array
while(temp.next!=null)
{
  //do something to temp
  temp=temp.next;
}

 
Now you can mess with this list like arrays never dreamed of, such as expanding the list to near-infinity by tacking on more stuff at the end, having objects themselves in the list include more lists of the same type at any length, etc etc.
 
I think that's pretty sexy.  
 
Um.. hope that helps and didn't deviate too much.
« Last Edit: Jun 19th, 2004, 1:15pm by mflux »  
Pages: 1 2 

« Previous topic | Next topic »