News for hackers. Stuff that matters.
Re:Show HN: Space Invaders in C by Luc on Friday December 06, 2019 @05:29AM (#21720573)
> bracket your ifs and place the starting { on the same line as if/while, or one day you'll sit there staring at the screen for 8 hours trying to figure out why your code no longer works due to an accidentally deleted line.

This is some rather strange advice. That and the rewrite dropped a '!'.

Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @06:30AM (#21720858)
That's advice borne from a lifetime of programming C. Good catch about the !, but that was just as an example of the form, not meant as a cut-and-paste replacement.
Re:Show HN: Space Invaders in C by kazinator on Friday December 06, 2019 @02:08PM (#21724967)
I'm at 30 years of programming in C, and don't understand the comment. I've never had a problem with either curly brace placement. If a line is deleted, and that line is a curly brace, there will be a syntax error. "git diff" (or whatever you're using) will show you what lines you have deleted.

Both ways have some merit. Cuddled curly is good for ismple one-liners. Curly on next line is when the statement header is stuffed with multi-line expressions.

After all the years, I have settled on:

  while (simple) {
  }

  while ((big || complex)
         (condition && multiple && lines))
  {
  }


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

  }

  for (big_initialization;
       big && step;
       complex, increment++)
  {
  }
Re:Show HN: Space Invaders in C by jacquesm on Friday December 06, 2019 @04:09PM (#21726007)
Those all work because in all cases single deleted lines will generate an error message.
Re:Show HN: Space Invaders in C by kazinator on Tuesday December 10, 2019 @12:56PM (#21754826)
Why are you still going on about this bizarre single line deletion phenomenon?

The only way a single line deletion that contains curly braces will still compile is if it contains balanced curly braces:

  if (cond)
  { foo }
  stmt;
All styles which avoid this do not have an issue with the deletion of random single line that contains a curly brace.

Regardless of bracing style, we can easily find lines in a program which can be deleted without causing a compile error, just not ones which open a syntax without closing it and vice versa.

Try not to randomly delete lines from your program, and inspect your "git diff" or what have you should such a thing happen.

Re:Show HN: Space Invaders in C by loadzero on Friday December 06, 2019 @06:36AM (#21720906)
BTW, I generally agree about extra indentation and braces for these kind of statements.

The code is a bit terse due to a mechanical issue - my current laptop screen only gives me about 25 lines vertically.

Re:Show HN: Space Invaders in C by pvg on Friday December 06, 2019 @06:16AM (#21720779)
Well, if you want to go all K&R you can just put the whole thing in a single while line, without the extra conditional inside. Single for line, if you're going for a more 'asshole C expert' style. If you're on your 200th hour trying to capture the flavour of the original 8085 assembly, you might end up writing something like what's in the code.