Add Py Scirpt Enforce CamelCase Naming Convention in Scala Code
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
- Automated Detection: The script efficiently scans
.scalafiles within a specified directory to find instances of snake_case naming. - Optional Automatic Fixing: With the
--fixflag, the script automatically replaces snake_case names with their camelCase equivalents, saving manual effort. - 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
- Place the Script: The script
check_camel_case.pyis located in the root directory of the project. - Run the Script:
- To check for violations:
(Replace./check_camel_case.py <directory><directory>with the path to the directory containing your Scala code, e.g.,src) - To fix violations automatically:
./check_camel_case.py <directory> --fix
- To check for violations:
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.
Thanks for your work and contribution.
We need to address some issues before integrating this script into difftest.
-
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-formatandscalafmtto 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. -
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. -
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?
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.
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.
Still, thanks very much for your contribution and your research. It's much helpful for the project
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.
Thanks very much for your efforts!