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

Code Style Change #5644

Closed
wants to merge 3 commits into from
Closed

Code Style Change #5644

wants to merge 3 commits into from

Conversation

ericdecanini
Copy link
Contributor

@ericdecanini ericdecanini commented Mar 28, 2022

Draft until entire project is reformatted. See bottom of description for more detail

Open for discussion!

Proposal Doc

Summary:

We want to make changes to the code style for better readability and to stick closer to Kotlin code conventions.

The changes include:

  • Function and class parameters in new lines
  • Disable when branches in column
  • Disable all continuation indentation
  • Enable trailing commas
  • Enable place ‘)’ on new line for if statements
  • Disable keeping comments at first column

These changes have been decided on through chats and polls.

The changes were made on a .editorconfig file which automatically generates a project.xml file for projects that include it, which then automatically applies the code style to the IDE. This will then affect the style applied when auto formatting code.

The aforementioned .editorconfig file was generated through selecting all the necessary options in Android Studio's kotlin code style and exporting it as such. The exported file did also include style config for other languages (Java, C++, etc.) but I removed those from the config as we don't have a concensus on that yet.

I'm currently testing if this works too with the linter, both locally as well as through the CI

Edit: This affects the CI linter so the entire project needs to be reformatted. Turning this PR into a draft until that's done

@github-actions
Copy link

github-actions bot commented Mar 28, 2022

Unit Test Results

110 files  ±0  110 suites  ±0   1m 22s ⏱️ +9s
195 tests ±0  195 ✔️ ±0  0 💤 ±0  0 ±0 
650 runs  ±0  650 ✔️ ±0  0 💤 ±0  0 ±0 

Results for commit 37a6e60. ± Comparison against base commit 94099f4.

♻️ This comment has been updated with latest results.

@ericdecanini
Copy link
Contributor Author

ericdecanini commented Mar 30, 2022

image

Poll: "Do we want to keep arrow alignment in when statements (case -> expression)"
Majority vote: No, don't keep it (7/11 votes)

@ericdecanini
Copy link
Contributor Author

image

Poll: "Do we want to start using trailing commas?"
Majority vote: Yes, let's start using them (5/8 votes)

@ericdecanini
Copy link
Contributor Author

image

Poll: Do we want to placing ')' on new line for if statments?
Majority vote: Enable it (3/7)

@ericdecanini ericdecanini force-pushed the task/eric/code-style-changes branch from 9f13d0d to 94099f4 Compare April 6, 2022 17:14
@ericdecanini ericdecanini reopened this Apr 6, 2022
@ericdecanini ericdecanini changed the title Code Style Change [DISCUSSION] Code Style Change Apr 7, 2022
@ericdecanini ericdecanini requested review from a team, ouchadam and bmarty and removed request for a team April 7, 2022 08:52
@ericdecanini ericdecanini marked this pull request as draft April 7, 2022 09:44
Copy link
Member

@bmarty bmarty left a comment

Choose a reason for hiding this comment

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

Some thoughts. Also it will be a nightmare to handle so many (formatting) diff for the forks with lots of changes :/. Does it worth it?

image

.editorconfig Outdated
end_of_line = crlf
indent_size = 4
indent_style = space
insert_final_newline = false
Copy link
Member

Choose a reason for hiding this comment

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

it was true, I think it should stay true

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch!

@@ -20,7 +20,7 @@ import android.view.View
import im.vector.lib.attachmentviewer.databinding.ItemAnimatedImageAttachmentBinding

class AnimatedImageViewHolder constructor(itemView: View) :
BaseViewHolder(itemView) {
BaseViewHolder(itemView) {
Copy link
Member

Choose a reason for hiding this comment

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

is it the "continuation indent" thing?
I do not like the new format :/

class AnimatedImageViewHolder constructor(itemView: View) :
    BaseViewHolder(itemView) {
    val views = ItemAnimatedImageAttachmentBinding.bind(itemView)

is more confusing than

class AnimatedImageViewHolder constructor(itemView: View) :
        BaseViewHolder(itemView) {
    val views = ItemAnimatedImageAttachmentBinding.bind(itemView)

if I am reading it correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is indeed. The ideal formatting here should be as follows:

class AnimatedImageViewHolder constructor(
    itemView: View,
) : BaseViewHolder(itemView) {
    val views = ItemAnimatedImageAttachmentBinding.bind(itemView)

I'll investigate if I can get the formatter to achieve this

R.layout.item_video_attachment -> VideoViewHolder(itemView)
else -> UnsupportedViewHolder(itemView)
R.layout.item_video_attachment -> VideoViewHolder(itemView)
else -> UnsupportedViewHolder(itemView)
Copy link
Member

Choose a reason for hiding this comment

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

You will not convince me that the new format is easier to read. (but I know the advantages).
Applicable to nearly all the when statements.
Probably because to me code readability is top priority.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is mine too, but personally I find this easier to read than if they're all aligned. It also may well be the case that this is easier or harder to read based on what you're used to and falls into the realm of subjective, hence, the earlier poll

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AttachmentViewerActivity">
xmlns:tools="http://schemas.android.com/tools"
Copy link
Member

Choose a reason for hiding this comment

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

I do not understand why we now have 8 spaces for an indent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

A mistake. Good spot!

@ericdecanini
Copy link
Contributor Author

Some thoughts. Also it will be a nightmare to handle so many (formatting) diff for the forks with lots of changes :/. Does it worth it?

image

I agree that we have to give this some proper thought before pushing through with it. For the branches of the main fork, us all autoformatting on our own branches should work but this could be a lot more involved when it comes to the forks. We might have to look at alternate approaches to this, but I do very much think it's worth it as it boosts the code's readability up a lot and opens avenues for even more improvements

@bmarty
Copy link
Member

bmarty commented Apr 7, 2022

Have you considered re-enabling disabled rules? See https://github.com/vector-im/element-android/blob/main/build.gradle#L107

@ericdecanini ericdecanini force-pushed the task/eric/code-style-changes branch from 8efcec4 to 1c2b55d Compare April 9, 2022 09:13
@ericdecanini
Copy link
Contributor Author

Have you considered re-enabling disabled rules? See https://github.com/vector-im/element-android/blob/main/build.gradle#L107

Thanks for pointing this out! We'd most probably want to

@ericdecanini
Copy link
Contributor Author

ericdecanini commented Apr 11, 2022

Closing as we decided on the approach of tackling these changes one at a time. I'll make these changes on separate branches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants