Skip to content

STYLE: Replace declare-then-initialize with direct initialization#597

Open
hjmjohnson wants to merge 1 commit intomainfrom
itk-declare-then-init
Open

STYLE: Replace declare-then-initialize with direct initialization#597
hjmjohnson wants to merge 1 commit intomainfrom
itk-declare-then-init

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

Replace two-line declare-then-initialize patterns with idiomatic C++ single-line initialization across 39 files, following N-Dekker's methodology from ITK.

Locally verified: full ninja build (841 targets), zero errors, zero new warnings.

Patterns applied (78 fixes total)
Pattern Files Fixes Transformation
fill_zero 23 37 Type v; v.Fill(0)Type v{}
fill_nonzero 15 32 Type v; v.Fill(N)auto v = MakeFilled<Type>(N)
elem_same 4 4 Type v; v[i]=x (all same) → Fill(x)
elem_zero 3 3 Type v; v[i]=0Type v{}
setsize_fill 1 2 v.SetSize(N); v.Fill(V)Type v(N, V)

Patterns intentionally skipped

  • elem_diff (Type v; v[0]=x; v[1]=y;Type v{x,y}): ITK derived types (Vector, Point, ContinuousIndex) don't inherit FixedArray's brace-init constructor; narrowing conversions (intSizeValueType) also block this pattern.
  • assign (Type v; v = expr;Type v = expr;): Script's forward-reference hazard detection is unreliable — several fixes moved variable uses above their declarations.
Reference commits in ITK
  • 6cb6bf9823 — N-Dekker: fill_nonzero pattern
  • 9962e2f971 — N-Dekker: elem_same pattern
  • 03941e42d8 — N-Dekker: elem_diff pattern

Replace two-line declare-then-initialize patterns with idiomatic C++
single-line initialization across 39 files, following N-Dekker's
methodology from ITK commits 6cb6bf9823, 9962e2f971, 03941e42d8.

Patterns applied:
- fill_zero: Type v; v.Fill(0) -> Type v{}
- fill_nonzero: Type v; v.Fill(N) -> auto v = MakeFilled<Type>(N)
- elem_same: Type v; v[i]=x (all same) -> Type::Filled(x) or Fill(x)
- elem_zero: Type v; v[i]=0 -> Type v{}
- setsize_fill: v.SetSize(N); v.Fill(V) -> Type v(N, V)

All changes verified with full ninja build (841 targets, zero errors,
zero new warnings).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@hjmjohnson hjmjohnson force-pushed the itk-declare-then-init branch from a1a7463 to 83621f0 Compare April 13, 2026 15:19
@hjmjohnson hjmjohnson marked this pull request as ready for review April 13, 2026 19:02
VectorType axis{};

axis[0] = 0.0;
axis[1] = 0.0;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove unnecessary resetting to zero.

RadiusType r1;
r1.Fill(1);
auto r1 = itk::MakeFilled<RadiusType>(1);
return r1;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just return itk::MakeFilled<RadiusType>(1)

RotationMatrixType SpacingMatrix;
SpacingMatrix.Fill(0.0);
RotationMatrixType SpacingMatrix{};
SpacingMatrix[0][0] = this->m_Volume->GetSpacing()[0];
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Make a single call to "this->m_Volume->GetSpacing();" and index on temp variable.

itk::ContinuousIndex<double, 3> imageIndex;
imageIndex[0] = 0;
itk::ContinuousIndex<double, 3> imageIndex{};
imageIndex[1] = 0;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove resetting to zero.

ConstNeighborhoodIteratorType::RadiusType radius;
radius[0] = 1;
auto radius = ConstNeighborhoodIteratorType::RadiusType::Filled(1);
radius[1] = 1;
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

remove resetting to 1.

Copy link
Copy Markdown
Member Author

@hjmjohnson hjmjohnson left a comment

Choose a reason for hiding this comment

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

Some recommended cleanups.

@hjmjohnson
Copy link
Copy Markdown
Member Author

hjmjohnson commented Apr 13, 2026

@greptileai

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.

1 participant