Skip to content

Conversation

@hjmjohnson
Copy link
Member

Replace in Files, doing:
Find what: ^( [ ]+)([^ ].*)[ ]+(\w+);[\r\n]+\1\3.Fill(
Replace with: $1auto $3 = MakeFilled<$2>(

Simplify the creation of containers with initial
values.

PR Checklist

@hjmjohnson hjmjohnson added the type:Style Style changes: no logic impact (indentation, comments, naming) label Dec 8, 2024
@hjmjohnson hjmjohnson added this to the ITK 6.0 Beta 1 milestone Dec 8, 2024
@hjmjohnson hjmjohnson self-assigned this Dec 8, 2024
@hjmjohnson hjmjohnson force-pushed the replace-var.Fill-MakeFilled branch from 581dcca to c26c6f3 Compare December 9, 2024 00:17
@hjmjohnson hjmjohnson marked this pull request as draft December 9, 2024 00:21
@hjmjohnson
Copy link
Member Author

Builds upon #5023

@N-Dekker
Copy link
Contributor

N-Dekker commented Dec 9, 2024

Thanks Hans! Sorry, I overlooked the Examples when doing similar code clean-ups on ITK! I'm glad to see you do this follow-up. 👍

As I remarked in-line, I would prefer {} (rather than MakeFilled) for zero-initialization. As in the following pull request:

So I would suggest running the regex of pull request #4897 before adding the MakeFilled calls.

@N-Dekker
Copy link
Contributor

N-Dekker commented Dec 9, 2024

In general, I would suggest:

  1. To zero-fill a variable, use {}
  2. To fill a variable with arbitrary (non-zero) fill-value
    a. do auto var = T::Filled(fillValue) when the type is itk::Size<N> or itk::Index<N>
    b. do auto var = itk::MakeFilled<T>(fillValue) otherwise

As I wrote at #4887 (comment)

@hjmjohnson hjmjohnson force-pushed the replace-var.Fill-MakeFilled branch 2 times, most recently from 3e6e8ac to 32192c5 Compare December 9, 2024 15:34
Another run to replaced code of the form

    T var;
    var.Fill(0);

with `T var{};`

Using Notepad++, Replace in Files, doing:

    Find what: ^( [ ]+[^ ].* )(\w+);[\r\n]+ [ ]+\2\.Fill\(0\);
    Replace with: $1$2{};
    Filters: itk*Test*.cxx
    [v] Match case
    (*) Regular expression

The empty initializer list `{}` effectively zero-fills those variables.
Prefer Filled over MakeFilled for initializing Size<T> and Index<T> types

Replace in Files, doing:
Find what: ^( [ ]+)(SizeType)[ ]+(\w+);[\r\n]+\1\3\.Fill\(
Replace with: $1auto $3 = $2::Filled\(
This involved moving variable initialization to
the smallest possible scope.

The more locally scoped variables allow for future
automated code refactorings.
Identified code style improvements based on manually
reviewing use of .Fill for assignment of values
when initialization could have been used.
@hjmjohnson hjmjohnson force-pushed the replace-var.Fill-MakeFilled branch from 32192c5 to 6746ae4 Compare December 9, 2024 17:08
@hjmjohnson hjmjohnson marked this pull request as ready for review December 9, 2024 17:08
@github-actions github-actions bot added area:Examples Demonstration of the use of classes type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Core Issues affecting the Core module area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Segmentation Issues affecting the Segmentation module labels Dec 9, 2024
@hjmjohnson hjmjohnson merged commit 9bc3ea0 into InsightSoftwareConsortium:master Dec 10, 2024
16 of 17 checks passed
@hjmjohnson hjmjohnson deleted the replace-var.Fill-MakeFilled branch December 10, 2024 02:12
@jhlegarreta
Copy link
Member

Re: #5024 (comment) @N-Dekker Can this be put into the SWG and an ITK Example be added to the SphinxExamples repository?

@N-Dekker
Copy link
Contributor

@jhlegarreta Thanks, I can have a look at how to clearly put that guideline from #5024 (comment) into the SWG:

1. To zero-fill a variable, use `{}`

2. To fill a variable with arbitrary (non-zero) fill-value
   a. do  `auto var = T::Filled(fillValue)` when the type is `itk::Size<N>` or `itk::Index<N>`
   b. do `auto var = itk::MakeFilled<T>(fillValue)` otherwise

By the way, the most important guideline should of course just be: initialize, initialize, initialize, as soon as possible! The choice between {}, T::Filled(fillValue) and itk::MakeFilled<T>(fillValue) is more stylish. But I fully agree that it is worth mentioning in the SWG.

@jhlegarreta
Copy link
Member

@seanm
Copy link
Contributor

seanm commented Dec 12, 2024

I just tracked this down, but I see @jhlegarreta beat me to it. :)

@hjmjohnson you able to fix?

@dzenanz
Copy link
Member

dzenanz commented Dec 12, 2024

I made a PR for it: #5053.

N-Dekker added a commit to N-Dekker/ITKSoftwareGuide that referenced this pull request Dec 17, 2024
N-Dekker added a commit to N-Dekker/ITKSoftwareGuide that referenced this pull request Dec 17, 2024
@hjmjohnson hjmjohnson restored the replace-var.Fill-MakeFilled branch February 4, 2025 23:21
@hjmjohnson hjmjohnson deleted the replace-var.Fill-MakeFilled branch June 24, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module area:Examples Demonstration of the use of classes area:Filtering Issues affecting the Filtering module area:IO Issues affecting the IO module area:Segmentation Issues affecting the Segmentation module type:Style Style changes: no logic impact (indentation, comments, naming) type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants