prettier-plugin-solidity icon indicating copy to clipboard operation
prettier-plugin-solidity copied to clipboard

Formatting Questions

Open Jaime-Iglesias opened this issue 6 years ago • 28 comments

I've been playing a little bit with this plugin and I have some questions/concerns:

I'm trying to format a test file that looks like this:

    function _foo (uint256 _param1, uint256 param2) external pure returns (uint256 something, uint256 watever) {
    ...
    }

Now, after formatting, I was expecting to get functions that follow//ressemble the Solditiy Style Guide

i.e something like this:

function _foo(
   uint256 _param1,
   uint256 param2,
) external pure returns (
   uint256 something,
   uint256 watever
) {
   ...
}

But to my surprise I got:

	function _foo(uint256 _param1, uint256 param2)
		external
		pure
		returns (uint256 something, uint256 watever)
	{
                ...
	}

do options exists to format code in such a way? Or is the plugin "opinionated" this way thus requiring a complete re-write on the formatting functions ?

Jaime-Iglesias avatar Apr 06 '20 15:04 Jaime-Iglesias

We do follow the style guide as much as possible. This format actually follows those guidelines, as they are not explicit in when to separate the params in multiple lines. The choice to break the parameters in multiple lines is decided purely on line length.

Janther avatar Apr 06 '20 19:04 Janther

we have been talking about having an option here and always have arrived at the conclusion not to. Any thoughts @fvictorio @mattiaerre ??

Janther avatar Apr 06 '20 19:04 Janther

We do follow the style guide as much as possible. This format actually follows those guidelines, as they are not explicit in when to separate the params in multiple lines. The choice to break the parameters in multiple lines is decided purely on line length.

