Skip to content

Coding Style Guide #3026

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open

Coding Style Guide #3026

wants to merge 11 commits into from

Conversation

soheilshahrouz
Copy link
Contributor

Addresses #2678

@github-actions github-actions bot added the docs Documentation label May 7, 2025
@soheilshahrouz
Copy link
Contributor Author

@amin1377
@AlexandreSinger
@AmirhosseinPoolad
@vaughnbetz

Any comments on what should be added to this guide is appreciated.

@AmirhosseinPoolad
Copy link
Contributor

Fully agree with the usage of auto as documented here. I guess we also should talk about the naming schemes we use. What I personally extrapolated from the code base and remember currently is that Classes are PascalCase, everything else is snake_case, structs are trivial and C-like and start with t_ but it should all be written down.

One more thing is saying that people should prioritize (but not blindly use in all circumstances) using vtr:: containers over STL and use the various LOG/ERROR macros over directly using printf/cout.

Copy link
Contributor

@AlexandreSinger AlexandreSinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Soheil, thank you so much for starting to add documentation on coding style! This is extremely important for such a large project such as VTR.

I think the coding style that we tell the students in ECE297 will be a good reference for a checklist of things to add to this documentation:
tutorial5_code_style.pdf

Specifically this slide:
image

I think the major things that are missing are:

  • The variable, function, and classes naming conventions that we use in VTR
  • Keeping function short (i.e. pulling out code into helper methods)
  • Self-checking code (i.e. proper use of VTR_ASSERT)

I think the three above are very important to document since we can point people to this page if they raise a PR that does not have proper style; since I believe these three things (as well as the things you have documented) would block a PR from being merged in my opinion!

I know our goal is to keep this style guide brief; but if you would like more things to add to the style guide, I can point you to some of the different style guides that I am familiar with!

Copy link
Contributor

@vaughnbetz vaughnbetz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


Comment types and rules:

- Use `/* ... */` **only** for Doxygen documentation comments.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd weaken this a bit / explain the reasoning. Seem a bit strong on the /* vs. //

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a uniform commenting style is helpful. This doesn’t forbid multiline comments. It just means implementation-related comments should start with //, including each line of a multiline comment. This makes it easy to distinguish implementation comments from documentation comments

- Do not explain *what* the code is doing if it's already clear from the code itself.
- Keep comments up to date. Outdated comments are worse than no comments.
- Use Doxygen-style `/** ... */` or `///` for documenting APIs, classes, structs, members, and files.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow the instructions at <link> to add new utilities, APIs or internal functionality doxygen documentation to the html pages at <link>.

Copy link
Contributor Author

@soheilshahrouz soheilshahrouz Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure which page in the docs I should point to. Can you send the link here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first link: https://docs.verilogtorouting.org/en/latest/dev/code_documentation/

and

for second link, could do:

https://docs.verilogtorouting.org/en/latest/api/vpr/
(or just a generic link to the docs, which would end at /latest)

@AlexandreSinger
Copy link
Contributor

@soheilshahrouz We should also add a section on the use of header files. Specifically that "#pragma once" should be the first line in any header file (even before file comments).

Through looking around online, for large projects it is recommended for "#pragma once" to be declared before the file comment as well as code. The idea is that the file comment will be duplicated in files that may include it multiple times, which for large projects can slow down the compilation speed due to IO.

I am currently working on a PR that moves all include files to this style.

@soheilshahrouz soheilshahrouz requested a review from haydar-c May 27, 2025 21:41
@soheilshahrouz soheilshahrouz changed the title [WIP] Coding Style Guide Coding Style Guide Jun 5, 2025
@vaughnbetz
Copy link
Contributor

I'd like to put a discussion of this on the vtr agenda for next week. Can I get you to walk through the style guide then?

@vaughnbetz
Copy link
Contributor

  1. Should move the 'auto' discussion to the end. It's not the most important point.
  2. We should tell them to use the vtr logging utilities, and error functions and have a link to the docs for them.
  3. We should tell them to familiarize themselves with the vtr container utilities (e.g. vectors with type-safe indices, matrices) and use them -- give a link.

@AmirhosseinPoolad
Copy link
Contributor

image
This section's formatting seems to be a little weird with some bold items and some random newlines.

~~~~~~~~~~~~~~~~~~~~~~~~~~~

- Prefer easier-to-read language features and idioms.
- Avoid overusing advanced C++ features (e.g., template metaprogramming, operator overloading, SFINAE) unless they are essential.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My (unsolicited) opinion on this: I agree with the sentiment, but maybe the examples aren't very accurate. Operator overloading has been in C++ since 1998 and should be OK unless people want to + rr graphs together. Also the std::vector<int> example below relies on knowing the compiler would optimize out that copy. It could return a const reference, but if the underlying storage isn't exactly a vector, it makes sense to return a view or range with an auto return type. For instance, Tatum's interfaces are written assuming std::vector and I once gave up trying to make a parallel incremental analyzer based on tbb::concurrent_vector because of this. All in all, I think advanced and new features should be OK as long as they make things simpler.

I think it's good to advise against deep abstract class hierarchies here as it's really hard for tools to jump to the definition of a virtual function (another tatum thing but it may have examples in other places). The combination of templates and virtual functions is particularly hard to navigate. Maybe a piece of code trying to be generic should use one or the other, and not both (so don't do what uxsdcxx-generated code does. or NetlistRouter haha)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants