std::cout << "This is ""a multiline" //literal concatentation
<< "cout statement." << std::endl
<< "I like to have "
<< "the << line up "
<< "but then he endl "
<< "will be at the end" << std::endl;
int main(){
std::cout << "O Hai Thar";
return 0;
}
The reason is simple:
when you want to determine a block of code it's easier if opening/closing braces are on the same level. That way you just have a go up some lines and you got it, otherwise your eyes have to stray all over the code
@coder777, do you really think you need {}s to tell where a block is? Can you say that
1 2 3 4
int main()
std::cout << "O Hai Thar";
return 0;
is somehow unclear? Before you say that this will get confusing in a function of 100 loc and 10 levels of indentation, I'll remind you that if you have that sort of functions in your code, you ought to be punched.
class MyClass // classes, structs, enums all in upper case.
{
private:
int myInt; // lower case for private field.
public:
MyClass();
~MyClass();
int MyInt; // Upper case for public field.
// Upper case for functions, which I use to refer them as methods in C#
void MyFunction();
};
void MyClass::MyFunction()
{
int a = 0;
double b = 1.5;
while (a < 10) // single line, no braces.
a++;
while (b < 10)
{
b += 0.1;
std::cout << b << std::endl;
}
}
int main()
{
MyClass myClass // lower case for all variables.
return 0;
}
Because in C# there are Interfaces, it was hard at first getting used to putting the class members stuff outside of the class. As in C# everything is inside the class braces. If you've never used C# I will not go in details here as it is beyond the scope of this thread. But feel free to look up C# interfaces on MSDN.
Enums, Structs, and so forth all following the same rules as above.
I believe one-liners should live up to their name and actually be one line:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if(condition)
statement; //wrong!
statement; //I'm a python coder and don't know better!
if(condition) statement; //better, one line
if(condition){
statement; //kind of claustrophobic, but much better
statement;
}
if(condition)
{
statement; //ahh, much better
statement;
}