@Janther Yeah, I do see the guidelines not being very opinionated about it (perhaps my comment could be seen as If I'm saying that you guys are not following the guidelines, which is not my intention) I just found it weird that the modifiers got formatted into one line each but the parameters remained in the same line making it quite weird (ofc this is just pure opinion) I was wondering if it would be feasible to allow customization in such a way that you could choose to separate only the parameters. only the modifiers or both - personally it's the first time I see code with the modifiers splitted into lines, but it's not uncommon to see parameters being splitted (atleast in my experience).

Jaime-Iglesias avatar Apr 06 '20 19:04 Jaime-Iglesias

well here is the issue with function declarations. we have parameters, modifiers, and return parameters. all of these are different groups. the desitions on where to split and what takes precedence do vary and some combinations might need a refactor. I guess the one that we have right now is the cleanest in terms of maintaining the library. Also for some reason, I personally like splitting first the modifiers 😄 I have played with the idea of splitting everything if the 80 characters are reached but that's only in my head now. Again I like that the community is vocal about these things and would love to hear ideas or opinions about this (preferably with examples).

Janther avatar Apr 06 '20 21:04 Janther

@Janther I see, tbh it seems like everyone is just doing their thing in terms of formatting. when you refer to examples, what do you mean exactly? examples of well-known contracts that use certain formatting styles? or examples as in a rationale for a specific format ?

Jaime-Iglesias avatar Apr 06 '20 22:04 Jaime-Iglesias

Like, to give some examples on different approaches to formatting code:

Gnosis splits both parameters and modifiers (although not consistently) AZTEC seems to go with only parameters too Dharma does parameters only, and very consistently Aave mixes both approaches in Maker they just do their own thing haha OpenZeppelin seems to split only parameters too

Ofc, this doesn't mean that one approach is better than the other, or that one is wrong or the other is right, I just wanted to point out that the style is very split.

Jaime-Iglesias avatar Apr 06 '20 22:04 Jaime-Iglesias

In a perfect world, I'd love to see as many cases and combinations (unless the suggestion is split everything always which is ugly for small signatures).

  • too many parameters
  • too many modifiers
  • modifier call with a long and complicated expression
  • too many return parameters

what we want to answer is, does this help read the code better? for example, I try to avoid the verticality of splitting into multiple lines as much as I can while trying to keep each line as readable as possible. We have been tackling this with the 80 characters per line rule but we could do a much more in-depth analysis of the AST.

Janther avatar Apr 06 '20 22:04 Janther

Like, to give some examples on different approaches to formatting code:

Gnosis splits both parameters and modifiers (although not consistently) AZTEC seems to go with only parameters too Dharma does parameters only, and very consistently Aave mixes both approaches in Maker they just do their own thing haha OpenZeppelin seems to split only parameters too

Ofc, this doesn't mean that one approach is better than the other, or that one is wrong or the other is right, I just wanted to point out that the style is very split.

I'll have a look at these but at the moment my priority with this project is bugs and the 0.6.0 features rather than redoing the formatting in a different way.

Thank you and I'll let you know what comes up.

Janther avatar Apr 06 '20 22:04 Janther

I totally understand your point.

Perhaps an alternative would be splitting based on number of parameters // returnParameters? For example, you always split the paramaters when the number is >= 2, and the same for the return paramaters - then to avoid massive identifiers you could use the max horizontal lines as a second check (for single parameters with huge identifier names).

I'll have a look at these but at the moment my priority with this project is bugs and the 0.6.0 features rather than redoing the formatting in a different way.

Thank you and I'll let you know what comes up.

For sure, just wanted to start a friendly discussion about the format, thx for the answers :D and bearing with me hahaha

Jaime-Iglesias avatar Apr 06 '20 22:04 Jaime-Iglesias

I like the 2 parameters rule.

Janther avatar Apr 06 '20 22:04 Janther

I like the 2 parameters rule.

hahaha! I think it can bring a lot of consistency

Jaime-Iglesias avatar Apr 06 '20 22:04 Jaime-Iglesias

Boom 💥 there you go. let's see what the team thinks about this

Janther avatar Apr 06 '20 23:04 Janther

I actually like #257 good stuff @Janther and thanks @Jaime-Iglesias for the idea/suggestion.

also, I'm thinking that maybe we should support some sort of externally pluggable rules that can work as overrides for specific cases so that we could technically support multiple printers? what do you think?

mattiaerre avatar Apr 07 '20 21:04 mattiaerre

I actually like #257 good stuff @Janther and thanks @Jaime-Iglesias for the idea/suggestion.

also, I'm thinking that maybe we should support some sort of externally pluggable rules that can work as overrides for specific cases so that we could technically support multiple printers? what do you think?

That sounds very cool, I guess the adoption would depend on how easy it's to write rules, to give an example rules on solhint 2.x are quite a pain to understand and write, whereas in 3.0 they are easy as f-ing pie.

Maybe a good starting point would be looking at different repos from known contracts/dev teams, like the ones I used as an example above and see what common style points they share and where differences are, contrast that with the Solidity style guidelines and think about possible minimal customization out-of-the-box.

For example: to me it seems like the differences in style are particularly big when formatting functions and it seems like pretty much everyone is comfortable following the standard when it comes to the other aspects, perhaps because it's more opinionated in those cases i.e identation, tabs, blank lines, control structures, etc... so perhaps the smart thing would be to remain heavily opinionated on those...

Jaime-Iglesias avatar Apr 07 '20 22:04 Jaime-Iglesias

So what you are saying is that you’d want people to propose their own printers that would be used instead of our nodes folder?

On Wed, 8 Apr 2020 at 11:00, Jaime Iglesias [email protected] wrote:

I actually like #257 https://github.com/prettier-solidity/prettier-plugin-solidity/pull/257 good stuff @Janther https://github.com/Janther and thanks @Jaime-Iglesias https://github.com/Jaime-Iglesias for the idea/suggestion.

also, I'm thinking that maybe we should support some sort of externally pluggable rules that can work as overrides for specific cases so that we could technically support multiple printers? what do you think?

That sounds very cool, I guess the adoption would depend on how easy it's to write rules, to give an example rules on solhint 2.x are quite a pain to understand and write, whereas in 3.0 they are easy as f-ing pie.

Maybe a good starting point would be looking at different repos from known contracts/dev teams, like the ones I used as an example above and see what common style points they share and where differences are, contrast that with the Solidity style guidelines and think about possible minimal customization out-of-the-box.

For example: to me it seems like the differences in style are particularly big when formatting functions and it seems like pretty much everyone is comfortable following the standard when it comes to the other aspects, perhaps because it's more opinionated in those cases i.e identation, tabs, blank lines, control structures, etc... so perhaps the smart thing would be to remain heavily opinionated on those...

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256#issuecomment-610660841, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHZQZUAZLXYV6MYM2DAKLLRLOV7DANCNFSM4MCMFLAA .

Janther avatar Apr 07 '20 23:04 Janther

It’s like a plugin for our plugin 😂

On Wed, 8 Apr 2020 at 11:21, Klaus Hott [email protected] wrote:

So what you are saying is that you’d want people to propose their own printers that would be used instead of our nodes folder?

On Wed, 8 Apr 2020 at 11:00, Jaime Iglesias [email protected] wrote:

I actually like #257 https://github.com/prettier-solidity/prettier-plugin-solidity/pull/257 good stuff @Janther https://github.com/Janther and thanks @Jaime-Iglesias https://github.com/Jaime-Iglesias for the idea/suggestion.

also, I'm thinking that maybe we should support some sort of externally pluggable rules that can work as overrides for specific cases so that we could technically support multiple printers? what do you think?

That sounds very cool, I guess the adoption would depend on how easy it's to write rules, to give an example rules on solhint 2.x are quite a pain to understand and write, whereas in 3.0 they are easy as f-ing pie.

Maybe a good starting point would be looking at different repos from known contracts/dev teams, like the ones I used as an example above and see what common style points they share and where differences are, contrast that with the Solidity style guidelines and think about possible minimal customization out-of-the-box.

For example: to me it seems like the differences in style are particularly big when formatting functions and it seems like pretty much everyone is comfortable following the standard when it comes to the other aspects, perhaps because it's more opinionated in those cases i.e identation, tabs, blank lines, control structures, etc... so perhaps the smart thing would be to remain heavily opinionated on those...

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/prettier-solidity/prettier-plugin-solidity/issues/256#issuecomment-610660841, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHZQZUAZLXYV6MYM2DAKLLRLOV7DANCNFSM4MCMFLAA .

Janther avatar Apr 07 '20 23:04 Janther

It’s like a plugin for our plugin 😂 … On Wed, 8 Apr 2020 at 11:21, Klaus Hott @.> wrote: So what you are saying is that you’d want people to propose their own printers that would be used instead of our nodes folder? On Wed, 8 Apr 2020 at 11:00, Jaime Iglesias @.> wrote: > I actually like #257 > <#257> > good stuff @Janther https://github.com/Janther and thanks > @Jaime-Iglesias https://github.com/Jaime-Iglesias for the > idea/suggestion. > > also, I'm thinking that maybe we should support some sort of externally > pluggable rules that can work as overrides for specific cases so that we > could technically support multiple printers? what do you think? > > That sounds very cool, I guess the adoption would depend on how easy it's > to write rules, to give an example rules on solhint 2.x are quite a pain > to understand and write, whereas in 3.0 they are easy as f-ing pie. > > Maybe a good starting point would be looking at different repos from > known contracts/dev teams, like the ones I used as an example above and see > what common style points they share and where differences are, contrast > that with the Solidity style guidelines and think about possible minimal > customization out-of-the-box. > > For example: to me it seems like the differences in style are > particularly big when formatting functions and it seems like pretty much > everyone is comfortable following the standard when it comes to the other > aspects, perhaps because it's more opinionated in those cases i.e > identation, tabs, blank lines, control structures, etc... so perhaps the > smart thing would be to remain heavily opinionated on those... > > — > You are receiving this because you were mentioned. > > > Reply to this email directly, view it on GitHub > <#256 (comment)>, > or unsubscribe > https://github.com/notifications/unsubscribe-auth/AAHZQZUAZLXYV6MYM2DAKLLRLOV7DANCNFSM4MCMFLAA > . >

exactly ... very meta lol

mattiaerre avatar Apr 08 '20 01:04 mattiaerre

Sorry for the late response. Some thoughts on this.

First, I still think we should avoid having options as much as possible. The ones we have (single vs. double quotes for example), we have them because they exist in prettier core. Even then, we could have chosen not to include them, but well, the expectation from a user is probably to have this supported. Plus, is a very easy option to support.

A plugin system is somewhat like adding options, but maybe even worse, because it will add a lot of complexity and limit what we can do. Since this is a 100% OSS project, with no support at all, I think keeping it low cost is important.

With respect to the original issue, I don't have a strong opinion. I like the number-of-items approach, especially because it's really simple to explain. It's a little "un-prettier", but that's fine by me.

Finally, I think the fact that a lot of projects are doing their own different thing means we can do whatever we think it's best, since there's no strong consensus that we should follow.

fvictorio avatar Apr 08 '20 01:04 fvictorio

Sorry for the late response. Some thoughts on this.

First, I still think we should avoid having options as much as possible. The ones we have (single vs. double quotes for example), we have them because they exist in prettier core. Even then, we could have chosen not to include them, but well, the expectation from a user is probably to have this supported. Plus, is a very easy option to support.

A plugin system is somewhat like adding options, but maybe even worse, because it will add a lot of complexity and limit what we can do. Since this is a 100% OSS project, with no support at all, I think keeping it low cost is important.

With respect to the original issue, I don't have a strong opinion. I like the number-of-items approach, especially because it's really simple to explain. It's a little "un-prettier", but that's fine by me.

Finally, I think the fact that a lot of projects are doing their own different thing means we can do whatever we think it's best, since there's no strong consensus that we should follow.

I think that's a very good argument, perhaps it would be great to take a step back and look at the thing from a different perspective.

If for example we bring argument to the extreme then there's only two ways of looking at it.

  1. Making the plugin as oppinionated as possible i.e a very rigid approach.

    • PROS: - "Easier" to implement. - "Easier" to maintain. - It's the "Prettier" way.
    • CONS: - You run the risk of oppinionating it in such a way that devs don't want to sacrifice their own style for the sake of consistency.
  2. Making a "swiss knife" kind of plugin, with tons of customizations and tools for every occasion.

    • PROS: - Adoption would be easier, as it would be customizable.
    • CONS: - Complexity would be too high. - Where do you draw the line on customization? - Maintainability would be awful. - It's the "un-Prettier" way.

I think that the way to approach it would be being as oppinionated as possible, specially in things that are more "innocuous" and for which the official style guide is more opinionated i.e Tabs, control structutes, identation, etc... This way you reduce the complexity while maintaining the "Prettier" philosophy and being true to the style guide.

So now the million dollar questions - where do you draw the line on customization? I think that the line has already been drawn for you by the official Solidity style guide, whatever is not there doesn't exist.

That said, whatever decision is made in terms of style should be oppinionated and customizable based on the official style guide - thus making the plugin flexible enough to fit different styles but oppinionated enough so it doesn't deviate from the official thing, but more importantly than anything, whatever decision is made, it should be as consistent as possible, because in the end, that's the end goal of using a formatting tool.

Hope that helps!

Jaime-Iglesias avatar Apr 08 '20 12:04 Jaime-Iglesias

Hey guys,

is there any ETA to when the PR will be merged? I'm looking forward to keep on experimenting with all the Formatting options :D

Jaime-Iglesias avatar Apr 11 '20 15:04 Jaime-Iglesias