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

Add Py Scirpt Enforce CamelCase Naming Convention in Scala Code #384

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

linkedlist771
Copy link

Pull Request: Add Py Scirpt Enforce CamelCase Naming Convention in Scala Code

Description

This pull request introduces the check_camel_case.py script to the project. This script helps maintain consistent code style by automatically identifying and optionally fixing variables and functions named with underscores (snake_case) in the Scala files. Consistent use of camelCase improves readability and adheres to common Scala conventions.

Why CamelCase?

In Scala, camelCase is the preferred naming convention for variables, functions, and methods. It enhances code clarity by visually grouping words together and avoiding potential confusion with underscores (which often have special meanings in Scala).

Key Improvements

  1. Automated Detection: The script efficiently scans .scala files within a specified directory to find instances of snake_case naming.
  2. Optional Automatic Fixing: With the --fix flag, the script automatically replaces snake_case names with their camelCase equivalents, saving manual effort.
  3. Clear Reporting: Even without fixing, the script provides detailed reports of the file, line number, and column where snake_case violations occur. This helps developers quickly locate and correct issues.

Usage

  1. Place the Script: The script check_camel_case.py is located in the root directory of the project.
  2. Run the Script:
    • To check for violations:
      ./check_camel_case.py <directory>
      (Replace <directory> with the path to the directory containing your Scala code, e.g., src)
    • To fix violations automatically:
      ./check_camel_case.py <directory> --fix

Example Output

./check_camel_case.py src --fix
src/main/scala/Batch.scala:117:10: data_in is not in camelCase
Fixed: data_in -> dataIn
src/main/scala/Batch.scala:118:31: data_out is not in camelCase
Fixed: data_out -> dataOut
src/main/scala/Batch.scala:119:31: info_out is not in camelCase
Fixed: info_out -> infoOut

Additional Notes

  • The script currently handles simple snake_case to camelCase conversions. More complex patterns may require manual adjustment.
  • Consider incorporating this script into the CI/CD pipeline to automatically enforce camelCase naming in new code contributions.
  • I have not compiled the project and test the reformed code's functionality, we may need to chekc this.

In Scala, camelCase is the preferred naming convention for variables, functions, and methods. It enhances code clarity by visually grouping words together and avoiding potential confusion with underscores (which often have special meanings in Scala).
@poemonsense
Copy link
Member

Thanks for your work and contribution.

We need to address some issues before integrating this script into difftest.

  1. We should ensure our format script will not affect the functionalities of the source code, which is a basic requirement for formatting tools. Currently we are using clang-format and scalafmt to format our source code. They are mature tools for formatting the C++ and Scala code. Since this PR is creating a Python script for formatting, I'm assuming this requirement would be very difficult to achieve.

  2. I checked the formatted code, and found that some words in a Scala string are converted from underscore_word to camelCase (such as src/main/scala/common/SDCard.scala). They are not expected to be changed.

  3. We need to integrate the script into our CI flow and check the format on every PR. This requirement brings many challenges because current issue 1) will most likely break this automated workflow.

Given the difficulties in designing a Scala formatter, I would suggest trying existing and mature format tools. I assume it's too diffcult for a single person to maintain a fully-functioning Scala formatter. Are there any existing format tools capable of handling camelCase conversion?

@linkedlist771
Copy link
Author

Actually, I was trying to use scalafmt for this, but after extensive research, I found that it doesn't provide an "underscore_word to camelCase" tool, nor does scalastyle or its plugins. The only options seem to be writing custom configuration files (a .scala file for scalafmt, or an .xml file for scalastyle). As a result, I resorted to Python as an alternative.

Regarding the converted inline Verilog code, I converted it line by line without considering the code's functionality. I acknowledge that there might be more sophisticated approaches to achieve this, such as using abstract syntax trees (ASTs) to parse the Scala code, identify variables, and replace them. However, as you mentioned, maintaining such a solution could be challenging for a single person.

Based on my research, Scala lacks a dedicated tool for this conversion due to its relatively smaller user base compared to languages like Java. Although there are discussions about similar issues for Java, even their best solutions involve using regular expressions with subsequent manual checks.😥  

Therefore, my final suggestions are to either gradually change the code style or create a custom tool for this task, although the latter could be quite complex.

@poemonsense
Copy link
Member

I see. So there are no existing tools for converting variables to camelCase.

I agree with your suggestions on gradually changing the code style and creating a custom tool for camelCase formatting. Since this PR has some issues to be address, I would prefer to convert this PR to draft (or an issue) and see whether in the future we can find an appropriate way to do the formatting.

@poemonsense
Copy link
Member

Still, thanks very much for your contribution and your research. It's much helpful for the project

@linkedlist771
Copy link
Author

I'm happy to contribute to the open-source community and plan to submit an issue to the scalafmt project, proposing the implementation of this feature. I'll keep you informed of any updates or progress on this front.

@poemonsense
Copy link
Member

Thanks very much for your efforts!

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