News for hackers. Stuff that matters.
Re:I don't like Python. Does that make me a bad person? by cookiecaper on Saturday June 26, 2010 @07:16AM (#1463499)
The indentation is not a big deal at all. You indent your code anyway, right? Python doesn't enforce a specific standard, you can indent however you want, just as long as you use the same units of indentation consistently.

The only time I've had a problem with indentation is while copy-pasting things, occasionally that can get messed up and be annoying. Everything else is fine.

Re:I don't like Python. Does that make me a bad person? by RodgerTheGreat on Saturday June 26, 2010 @09:31AM (#1463647)
The main reason I like having explicit braces and non-significant whitespace is that it allows me to use my whitespace flexibly to clarify the structure of a program.

There are some interesting ideas regarding the use of whitespace in C here:

http://www.quinapalus.com/coding.html

I don't agree with everything he has to say- for example, I think his nested for loop example would be far nicer as something like this:

  for(i=0;i<10;i++) {
  for(j=0;j<10;j++) {
  for(k=0;k<10;k++) {
  	printf("%d %d %d\n",i,j,k);
  }
  }
  }
(Not to mention more in keeping with the principle of "alignment".) Still, if nonstandard indentation can make it easier to see the patterns in your code, it benefits both development and maintenance.
Re:I don't like Python. Does that make me a bad person? by thedigitalengel on Saturday June 26, 2010 @09:51AM (#1463665)
Would it not be better this way:

for (i = 0; i < 1000; i++)

     printf ("%d %d %d\n", i / 100, (i % 100) / 10, i % 10);
IMHO, such patterns (multiple nesting with empty outer loops), in most cases, are really an indication of just one real loop. Using multiple counters might also be an option.

In case the loops are doing something different, they should be indented separately. For instance, the following makes little sense:

for (i = 0; i < 10; i++) {

foo (i);

for (j = 0; j < 10; j++) {

     foo (i + j);
for (k = 0; k < 10; k++) {

foo (i + j + k);

}

}

}

Re:I don't like Python. Does that make me a bad person? by RodgerTheGreat on Saturday June 26, 2010 @10:25AM (#1463707)
I absolutely agree with your second example, but I think you're missing my point for nested loops. Perhaps this would be a better example:

Let's say I'm making a simple tile-based game that uses a two-dimensional array called "grid" to store the map. To draw that map to the screen, I might do something like the following:

  for(int x = 0; x < grid.length;    x++) {
  for(int y = 0; y < grid[0].length; y++) {
  	drawTile(grid[x][y], x * tileWidth, y * tileHeight);
  }
  }
There are other ways to refashion this as a single loop, (storing everything in a 1d array and handling the dimensioning ourselves, using an iterator and giving tiles a position attribute containing their (x,y) coordinates, etc) but is any of that really more straightforward?

Again, I am not arguing for indentation anarchy, merely that there are times when it can help to bend the rules.

edit: I imagine you'd prefer something like this?

  for(int t = 0; t < grid.length * grid[0].length; t++) {
  	int x = t % grid.length;
  	int y = t / grid.length;
  	drawTile(grid[x][y], x * tileWidth, y * tileHeight);
  }