# Refactoring code with \`cpplint\`

**URL:** <https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979>\
**Category:** Developers\
**Tags:** specification\
**Created:** [May 8, 2020, 11:17pm UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979 "2020-05-08T23:17:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rgoswami/32/7555_2.png) [@rgoswami](https://discourse.mc-stan.org/u/rgoswami)\
**Post date:** [May 8, 2020, 11:17pm UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/1 "2020-05-08T23:17:45Z")

</div>

Hi!  
With the [new `cpplint`](https://github.com/stan-dev/math/pull/1871), there are some additional defaults, which are currently disabled but I would like to enforce, namely:

- `build/include_order` 3301
- `build/header_guard` 2805
- `build/namespaces` 3
- `runtime/string` 36
- `build/include_what_you_use` 23

These essentially end causing changes akin to the following

```cpp
#ifndef STAN_MATH_REV_FUN_VARIANCE_HPP -> #ifndef
STAN_MATH_REV_FUN_VARIANCE_HPP_
#endif -> #endif // STAN_MATH_REV_FUN_VARIANCE_HPP_
For a static/global string constant, use a C style string instead: "const char MAJOR_VERSION[]". [4]
stan/math/rev/functor/cvodes_ode_data.hpp(195): error cpplint:[build/include_what_you_use] Add #include <utility> for move [4]

```

Since enabling these for the `math` library will involve making changes in the `stan` repo, and also will cause much breakage for anyone working with a fork right now, I was hoping to get some opinions about this change.

---

<div class="post-metadata">

**Author:** ![stevebronder](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stevebronder/32/17242_2.png) [@stevebronder](https://discourse.mc-stan.org/u/stevebronder)\
**Post date:** [May 9, 2020, 12:35am UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/2 "2020-05-09T00:35:09Z")

</div>

Where are these documented at?

> [@rgoswami](#):
>
> Since enabling these for the `math` library will involve making changes in the `stan` repo, and also will cause much breakage for anyone working with a fork right now, I was hoping to get some opinions about this change.

Why would changing things at the stan level cause breaking changes in the math repo?

---

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rgoswami/32/7555_2.png) [@rgoswami](https://discourse.mc-stan.org/u/rgoswami)\
**Post date:** [May 9, 2020, 12:55am UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/3 "2020-05-09T00:55:49Z")

</div>

Well the linter runs on every C++ file in both repositories, so they have to be changed in tandem. The exact options are documented only via the `help` string, and pretty vaguely, so I’ll link to sections of the [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html) which `cpplint` enforces.

- `build/header_guard` -\> [The #define Guard](https://google.github.io/styleguide/cppguide.html#The__define_Guard)
- `build/include_order` -\> [Names and Order of Includes](https://google.github.io/styleguide/cppguide.html#Names_and_Order_of_Includes)
- `build/namespaces` -\> [Namespaces](https://google.github.io/styleguide/cppguide.html#Namespaces)
- `runtime/string` -\> Part of [Local Variables](https://google.github.io/styleguide/cppguide.html#Local_Variables)
- `build/include_what_you_use` -\> Not clearly documented, but it makes sense to (reduces the imports and ensures headers have imports which are used)

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [May 18, 2020, 12:55am UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/4 "2020-05-18T00:55:40Z")

</div>

> [@rgoswami](#):
>
> Not clearly documented, but it makes sense to (reduces the imports and ensures headers have imports which are used)

Are our header tests not catching that everything’s included?

I’m not sure we want to change every one of our header guards just because a style linter decided they should end in “\_” this release. We used to have the include order thing that required standard libs last. Is that sitll the include order issue?

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [May 18, 2020, 7:00am UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/5 "2020-05-18T07:00:05Z")

</div>

Sorry for missing this thread. Thanks for listing all these.

> [@rgoswami](#):
>
> - `build/header_guard` → [The #define Guard](https://google.github.io/styleguide/cppguide.html#The__define_Guard)

I like the comment on the #endif ( #endif // FOO\_BAR\_BAZ\_H\_), but I am with Bob on the trailing underscore. Is there a way to only use one of those? Probably not, right?

> [@rgoswami](#):
>
> - `build/namespaces` → [Namespaces](https://google.github.io/styleguide/cppguide.html#Namespaces)

Does this change anything in our code base? Reading through it quickly, it seems to me that we are following that. Does the 3 next to it indicate that we are missing only three cases? If that is the case I think this one should be added.

> [@rgoswami](#):
>
> - `build/include_what_you_use` 23

We did use the include what you use in the previous cpplint version. Was something added to the rules for the new version that is causing the 23 fails?

> [@rgoswami](#):
>
> - `runtime/string` 36

These are hopefully happening in the test/ folder? Or do we have some global strings floating around? We do have some global string constants in `test/` and I am fine with fixing those (though we should also rethink if we can get rid of the global string).

Bottom line, I think we can safely do the following in one PR

- `build/namespaces` 3
- `runtime/string` 36
- `build/include_what_you_use` 23

I am not sure on the include order, but that is definitely a separate PR with the number of fixes required. But we need to decide on whether we want to do it or not.

---

<div class="post-metadata">

**Author:** ![rgoswami](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/rgoswami/32/7555_2.png) [@rgoswami](https://discourse.mc-stan.org/u/rgoswami)\
**Post date:** [May 19, 2020, 3:57pm UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/6 "2020-05-19T15:57:12Z")

</div>

Thanks for the detailed inputs, I’ll try to address them individually.

> I like the comment on the #endif ( #endif // FOO\_BAR\_BAZ\_H\_), but I am with Bob on the trailing underscore. Is there a way to only use one of those? Probably not, right?

We can actually modify the linter (it is very simple `python`) to do that, but in that case we will just have to keep that in mind (documented) so the change is propagated when updating the versions.

I think I can open a PR with

- `build/namespaces` 3
- `runtime/string` 36
- `build/include_what_you_use` 23

in a bit, and add to the discussion on the other rules then.

---

<div class="post-metadata">

**Author:** ![rok\_cesnovar](https://avatars.discourse-cdn.com/v4/letter/r/7bcc69/32.png) [@rok\_cesnovar](https://discourse.mc-stan.org/u/rok_cesnovar)\
**Post date:** [May 19, 2020, 4:36pm UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/7 "2020-05-19T16:36:33Z")

</div>

> [@rgoswami](#):
>
> I think I can open a PR with

That would be fantastic!

> [@rgoswami](#):
>
> We can actually modify the linter (it is very simple `python` ) to do that, but in that case we will just have to keep that in mind (documented) so the change is propagated when updating the versions.

I am not sure we want to go that deep. If it was something really really important we might. But the comment on the #endif is, at least in my opinion, not worth the effort and extra maintainance.

---

<div class="post-metadata">

**Author:** ![Bob\_Carpenter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bob_carpenter/32/9230_2.png) [@Bob\_Carpenter](https://discourse.mc-stan.org/u/Bob_Carpenter)\
**Post date:** [June 5, 2020, 10:49pm UTC](https://discourse.mc-stan.org/t/refactoring-code-with-cpplint/14979/8 "2020-06-05T22:49:08Z")

</div>

I’m OK with comments on endifs. I don’t really feel they’re necessary since they’re at the end of files and thus always match the header guard. But I can see how it might avoid some mistakes in coding if there were internal ifdefs beyond the outer one.
