News for hackers. Stuff that matters.
Re:C puzzles by CapacitorSet on Thursday November 10, 2016 @10:41AM (#12921292)
I really dislike his choice of coding style. Compare:

  int CountBits (unsigned int x )
  {
      static unsigned int mask[] = { 0x55555555,
          0x33333333,
          0x0F0F0F0F,
          0x00FF00FF,
          0x0000FFFF
          } ;

          int i ;
          int shift ; /* Number of positions to shift to right*/
          for ( i =0, shift =1; i < 5; i ++, shift *= 2)
                  x = (x & mask[i ])+ ( ( x >> shift) & mask[i]);
          return x;
  }
as opposed to:

  int countBits (unsigned int x) {
    static unsigned int mask[] = {
      0x55555555,
      0x33333333,
      0x0F0F0F0F,
      0x00FF00FF,
      0x0000FFFF
    };

    int i;
    int shift; // Number of positions to shift to the right
    for (i = 0, shift = 1; i < 5; i++, shift *= 2)
      x = (x & mask[i]) + ((x >> shift) & mask[i]);
    return x;
  }
Re:C puzzles by btilly on Thursday November 10, 2016 @12:25PM (#12922303)
Really, are we going to argue about the One True Formatting style?

A 2 space indent is more compact. A 4 space indent is more readable for older people. Putting braces around blocks on their own lines highlights blocks. Putting braces inline is again more compact. Outdenting declarations highlights an important piece of information. Keeping them in line focuses on blocks. And so on.

None of these choices are particularly important. Being CONSISTENT does. This is his site, and his code. Adapt. If he comes to work with you, then he'll need to adapt to you.

Yes, it is a shock to see someone whose style is unfamiliar to you. Each of your choices has a reason behind it, and you may have thought through all of them. But it matters less than you think. Grow up and get over it. Be consistent with code around you.

Re:C puzzles by Koshkin on Thursday November 10, 2016 @04:42PM (#12924676)
> A 2 space indent... A 4 space indent...

I have been wondering for a while now why is it that everybody is stuck with this typewriter/punchcard mentality and assumes that only a fixed-width (non-proportional) font could be used to display program code on a computer screen. Would it not be more appropriate, in this century, to realize that since indentation (and spacing in general) is essentially something that only pertains to the graphical representation of program's code, it should be left to the editor/viewer software (and its user) to set, given a single tab or space character, the number of pixels (or inches or any other unit of length) that should be used to represent the indentation or white space? This should have nothing to do with the width of a character, and therefore using modern proportional fonts should be just as convenient and natural as it is when using a word processor.

Re:C puzzles by metaphor on Thursday November 10, 2016 @10:12PM (#12927279)
>> Would it not be more appropriate, in this century, to realize that since indentation (and spacing in general) is essentially something that only pertains to the graphical representation of program's code...

False assumption.

Suppose a language exists which: 1.) truncates all characters beyond the 80th prior to interpretation; 2.) will throw an exception during run-time if tab characters are used for indentation; 3.) strictly defines, in BNF, valid cases for the first 7 column-wise characters, of which 7 spaces is distinctly meaningful.

Although it may be "this century", I think I speak for more than a few who continue to maintain the corrected mistakes of our predecessors out of economic necessity...and I'm not talking about reaping a paycheck either.

Re:C puzzles by CapacitorSet on Thursday November 10, 2016 @03:24PM (#12923966)
>None of these choices are particularly important. Being CONSISTENT does.

The thing is - there isn't much consistency in his syntax. The most obvious example is the indentation, that appears to be 8 spaces except for one line which is indented with 4; there isn't consistency in whether to have a space or not before semicolons, before and after parentheses, or before and after binary operators.

Re:C puzzles by mkagenius on Thursday November 10, 2016 @01:23PM (#12922807)
> Be consistent with code around you

I sometimes forget what i did in last function, so in my code there might be inconsistencies and a mix of both styles found. I hope to get consistent some day but a working code is the priority right now :)

Re:C puzzles by btilly on Thursday November 10, 2016 @02:06PM (#12923212)
In a code base there ideally should be a single coding standard. Achieving this ideal always seems impossible. But various languages have various tools to enable mass reformatting to fix this if there is no current consistency. This turns the problem from being a technical one to a social one.

Good luck with reaching agreement with your fellow developers...

Re:C puzzles by Fifer82 on Thursday November 10, 2016 @11:14AM (#12921634)
I prefer the one who takes a dedicated line for the `{`. It leaves me the ability to group together chunks of related functionality, whereas the latter method throws out that option instantly.
Re:C puzzles by adekok on Thursday November 10, 2016 @04:56PM (#12924822)
This is a religious war. :( For me, having a screen full of (mostly) blank lines containing { and } is terrible. It means that the information density of the screen is low, to the point of being useless.
Re:C puzzles by lisper on Thursday November 10, 2016 @11:38AM (#12921895)
Huh??? Can you please elaborate on that? Because that makes no sense to me at all.
Re:C puzzles by bryondowd on Thursday November 10, 2016 @01:20PM (#12922776)
If all { and } are on their own lines, then it modestly simplifies the ability to visually scan from one to its match because they appear on the same column with nothing between them.
Re:C puzzles by lisper on Thursday November 10, 2016 @01:27PM (#12922850)
But if your code is properly indented then you can do the same thing with the first character in the line containing the open brace:

    if (foo) {
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    |   blah;
    }
Re:C puzzles by Fifer82 on Thursday November 10, 2016 @07:27PM (#12926180)
I honestly think it may just be something physical in the brain!

That style, especially in other languages with callback hell, feels tight. I can't breathe...

That feeling of can't breathe is far far more of a potent dagger in the heart, than simply scrolling one extra mouse wheel in a Class.

The open and airy, instantly identifiable shape of the code is just like the fresh sea air.

An old friend at work used to call that style "NotepadOpenBinaryFileStyle". The reason that stuck with me was due to its accuracy!

Re:C puzzles by lisper on Thursday November 10, 2016 @07:37PM (#12926256)
Heh, I have a similar visceral reaction to the "open" style: what a waste of precious screen real estate. Now my eyes have to scan a much bigger area in order to see all the relevant information, and I might even have to waste time scrolling around to find what I'm looking for that I would not have had to if only all that unnecessary white space were eliminated.

Putting the brace on the same line also makes it easier to grep for function definitions.

So why not:

    if (
        x>y
       )
    {
      code;
      ...
     }
    }
(I've actually seen that in real code.)
Re:C puzzles by babo on Thursday November 10, 2016 @12:22PM (#12922261)
The purpose of these puzzles is to move you out of your comfort zone. Take the none familiar indentation as part of that experience.
Re:C puzzles by zerocrates on Thursday November 10, 2016 @10:45AM (#12921331)
I was about to say I didn't think it made a big difference but I have no idea what's going on with that intraline whitespace.
Re:C puzzles by CapacitorSet on Thursday November 10, 2016 @10:52AM (#12921411)
To me, having consistent indentation does make a substantial difference - it is fundamental to understand at a glance what structures are present in the code, rather than "parsing" it in detail every time I'm looking for something.
Re:C puzzles by reacweb on Thursday November 10, 2016 @11:26AM (#12921768)
Consistency is the main point. The second layout is more compact and this makes a huge difference if you need to put code in a document (also a small difference for websites).

Having more compact code allows to have more code on the screen without using the scrollbar. For me, it is also more readable because I am used to it.

Re:C puzzles by Cyph0n on Thursday November 10, 2016 @10:49AM (#12921375)
If I saw this in a codebase, I'd assume the programmer's fingers slipped a lot.
Re:C puzzles by nickff on Thursday November 10, 2016 @12:21PM (#12922259)
I agree with you on some counts, but not on others.

  unsigned int countBits (unsigned int x)
  {
      int i;
      int shift; // number of positions to shift to the right
      static unsigned int mask[] = {
      0x55555555,
      0x33333333,
      0x0F0F0F0F,
      0x00FF00FF,
      0x0000FFFF};

      for (i = 0, shift = 1; i < 5; i++, shift *= 2)
          x = (x & mask[i]) + ((x >> shift) & mask[i]);

      return x;
  }
Where to put the first '{' depends on your development environment, as some hide the line where the bracket is, and some don't (when you hide a function or loop). Array initialization format is a tricky one, as the spacing is very dependent on how you are trying to visualize the data, but I would agree that all (leftmost) elements should be aligned to the same margin. I find the blank line between array and other declarations puzzling for both examples. I prefer to leave blank lines between code of the same indent (outside of declarations and initializations). Commenting using '//' allows you to comment out blocks of code on both sides of your line comment, so I'm with you there.

P.S. I do not like Hacker News' paragraph formatting

Re:C puzzles by CapacitorSet on Thursday November 10, 2016 @03:28PM (#12924013)
I wouldn't mind working with your coding style either: though I don't have the same standards for braces and array initialization, you apply your style consistently across the whole code.
Re:C puzzles by exprF on Thursday November 10, 2016 @12:24PM (#12922295)
I dislike your choice, too. So?

    int count_bits(uint32_t x)
    {
    	static const uint32_t mask[] = {
    		0x55555555,
    		0x33333333,
    		0x0f0f0f0f,
    		0x00ff00ff,
    		0x0000ffff
    	};
    	
    	for (int i = 0, shift = 1; i < 5; ++i, shift *= 2)
    		x = (x & mask[i]) + ((x >> shift) & mask[i]);
    	return x;
    }
Re:C puzzles by UhUhUhUh on Thursday November 10, 2016 @02:31PM (#12923446)
I agree, formatting is like punctuation in a narrative. Not massively significant but certainly not meaningless. There is a sort of rhythm, flavor, style, whatever you want to call it, that winds up being part of it all. And it can prevent mistakes.
Re:C puzzles by gowrikumar on Thursday November 10, 2016 @05:12PM (#12924976)
I have had prepared slide set explaining the algorithm a while back here. May be useful for understanding the code: http://www.slideshare.net/gkumar007/counting-bits-presentati...
Re:C puzzles by rationalhero on Thursday November 10, 2016 @03:24PM (#12923971)
Most of the modern IDE's have built-in auto code formatters. You can style your code according to your preference with just a couple of clicks. Unless you are using the plain old NOTEPAD for coding.
Re:C puzzles by mfukar on Thursday November 10, 2016 @01:40PM (#12922984)
Arguing about style?

I really thought that as a species we had decided it was a preference thing.