Skip to content
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

[docs]Update code guidelines for readability of setting table-like structures #16924

Merged
merged 1 commit into from Dec 5, 2019

Conversation

DaveTBlake
Copy link
Member

Propose an update to code guidelines to allow for initialization of table-like strutures to be vertical aligned using whitespace and exceed line limits for readability. Thus option to skip Clang formating on these rare ocassions.

In the end human judgement on what is readable can be better than any set of Clang format rules.
For example

static const CGUIDialogMediaFilter::Filter filterList[] = {
{ "movies", FieldTitle, 556, SettingType::String, "edit", "string", CDatabaseQueryRule::OPERATOR_CONTAINS },
{ "movies", FieldRating, 563, SettingType::Number, "range", "number", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "movies", FieldUserRating, 38018, SettingType::Integer, "range", "integer", CDatabaseQueryRule::OPERATOR_BETWEEN },
//{ "movies", FieldTime, 180, SettingType::Integer, "range", "time", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "movies", FieldInProgress, 575, SettingType::Integer, "toggle", "", CDatabaseQueryRule::OPERATOR_FALSE },
{ "movies", FieldYear, 562, SettingType::Integer, "range", "integer", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "movies", FieldTag, 20459, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "movies", FieldGenre, 515, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "movies", FieldActor, 20337, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "movies", FieldDirector, 20339, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "movies", FieldStudio, 572, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldTitle, 556, SettingType::String, "edit", "string", CDatabaseQueryRule::OPERATOR_CONTAINS },
//{ "tvshows", FieldTvShowStatus, 126, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldRating, 563, SettingType::Number, "range", "number", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "tvshows", FieldUserRating, 38018, SettingType::Integer, "range", "integer", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "tvshows", FieldInProgress, 575, SettingType::Integer, "toggle", "", CDatabaseQueryRule::OPERATOR_FALSE },
{ "tvshows", FieldYear, 562, SettingType::Integer, "range", "integer", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "tvshows", FieldTag, 20459, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldGenre, 515, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldActor, 20337, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldDirector, 20339, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "tvshows", FieldStudio, 572, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "episodes", FieldTitle, 556, SettingType::String, "edit", "string", CDatabaseQueryRule::OPERATOR_CONTAINS },
{ "episodes", FieldRating, 563, SettingType::Number, "range", "number", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "episodes", FieldUserRating, 38018, SettingType::Integer, "range", "integer", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "episodes", FieldAirDate, 20416, SettingType::Integer, "range", "date", CDatabaseQueryRule::OPERATOR_BETWEEN },
{ "episodes", FieldInProgress, 575, SettingType::Integer, "toggle", "", CDatabaseQueryRule::OPERATOR_FALSE },
{ "episodes", FieldActor, 20337, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "episodes", FieldDirector, 20339, SettingType::List, "list", "string", CDatabaseQueryRule::OPERATOR_EQUALS },
{ "musicvideos", FieldTitle, 556, SettingType::String, "edit", "string", CDatabaseQueryRule::OPERATOR_CONTAINS },

is more reable than after reformatting with Clang

static const CGUIDialogMediaFilter::Filter filterList[] = {
    {"movies", FieldTitle, 556, SettingType::String, "edit", "string",
     CDatabaseQueryRule::OPERATOR_CONTAINS},
    {"movies", FieldRating, 563, SettingType::Number, "range", "number",
     CDatabaseQueryRule::OPERATOR_BETWEEN},
    {"movies", FieldUserRating, 38018, SettingType::Integer, "range", "integer",
     CDatabaseQueryRule::OPERATOR_BETWEEN},
 ... [over 64 lines]
}

Hence permit the use of use // clang format off ... // clang format on in these situations.

@DaveTBlake DaveTBlake added Type: Improvement non-breaking change which improves existing functionality Documentation v19 Matrix labels Nov 16, 2019
@DaveTBlake DaveTBlake added this to the Matrix 19.0-alpha 1 milestone Nov 16, 2019
Copy link
Contributor

@phunkyfish phunkyfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table like structures are one example of where vertical alignment is acceptable. What are the others? SQL blocks for instance would be another case.

I think this section should be explicit on where this is permitted.

Copy link
Member

@Rechi Rechi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general vertical alignment isn't allowed to prevent having to realign a whole block, because a longer value is added. What should be done in that case for such sections? Should the block be realigned if the longest value is removed?

The lines in the example are longer then the current maximum line length of 100 characters and I see no exception about the line length in the new section.

Actually reading the new section only tells one can add // clang-format off and // clang-format on, but the nothing tells that all the code guidelines (including maximum line length and no vertical alignment) do not apply for that section.

@yol
Copy link
Member

yol commented Nov 17, 2019

In general vertical alignment isn't allowed to prevent having to realign a whole block, because a longer value is added. What should be done in that case for such sections? Should the block be realigned if the longest value is removed?

If a longer value is added, realign - no way around it. And in this special case, I think that the benefit of having this code be readable in general beats having to do one or two more git blame steps in case you have to trace back something. If the longest value is removed, just use good judgement. If it's 1 character difference, no need to reformat it. If it's 20, different story.

I think this section should be explicit on where this is permitted.

It doesn't hurt to have some examples, but I don't think it has to be exhaustive.

@DaveTBlake
Copy link
Member Author

The lines in the example are longer then the current maximum line length of 100 characters and I see no exception about the line length in the new section.

... nothing tells that all the code guidelines (including maximum line length and no vertical alignment) do not apply for that section.

Fair point @Rechi I have re-drafted as a separate section and added guidance on the points you raised.

@phunkyfish I could not find any SQL I would apply this to as an example, but not saying that there isn't some somewhere. I really don't think we need to be too prescriptive in these guidelines, or try to cover everything explicitly.

Since we are going to apply our guidelines via Clang script, and at some point automatically update all the code base to a common standard, we need to be practical and allow for those rare situations where the code would be made less readable by such automatic formatting. Sometimes the human knows best!

@phunkyfish
Copy link
Contributor

phunkyfish commented Nov 19, 2019

It was just the other example I could think of where you want to keep specific formatting.

Example:

m_pDS->exec(
"CREATE TABLE channels ("
"idChannel integer primary key, "
"iUniqueId integer, "
"bIsRadio bool, "
"bIsHidden bool, "
"bIsUserSetIcon bool, "
"bIsUserSetName bool, "
"bIsLocked bool, "
"sIconPath varchar(255), "
"sChannelName varchar(64), "
"bIsVirtual bool, "
"bEPGEnabled bool, "
"sEPGScraper varchar(32), "
"iLastWatched integer, "
"iClientId integer, " //! @todo use mapping table
"idEpg integer, "
"bHasArchive bool"
")"
);

SQL can be hard enough to decipher if complex so it's another case where vertical alignment can make it easier to read.

@DaveTBlake
Copy link
Member Author

Ah, SQL tastes vary @phunkyfish as does how you view that SQL. In the RDBMS itself and in debug the SQL is a single string again and the whitespace does not help at all.
The Clang formatted version of those lines seems just as readable to me

m_pDS->exec("CREATE TABLE channels ("
              "idChannel            integer primary key, "
              "iUniqueId            integer, " 
              "bIsRadio             bool, " 
              "bIsHidden            bool, " 
              "bIsUserSetIcon       bool, " 
              "bIsUserSetName       bool, " 
  . . .
              "bHasArchive          bool" 
              ")");

But I don't know if some other whitespace checker would get upset.
Having said that I would not object to SQL being layed out with whitespace, nor would I enforce it.

@phunkyfish
Copy link
Contributor

Hah, you know I didn’t even check it. I just assumed clang-format would do something funny with it!

@DaveTBlake
Copy link
Member Author

You know I think we should let Clang formatter lose on the codebase as a test, and carefully inspect the end results for areas that need "protection" or changes to the format (if possible). I am totaly for having an automated tool that helps maintain a common code standard, just hesitant about when we apply such a change across the codebase, and believe we need to do it intelligently.

docs/CODE_GUIDELINES.md Outdated Show resolved Hide resolved
@enen92
Copy link
Member

enen92 commented Nov 25, 2019

@DaveTBlake code guidelines are also generated from doxygen (using the same .md file). Can you compile the docs and check if the change doesn't break anything?

…ctures to be vertical aligned and exceed line limits for readability
@DaveTBlake
Copy link
Member Author

DaveTBlake commented Dec 1, 2019

Can you compile the docs and check if the change doesn't break anything?

@enen92 I don't know how to do that?

@DaveTBlake
Copy link
Member Author

@Rechi I have reworded again, I hope this is now sufficiently explicit.

@enen92
Copy link
Member

enen92 commented Dec 2, 2019

You just need to install doxygen and run doxygen Doxyfile.doxy in docs/doxygen folder. An html page will be generated one level up (in docs). Code guidelines is an entry in the sidebar menu, see: https://codedocs.xyz/xbmc/xbmc/md_docs__c_o_d_e__g_u_i_d_e_l_i_n_e_s.html

@DaveTBlake
Copy link
Member Author

@enen92 I can confirm that this change works correctly in the Doxygen generated docs, thanks for pointing out this extra check.

Anyone else want to comment, otherwise I will merge this tomorrow.

@DaveTBlake DaveTBlake merged commit 0b09b48 into xbmc:master Dec 5, 2019
@DaveTBlake DaveTBlake deleted the docsKeepFormat branch December 5, 2019 14:29
Maven85 pushed a commit to Maven85/kodi that referenced this pull request Jan 21, 2020
[docs]Update code guidelines for readability of setting table-like structures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Type: Improvement non-breaking change which improves existing functionality v19 Matrix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants