-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Choosing a project-wide indentation style. #2091
Comments
I think this is more of stylistic issue than a technical one. So I think it is not very important. But as this issue is created let the flame begins!
+1 |
@sorokin Let's be clear. You are supporting opening curly braces on their lines, right? |
@sledgehammer999 yes
|
Some tips for anyone wanting to give example on indentation using code. Here is how have a code block on github comments: |
Just a random thought. Could we discuss using 4 spaces for indentation? :-) |
Sure. Anything related to indentation is welcome here. |
spaces, 2.
There aren't any. Just a matter of style. :-) |
So @sorokin makes a suggestion of having 4 spaces as indentation. This is up for a vote too. |
I agree with @sorokin proposal for 4 spaces. for IFs with signle statement i believe there is no need for braces and actually i think that statement in IF operator should not be placed on next line. I don't really see much of point in that if you can just align your code so it could be look like a more compact switch()
Take for example this piece of code: if (alternative_speeds)
down_limit = pref->getAltGlobalDownloadLimit();
else
down_limit = pref->getGlobalDownloadLimit();
if (down_limit <= 0) {
// Download limit disabled
setDownloadRateLimit(-1);
} else {
// Enabled
setDownloadRateLimit(down_limit*1024);
} if (alternative_speeds) down_limit = pref->getAltGlobalDownloadLimit();
else down_limit = pref->getGlobalDownloadLimit();
if (down_limit <= 0) setDownloadRateLimit(-1); // Download limit disabled
else setDownloadRateLimit(down_limit*1024); // Enabled For me it looks the same. The original way is a little too big for me and i would prefer to have the second. Also i believe that there should be space after each coma. It should be the same for everyone but still it would be nice to have such rule :) Last i totally agree that there should be some sorta design rules which should be followed by everyone. It will make code look better |
Hi. botanegg@7d82e7f |
Regarding the indentation, I'm used to tabs, so I agree with the bump in the number of spaces from 2 to 4. As for the braces, I like the style of the Linux kernel (Chapter 3: Placing Braces and Spaces), but I'm pretty much open to anything as long as there are some defined guidelines. |
If you would like to propose another style please give examples. And since I read linux kernel's style: |
https://gist.github.com/botanegg/73cb6871628bd460b525 short something like java |
So basically @botanegg wants opening braces on the same line for both functions and if/switch. |
@sledgehammer999 that's weird, it doesn't do that to me (Qt Creator 3.2.1). |
Hello everyone!
...
From increasing the number of lines in the source program will not increase. Sources formatted for people, not for the compiler (we can write all in one line for compiler). The following are the issues that is necessary to solve.
|
I didn't argument my choices mostly because there are no arguments, I support what I'm used to with no specific reasons. As long as the code is readable, it's fine for me. In general, I prefer a compact style that doesn't affect the readability of the code. That means I'm usually against using one line for if statements, unless the statement is really simple. However in that case I'd use the ternary operator directly, so in my opinion anything that is hardly readable in a ternary form needs to be written on multiple lines. I support the idea of saving some lines as the chances of having entire blocks visible without scrolling are higher, so I'd skip braces if not necessary and wouldn't put them on separate lines. Another thing I'd like to bring up is line wrapping. Even though most of the text editors out there can wrap long lines automatically, I personally prefer wrapping the code manually at a given column. That said, the Qt Creator rules I linked in my previous comment are pretty close to how I'd write the code. |
Oh man! It is was only the 'indentation style' I'd be a really happy bunny. :) |
easy there buddy =) Talking serious there are more points worth considering:
P.S. My personal favourite: explicit void in function declarations/definitions, e.g. |
QtCreator Formatting Rules look like alternative between my own preferences and yours (and current qBittorrent formatting). Some rules from Linux kernel formatting style must be added to them:
|
|
class Foo
{
public:
Foo();
};
class Bar
{
public:
Bar();
}; Did you mean it? switch(val) {
case 1:
// do something...
break;
case 2:
// do something...
break;
default:
// do something...
} |
Personally I prefer 4 spaces for indentation, I feel it looks clearer and helps differentiate the blocks. As for the style of the curly braces, I am used to do int foo()
{ for functions and if (x > 0) {
} else {
} for conditionals and other blocks. Although, it is not really that important as long as there is consistency, so I am good with any way. |
I think - yes. But only simple getters/setters or constructors/destructors with empty body. class Foo : public Bar
{
public:
Foo(int arg1, int arg2) : Bar(arg1), val_(arg2) {}
~Foo() {}
void setValue(int val) { val_ = val; }
int getValue() const { return val_; }
void moreComplexFunc(int param); // defined in .cpp file
private:
int val_;
}; |
FFS!!!!!!!!!! Stop acting like spoilt children! NOBODY gives a toss who prefers what or why. The decision is the maintainers to make and EVERBODY ELSE falls in line, HE is project leader and therefore THE authority on the semantics. This whole problem STARTED because of every developer doing what they prefer, so now it is time to make qbittorrent become a cohesive project rather than a dog's breakfast of disparate coding styles and 'conventions'. |
Why are you acting like this? I specifically invited people to propose styles/standards and then vote on that. It's true that until now there wasn't a constistent style followed by everyone that resulted in the current qbt stylistic mess. That's why now we will define a standard, create a document and have everyone follow it. @ everyone |
One more remark. I hope it is clear that if we use any files from other projects (or embed source libraries), you will not need to change their formatting (only encoding and line endings if necessary). Or I am not right?.. |
+1 |
Because I'm trying to get you to see that there are FAR MORE pressing concerns than how many spaces to indent the code with. |
@chrishirst How much do you think the time spent on each of us is here to express his opinion? |
Yeah the rules are pretty much decided IMO. I just need to make them official. |
I created the wiki page and I am going to make commit too with the rules. |
Would be nice to have coding style template for Qt Creator. |
Are reformatting patches acceptable? |
+1
Of course. Be sure to mention this bug number in the commit message for tracing back. |
I have a question. The major difference between now and then is the number of spaces used to indent the code. How do we deal with this change? Should new requests include at least two commits, one to indent the files that are going to be changed and the others with the actual changes, or just one commit? In the first case, even if "git blame" and "git diff" are able to ignore whitespace changes, I think that splitting the commits will make things more clear. |
I was thinking of doing the change in spaces just before releasing v3.2.0. However, your way makes sense too. (I opened #2192 to track this) |
@sledgehammer999 is missing indentation in https://github.com/qbittorrent/qBittorrent/blob/master/CODING_GUIDELINES.md at 1.(a, b, c) intentional? int myFunction(int a)
{
//code
} |
Do you mean the un-indented comment? |
I am reopening this to discuss 2 more things that came to my mind:
class myClasss
{
public:
myClass(int a, int b, int c, int d);
private:
int priv_a;
int priv_b;
int priv_c;
int priv_d;
};
myClass::myClass(int a, int b, int c, int d)
: priv_a(a),
priv_b(b),
priv_c(c),
priv_d(d)
{
//code
}
enum days
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}; Why? What do you think? PS: If we go vertical what is best? myClass::myClass(int a, int b, int c, int d)
: priv_a(a),
priv_b(b),
priv_c(c),
priv_d(d)
{
//code
}
//OR
myClass::myClass(int a, int b, int c, int d)
: priv_a(a)
, priv_b(b)
, priv_c(c)
, priv_d(d)
{
//code
} |
Second. In this case it is easier to insert a conditional Directive at the end of the list. myClass::myClass(int a, int b, int c, int d)
: priv_a(a)
, priv_b(b)
#ifdef SOME_DEF
, priv_c(c)
, priv_d(d)
#endif
{
//code
} And, by the way, indented, too. |
@glassez |
Yes. In your example enum is intended, so I decided not to talk about it.
Yes. I agree with vertical enums, but for initialisers - optional. |
I agree with vertical enums and initializers. As for comma placement - I guess this is a matter of preference. |
I really don't have a strong preference on this that I could justify in any way, so whatever you choose, it's fine for me. |
Here's Qt Creator config. Not much to look at. <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorCodeStyle>
<!-- Written by QtCreator 3.2.82, 2014-11-26T02:08:28. -->
<qtcreator>
<data>
<variable>CodeStyleData</variable>
<valuemap type="QVariantMap">
<value type="bool" key="AlignAssignments">true</value>
<value type="bool" key="AutoSpacesForTabs">false</value>
<value type="bool" key="BindStarToIdentifier">false</value>
<value type="bool" key="BindStarToLeftSpecifier">true</value>
<value type="bool" key="BindStarToRightSpecifier">false</value>
<value type="bool" key="BindStarToTypeName">true</value>
<value type="bool" key="ExtraPaddingForConditionsIfConfusingAlign">false</value>
<value type="bool" key="IndentAccessSpecifiers">false</value>
<value type="bool" key="IndentBlockBody">true</value>
<value type="bool" key="IndentBlockBraces">false</value>
<value type="bool" key="IndentBlocksRelativeToSwitchLabels">false</value>
<value type="bool" key="IndentClassBraces">false</value>
<value type="bool" key="IndentControlFlowRelativeToSwitchLabels">true</value>
<value type="bool" key="IndentDeclarationsRelativeToAccessSpecifiers">true</value>
<value type="bool" key="IndentEnumBraces">false</value>
<value type="bool" key="IndentFunctionBody">true</value>
<value type="bool" key="IndentFunctionBraces">false</value>
<value type="bool" key="IndentNamespaceBody">true</value>
<value type="bool" key="IndentNamespaceBraces">false</value>
<value type="int" key="IndentSize">4</value>
<value type="bool" key="IndentStatementsRelativeToSwitchLabels">true</value>
<value type="bool" key="IndentSwitchLabels">false</value>
<value type="int" key="PaddingMode">1</value>
<value type="bool" key="SpacesForTabs">true</value>
<value type="int" key="TabSize">4</value>
</valuemap>
</data>
<data>
<variable>DisplayName</variable>
<value type="QString">qBittorrent</value>
</data>
</qtcreator> |
@sledgehammer999 CODING_GUIDELINES.md has errors - after |
What about camelCase vs snake_case? |
In the last files I have worked the past months camelCase seems much more prevalent so we should use that. |
Some of you have already noticed that qbt code doesn't follow the same indentation style for all of its code.
I started maintaining the project almost a year ago. I tried to follow one specific indentation style for new code. Here is an example of it.
Opening curly braces should be on the same level as the function or statement:
Else or else if shouldn't follow a closing curly brace but instead be on its own line
Also single statement(?) ifs shouldn't have curly braces
or
There is one exception on the above rule. Readability. There are cases where we have either too many nested if/else blocks or too many branches. In this case single statement ifs should use braces too.
Contributor @glassez believes that having the opening curly braces on their own line is better. And he also noted the lack of guidelines. By his view the code would look like this:
Of course I like my style better. I feel like the other style wastes whitespace. And I feel QtCreator does a great job drawing my style.
HOWEVER, I don't want to force my style down your throats. So I would like your comments and votes on which style to use. Or even suggestion of some other style. Or a suggestion for some other detail. (eg how to style if/else blocks).
This issue will have 2 stages:
The text was updated successfully, but these errors were encountered: