How can I use whitespaces to improve code readability

Pages: 123
Thank you so much everyone for the useful ideas and tips given.

I'll bookmark this topic for future reference.
vlad wrote:
So you write something as

for ( int* first = a, last = a + N; first != last; ++first ) { /*...*/ }

Oops! I made an error. int* is not the type of last!:)


I agree that cramming too much into a single for statement is bad style and makes code more difficult to read... which is why I would never write code like the above. Generally I find that the comma operator leads to many more errors/confusion than the way you declare your pointers.

So my advice is to not use the comma operator.

The usual error of beginners that they follow your style and write for example

int* a[10];


That is a great example of why int* is so clear.

T a[10]; creates an array of 10 T's. So if the type is a pointer (T==int*):
int* a[10]; this creates an array of pointers. It's exactly what anyone would expect. The pointer is part of the type, and you have an array of it. There is nothing confusing about that code.

while they wanted to write

int ( *a )[10];


First of all... int (*a)[10]; is confusing syntax any way you slice it.

Second of all... creating a pointer to an array is extremely rare and newbies virtually never intend to do it.

Thirdly, if they do... the way to really do it clearly is with a typedef:

1
2
typedef int int10[10];
int10* a;  // <- foo* syntax still works just fine 


To expand on that... what if you wanted an array of pointers to an array?
1
2
3
4
5
int (*a[5])[10]; // <- wtf is this?  Is this even right?  I honestly don't know

// vs.

int10* a[5]; // <- very clear... an array of 5 int10 pointers, where each int10 is an array of 10 ints 


I honestly don't know if that first declaration is even right. This syntax is just confusing muck. Fortunately it's extremely rare to have to do anything like this.



But again... none of this is the point. This is all personal preference. We can argue about which styles we prefer and why all day... and the bottom line is that neither of us are wrong... we just have differing opinions.

You just seem to have the mindset that your opinion is right and everyone else is wrong and therefore they must be stupid. That irritates me.
Last edited on
@Disch

vlad wrote:

So you write something as

for ( int* first = a, last = a + N; first != last; ++first ) { /*...*/ }

Oops! I made an error. int* is not the type of last!:)


I agree that cramming too much into a single for statement is bad style and makes code more difficult to read... which is why I would never write code like the above. Generally I find that the comma operator leads to many more errors/confusion than the way you declare your pointers.


When someone is doing some stupidy he has to do another stupidy that to justify the first stupidy. Relative to the example I showed that very often can be seen in algorithms including standard algorithms you will introduce variables in the scope where they are not used. So you are trying with this bad style of programming to justify the initial bad style of programming.
There is no "too much into a single for statement" as you think. The loop is very clear and readable. But it does not fit into your initially bad programming style. So you are trying to invent a restriction that results in even more bad style of programming.

By the way if you will look through realizations of standard algorithms you will see that with comma operators in loops they look more smart than without the comma operators. Though in the example I showed there is no comma operator.
Last edited on
vlad wrote:
When someone is doing some stupidy he has to do another stupidy that to justify the first stupidy.


I'm done talking with you vlad.
I think that bracket on same line is provably better, in theory, than on the next line. ;)

But it's a preference, you learn to be comfortable with your own style.

Vlad is funny, because if you read carefully, he is calling himself an idiot.
I use int* i. I generally avoid declaring more than one variable in a line, and if I do I'm well aware of the issue with pointers and pay special attention to it.
Though in the example I showed there is no comma operator.

Ouch.
With int* p it looks like * is associated with its left, but this is wrong when you use int const* p. By personal preference I align the pointer to what it actually is associated with: int *const p
@Catfish4


Though in the example I showed there is no comma operator.

Ouch.


What does mean this "Ouch" in this case?:)
vlad from moscow wrote:
What does mean this "Ouch" in this case?:)
http://i.imgur.com/8gp0jdX.png
@ Vlad: good retort from you, I thought about saying the same thing.

Unfortunately your tone stifles the discussion but maybe that's what you want?

Edit: @ LB: well oops I missed that as well it seems.
Last edited on
¿? there is no comma operator in that case
I think Disch is against the comma being used for anything besides lists of elements or parameters. So no comma operator and no comma in variable initialization. It's just simpler to sum it up with "comma operator" even though you're technically right that you didn't use the actual operator at all.
Last edited on
Yes. Thank you, LB, that was it exactly.
Topic archived. No new replies allowed.
Pages: 123