4 ms·
I really dislike his choice of coding style. Compare: int CountBits (unsigned int x ) { static unsigned int mask[] = { 0x55555555, 0x333333
by CapacitorSet 10y ago
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;
}
- zerocrates 10y agoI 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.
- Cyph0n 10y agoIf I saw this in a codebase, I'd assume the programmer's fingers slipped a lot.
- CapacitorSet 10y agoTo 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.
- reacweb 10y agoConsistency 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.
- deleted 10y ago[deleted]
- Fifer82 10y agoI 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.
- lisper 10y agoHuh??? Can you please elaborate on that? Because that makes no sense to me at all.
- deleted 10y ago[deleted]
- bryondowd 10y agoIf 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.
- lisper 10y agoBut 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; }
- Fifer82 10y agoI 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!
- nickff 10y agoI 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
- CapacitorSet 10y agoI 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.
- babo 10y agoThe purpose of these puzzles is to move you out of your comfort zone. Take the none familiar indentation as part of that experience.
- exprF 10y agoI 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; }
- btilly 10y agoReally, 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.
- mkagenius 10y ago> 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 :)
- btilly 10y agoIn 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...
- CapacitorSet 10y ago>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.
- Koshkin 10y ago
- mfukar 10y agoArguing about style? I really thought that as a species we had decided it was a preference thing.
- UhUhUhUh 10y agoI 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.
- rationalhero 10y agoMost 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.
- gowrikumar 10y agoI 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-presentation http://www.slideshare.net/gkumar007/counting-bits-presentati...