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

Updating coding style of 2D shapes #35

Merged
merged 3 commits into from
Oct 9, 2020
Merged

Updating coding style of 2D shapes #35

merged 3 commits into from
Oct 9, 2020

Conversation

Irev-Dev
Copy link

Hi Loong Jin,

After we spoke about some coding style rules I kept thinking of more, and instead of asking you about each one I just went ahead and implemented my own rules on one file and stated them below. Please let me know which ones you want changed.

Unfortunately this doesn't review nicely with github, it seems to not recognise line modifications where indentations have change, and instead shows whole blocks removed and added. It must have something to do with tabs being replaced with spaces!?
I also had trouble where the indentation looked correct on my editor (VSCode) but when pushed to github it was wrong again. I fixed it by redoing every single line's indentation, I'll have to come up with a better solution in future.

The examples all still worked when I finished. So I don't think I broke anything.

Preliminary code rules:

  • Variable class and module names should be meaningful in preference to being short (I did not change any variable names)
  • Variable should only use abviations for widely accepted conventions such or (x,y,z and r) any others?
  • 4 spaces for indentation
  • variable_names should use underscores between words
  • ClassNames should use camel case starting with a capital.
  • moduleNames should use camel case NOT starting with a capital.
  • New line for each child in a chain with no extra indentation
  • blank line between each chain
  • new line for curly braces '{' except for module definitions
  • space between comment and slashes ie // some comment with space in front
  • examples to be wrapped in example module
  • /* */ block comments reserved for multi-line comments not for commenting out examples
  • Max line length of 80
  • if the first line of a module definition breaks max line length, each argument should be on a new line
  • if array definition breaks max line length each element should should be on a new line

@Irev-Dev
Copy link
Author

Is the problem I mentioned about github not recognising individual changes with indentation going to be a problem when it comes to merging other branches? and therefore should updating the code style be left to much later, when the repo is much more tidy?

@kintel
Copy link
Member

kintel commented Feb 23, 2018

I think official guidelines for code written in the OpenSCAD language makes a lot of sense, at least for code and libraries maintained and endorsed by the extended OpenSCAD team.
Some initial comments:

variable_names should use underscores between words
I feel there are too many schools of thought here. I tend to use mixedCase for variable names myself.
The key is to try to be consistent within one script.

ClassNames should use camel case starting with a capital.
I assume this one is copy pasta from some other guidelines?

moduleNames should use camel case NOT starting with a capital.
I tend to think of modules as "classes" in other languages and hence use CamelCase for them.
For built-in modules, we use snake_case. This differentiation is very similar to how the C++ standard library does it.

New line for each child in a chain with no extra indentation
This is a bit unclear to me. Are you talking about newlines caused by too long lines, or chains in general? I find long chains to be tricky to get right, stylewise, in OpenSCAD.
I like constructs like this:

rotate(...) translate(...) object(); // Single-line statement
if (expression) someObject(); // Single-line if

if (expr) {
    anotherObject(); // Multi-line if: Always use curly braces for grouping
}   

// Multi-line single-expression chain. Indentation helps see where it ends without using curly braces for grouping
rotate(someVeryLongArgument)
    translate(anotherVeryLongArgument)
        MyObject(yetAnotherVeryLongArgument);

Note: There is no penalty for using curly braces to group "multi-line single-expression" chains, but I also don't find the result to be very readable.

blank line between each chain
I find it hard to enforce blank line rules. I think blank lines should be used to separate blocks of code that has a natural/logical grouping. One line per chain sounds like a lot of space.

new line for curly braces '{' except for module definitions
I would argue that the same rule can be used everywhere. If anything, I tend to do the opposite: Don't use newlines before { unless there is a good reason to (e.g. for large modules where it makes code stand out more.

examples to be wrapped in example module
This will cause the example module to be exported into the global namespace. This may not be the intention behind the examples.

Max line length of 80
A bit old school. Most styles I see these days have moved to ~120.
Since OpenSCAD tends to produce long lines, this makes life a bit easier.

@hyperair
Copy link

hyperair commented Feb 26, 2018 via email

@hyperair
Copy link

Unfortunately this doesn't review nicely with github, it seems to not recognise line modifications where indentations have change, and instead shows whole blocks removed and added. It must have something to do with tabs being replaced with spaces!?
I also had trouble where the indentation looked correct on my editor (VSCode) but when pushed to github it was wrong again. I fixed it by redoing every single line's indentation, I'll have to come up with a better solution in future.

I haven't had time to look at this properly yet, but it's normal for whole-file indentation changes to look like entire blocks were modified.

@hyperair hyperair merged commit 3557ecb into openscad:master Oct 9, 2020
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.

3 participants