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

[WIP] Apply clang-format to WarpX.H, WarpX.cpp #5739

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

EZoni
Copy link
Member

@EZoni EZoni commented Mar 7, 2025

Following up on #5687, this PR applies the current clang-format style, as is, to two more files, WarpX.H and WarpX.cpp. The code changes were implemented automatically by running pre-commit run -a from the root directory.

This should be enough to showcase most of the style changes that this clang-format configuration (LLVM's default, with few minor adjustments introduced in #5687) produces.

The idea would be for reviewers to briefly go through the changes and raise objections, if any, which we could try to mitigate by adjusting the clang-format configuration further.

Of course it's still possible to object the whole idea of automatic style formatting, too.

@EZoni EZoni added the cleaning Clean code, improve readability label Mar 7, 2025
@EZoni EZoni force-pushed the clangformat_continue branch from b8ae4d6 to dedbed9 Compare March 7, 2025 00:58
Copy link
Member

@lucafedeli88 lucafedeli88 left a comment

Choose a reason for hiding this comment

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

Thanks a lot, @EZoni, for working on this!

I generally like the changes.

My main doubts concern:

  • indentation with pre-processor directives: I think that they are useful for readability!
  • opening curly brace on a new line for classes and functions : I personally like this style since it marks a difference with respect to curly braces after if(), while()...
  • automatic new lines for variables in function declarations: I think that sometimes we use newlines in function declaration to express ideas (e.g., some variables are related) and to improve readability. I am not sure that the proposed style changes are more readable than the original version.

See more detailed comments below.

Comment on lines +33 to +38
#ifdef WARPX_DIM_RZ
#include "BoundaryConditions/PML_RZ_fwd.H"
#include "FieldSolver/SpectralSolver/SpectralSolverRZ_fwd.H"
#else
#include "FieldSolver/SpectralSolver/SpectralSolver_fwd.H"
#endif
Copy link
Member

Choose a reason for hiding this comment

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

I am not completely sure about this style change. It seems to me that indentation was quite useful to make the code more readable here.

Copy link
Member

Choose a reason for hiding this comment

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

I would agree - the indentation makes it more readable.

Copy link
Member Author

@EZoni EZoni Mar 12, 2025

Choose a reason for hiding this comment

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

This might be controlled by the following option:
Screenshot from 2025-03-11 17-28-24

So you both would prefer BeforeHash, correct?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, BeforeHash.

: public amrex::AmrCore
{
public:
class WARPX_EXPORT WarpX : public amrex::AmrCore {
Copy link
Member

Choose a reason for hiding this comment

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

I am not completely sure about this style change.
I personally prefer the style:

void function_name ()
{
}

class MyClass
{
};

for functions and classes, and

if (/*..*/) {
// ...
}

for if, while, for...

Copy link
Member

Choose a reason for hiding this comment

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

I prefer the original. Putting it all on one line makes it harder to read. With the original, I can just glance at the code and see what it is doing. With it all on one line, I have to stare at it a moment to parse it out.


static std::string Version (); //!< Version of WarpX executable
static std::string Version (); //!< Version of WarpX executable
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes we use spaces to align comments and make them more readable. I don't have a strong opinion on this specific change, but we may want to discuss it.

amrex::Real a_full_dt,
int a_nl_iter,
bool a_from_jacobian );
void ImplicitPreRHSOp (amrex::Real cur_time, amrex::Real a_full_dt,
Copy link
Member

Choose a reason for hiding this comment

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

Sometimes we use newlines in function declarations strategically to improve readability, e.g. :

void my_function (
    double Ex, double Ey, double Ez,
    double Bx, double By, double Bz,
    int i, int j, int k,
    bool flag);

I think that we may want to discuss if and how we would like clang-format to change this for us.

Copy link
Member

Choose a reason for hiding this comment

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

I would agree here, that the layout of the arguments should be up to the developer to allow nice grouping.

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Can the formatting of the routine arguments be turned off so that there is no standard? In this case, considering the wide variety of argument lists, there is no single standard that works well in all cases.

@@ -489,22 +577,28 @@ public:
void UpdateInjectionPosition (amrex::Real dt);

void ResetProbDomain (const amrex::RealBox& rb);
void EvolveE ( amrex::Real dt, amrex::Real start_time);
void EvolveE (amrex::Real dt, amrex::Real start_time);
Copy link
Member

Choose a reason for hiding this comment

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

I am not completely sure about this change. I think that the original version was slightly more readable thanks to the extra spaces.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is probably conflicting with #5739 (comment). Meaning, if we like one and don't like the other, or viceversa, there might be no way to make this systematic.

* \param[in] patch_type PatchType on which the field is initialized (fine or coarse)
* \param[in] eb_update_field flag indicating which gridpoints should be modified by this functions
* \param[in] use_eb_flags (default:true) flag indicating if eb points should be excluded or not
* \param[in] patch_type PatchType on which the field is initialized (fine
Copy link
Member

Choose a reason for hiding this comment

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

While I like the idea of stetting a limit for the number of columns used for docstrings, I think that we may need an extra indentation here for the description of the input parameters.

Copy link
Member

Choose a reason for hiding this comment

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

Like:

\param[in] patch_type PatchType on which the field is initialized (fine
                    or coarse)

Copy link
Member Author

@EZoni EZoni Mar 12, 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 if it is possible to customize this in a way that is systematic, beyond wrapping the relevant lines in a "don't format" wrap.

}
namespace {

[[nodiscard]] bool
Copy link
Member

Choose a reason for hiding this comment

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

I know that it is not recommended, but I actually like the indentation inside a namespace. We may want to discuss this.

Copy link
Member

Choose a reason for hiding this comment

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

I would agree that it is better to use indentation inside a name space. This makes it readily clear that the routines defined are in a defined space.

Copy link
Member Author

@EZoni EZoni Mar 12, 2025

Choose a reason for hiding this comment

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

I think it should be possible to guarantee this by replacing LLVM's

NamespaceIndentation: None

with

NamespaceIndentation: All

m_safe_guard_cells,
WarpX::do_multi_J,
WarpX::fft_do_time_averaging,
dt[lev], dx, m_do_subcycling, WarpX::use_fdtd_nci_corr, grid_type,
Copy link
Member

Choose a reason for hiding this comment

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

Here I think that the original version was more readable. The main issue, however, is probably having methods with more than ~10 parameters!

Copy link
Member Author

Choose a reason for hiding this comment

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

Here too I am not sure if there is a way to make this systematic, as one version might look more readable in one place, the other version in another place. I personally like the Ruff linting we use in Python, which enforces one argument per line, except for very short lines, because it makes reviewing code changes easier.

These might be our only options:
Screenshot from 2025-03-11 17-22-20

{
public:
class WARPX_EXPORT WarpX : public amrex::AmrCore {
public:
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it's just what I'm used to, but I prefer no indentation on the public, 'private', `protected' lines.

Copy link
Member Author

Choose a reason for hiding this comment

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

This might be controlled by IndentAccessModifiers?
Screenshot from 2025-03-11 17-12-13


/** Destructor */
~WarpX () override;

/** Copy constructor */
WarpX ( WarpX const &) = delete;
WarpX (WarpX const&) = delete;
Copy link
Member

Choose a reason for hiding this comment

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

I approve of this standard. I've never liked the extra spaces before the arguments (and at the end of the list).

amrex::Vector<std::array< std::unique_ptr<amrex::iMultiFab>,3 > >& GetEBUpdateEFlag() { return m_eb_update_E; }
amrex::Vector<std::array< std::unique_ptr<amrex::iMultiFab>,3 > >& GetEBUpdateBFlag() { return m_eb_update_B; }
amrex::Vector< std::unique_ptr<amrex::iMultiFab> > const & GetEBReduceParticleShapeFlag() const { return m_eb_reduce_particle_shape; }
void SetElectricFieldAndApplyBCs (const WarpXSolverVec& a_E,
Copy link
Member

Choose a reason for hiding this comment

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

Presumably these changes are being made because of the line length limit. I've always preferred longer lines which, to me, makes this type of code easier to read and more compact. What is the maximum line length?

Copy link
Member Author

Choose a reason for hiding this comment

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

The LLVM configuration file sets

ColumnLimit:     80

so it's probably 80 at the moment.

@EZoni
Copy link
Member Author

EZoni commented Mar 12, 2025

@lucafedeli88 @dpgrote

I will try to go through each of your comments and see if there is a corresponding option that can be set to match your suggestions.

However, I'm starting to think that we might need to choose a strategy to move forward.

Should we discuss in the Technical Committee every formatting change that receives an objection? I am afraid that this might not be interesting for some. On the other hand, I or others might not agree with your or others' preferences, in which case a vote would be the only way to resolve, so I'm not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleaning Clean code, improve readability
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants