JoshMarler / react-juce

Write cross-platform native apps with React.js and JUCE
https://docs.react-juce.dev
MIT License
761 stars 80 forks source link

Style Guide / Code Formatting #24

Open tobanteAudio opened 4 years ago

tobanteAudio commented 4 years ago

For future contributions, it would probably be very helpful to have a common style guide for source files (JavaScript, C++ & Markdown).

For C++ the only real option is clang-format, which is really nice once you find a config that you like.

For JavaScript and Markdown, there are more options. The one that I'm familiar with is Prettier, which does both JS & MD.

We could add a script which runs both tools on the source files.

Is this something you are interested in?

nick-thompson commented 4 years ago

💯 absolutely. I think that would be helpful even just for me, but also to lower the overhead for any interested contributors.

To be honest, I'm not very well versed in either Prettier or clang-format, but I've seen them both around a bunch. I've tried to keep pretty close to the JUCE code style in my C++ but my JavaScript formatting has just been habitual. If we can roughly formalize the existing style into a config setting for clang-format and prettier then I think that'd be perfect. Do you want to give it a try?

JoshMarler commented 3 years ago

@nick-thompson , definitely needed for beta?

nick-thompson commented 3 years ago

100%, let's add it (edit: keep it)

nick-thompson commented 3 years ago

Quick update here, @olidacombe has some work with Prettier going on the js side in #191.

This morning, I'm looking at clang-tidy and clang-format to cover the C++ side. Should be pretty easy to integrate these things into our workflow, but a prerequisite for clang-format seems to be picking and adhering to a .clang-format style. I'm not very well-versed in these, so figured I'd ask here: anybody familiar with some .clang-format options that land pretty close to the way the code is generally formatted in our codebase already?

JoshMarler commented 3 years ago

Not sure if you've seen this Nick but worth a look: https://forum.juce.com/t/automatic-juce-like-code-formatting-with-clang-format/31624/7

Granted we might not want to match juce's coding style exactly.

nick-thompson commented 3 years ago

Excellent, I'll poke at that later this week! Thanks

olidacombe commented 3 years ago

I am finding myself thinking about style guidance again in the context of props defined for core js components. TextInputProps is a good example, where there's a mixture of cases for the props (some are camelCase, some kebab-case). Although it's perhaps not worth losing sleep over, I think having consistency in the interface exposed to clients has value. What we've done with css props is convert to kebab-case when crossing to C++, so clients have a choice of case, and we use camelCase in the examples. If we want to "go consistent", 2 options are:

I seem to have talked myself out of option 2. But I think consistency in the api (i.e. core component props) is a good idea.

nick-thompson commented 3 years ago

Thanks @olidacombe!

I think having consistency in the interface exposed to clients has value. What we've done with css props is convert to kebab-case when crossing to C++, so clients have a choice of case, and we use camelCase in the examples.

I very much agree, and I think option 1 is my preference here too with perhaps a slight twist: what if we just set a convention that says that within the native code, all prop names are lowercase-with-hyphen, and in javascript code we run a "convert to lowercase-with-hyphen" on every prop key. For keys that are already lowercase-with-hyphen, this transform will do nothing, and for camelcased keys it will make the appropriate transformation. That way we align on a consistent format within native (and a format that agrees with Yoga's existing convention), and allow the end user to use camelcase or hyphen-case as they please.

How does that sound?

olidacombe commented 3 years ago

Yes @nick-thompson that sounds ideal. And I think keeping typescript interfaces in camelCase to favour usage aligned with react native will be the right move too. But yeah, this is easy to implement, understand, and it is as flexible as the end user could want 👍🏾