"The shorter the code the beta the program"
What?
So you're saying that if I write 200 lines of unreadable code instead of, say, 250 lines of clear, readable, well formatted and nicely laid-out code and the output of the code is exactly the same, that's better than the second option?
Writing compact code is good, but writing readable code is far more important.
For example:
std::cout << (myBoolean) ? "True" : "False" << std::endl;
and
1 2 3
|
if (myBoolean) std::cout << "True";
else std::cout << "False";
std::cout << std::endl;
| |
?
are better than
1 2 3 4 5
|
if (myBoolean == true) {
std::cout << "True" << std::endl;
} else {
std::cout << "False" << std::endl;
}
| |
?
I would say yes, but those examples are ridiculously simple. The first version is very short but the third version is much easier to read than the other two.
Anyway; my first tip would be to use a more consistent indenting style. Alot of people write code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct myStruct
{
int member1;
} object1;
int myFunction(void)
{
if (true)
{
// do something
}
else
{
// do something
}
}
| |
which is known as the ANSI style, BSD style, or Allman style (because of it's use in ANSI examples and by some guy about who I have forgotten, but he worked on BSD and his surname was Allman).
Or, my personal style;
1 2 3 4 5 6 7 8 9 10 11
|
struct myStruct {
int member1;
} object1;
int myFunction(void) {
if (true) {
// do something
} else {
// do something
}
}
| |
which is known as the K&R style because it was used in the K&R C book. In the book, they declare functions with the braces like this:
1 2 3 4
|
int myFunction(void)
{
}
| |
but I do it as above.
And the last one I can remember, and, the least readable (to me), is the GNU style:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
struct myStruct
{
int member1;
} object1;
int myFunction(void)
{
if (true)
{
// do something
}
else
{
// do something
}
}
| |
I think it's something like that. Duoas uses that style, or at least, a similar style.
Anyway whichever style you choose (you can make up your own) notice that the majority of those use 4-space indentation. 2 spaces is fine, but personally I find anything smaller than 2 is hard to read.
Anyway the first thing I would recommend would be to choose a style and stick to it like mad.
Secondly, to quote helios:
helios wrote: |
---|
It looks quite short to me. |
80 lines isn't that much; but if you want to shorten it, the style I use (K&R style) tends to be very concise. Especially if you use multiple loops and if-else if-else ladders. :)