# SimpleType m\_Var = initialValue;

**URL:** <https://discourse.itk.org/t/simpletype-m-var-initialvalue/651>\
**Category:** Engineering\
**Created:** [February 7, 2018, 2:15pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651 "2018-02-07T14:15:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [February 7, 2018, 2:15pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/1 "2018-02-07T14:15:22Z")

</div>

Should we move away from the practice that the member variables are initialized in the constructor, and rather move them to the declaration? At least for simple types. A “go to declaration” would also inform the programmer about member’s default value, not just its type and containing class.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [December 15, 2018, 11:05am UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/2 "2018-12-15T11:05:17Z")

</div>

> [@dzenanz](#):
>
> Should we move away from the practice that the member variables are initialized in the constructor, and rather move them to the declaration?

Interesting. 🙂 When initializing data members of a class this way (using C++11 [in-class member initializers](https://isocpp.org/wiki/faq/cpp11-language-classes#member-init)), it appears that their initial values are included automatically with the Doxygen generated documentation of the class! So they are self-documented!

So apparently, _in-class member initialization_ reduces the chance of having mismatches between documentation and code (like this issue: [Mismatch documentation and code SyNImageRegistrationMethod GaussianSmoothingVarianceForTheUpdateField default value](https://github.com/InsightSoftwareConsortium/ITK/issues/273)).

---

<div class="post-metadata">

**Author:** ![jhlegarreta](https://discourse.itk.org/user_avatar/discourse.itk.org/jhlegarreta/32/476_2.png) [@jhlegarreta](https://discourse.itk.org/u/jhlegarreta)\
**Post date:** [December 15, 2018, 10:08pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/3 "2018-12-15T22:08:50Z")

</div>

From the outlined reasons, this looks a nice feature to me, if there are no downsides at least.

I guess not every data type may be initialized this way, but in any case, looks reasonable to consider it.

Also, and since this has arised a few times lately, if this somehow impacts performance _vs_ using initialization lists due to some reason that I ignore right now, then we may need to be more cautious.

Finally, I guess the script using some regex to spot the initializations/assignment of member variables and move them to the declaration would not be that easy to develop. But may both spot uninitialized variables (much like using the `EXERCISE_BASIC_OBJECT_METHODS` macro does in tests), as well as miss/identify others due to incorrect naming. So definitely a huge effort. But it may be a worthwhile one.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [December 16, 2018, 12:18pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/4 "2018-12-16T12:18:09Z")

</div>

Thanks for your encouraging reply, Jon.

> [@jhlegarreta](#):
>
> Finally, I guess the script using some regex to spot the initializations/assignment of member variables and move them to the declaration would not be that easy to develop

Maybe _clang-tidy_ could do the job for us 😀  
[https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-default-member-init.html](https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-default-member-init.html)

Unfortunately I don’t have clang properly installed yet, on my Windows pc, and I’m not so familiar with clang anyway.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 17, 2018, 3:46pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/5 "2018-12-17T15:46:10Z")

</div>

This might be a job for our resident clang-tidy master @hjmjohnson! Like he doesn’t have enough to do already 😃

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [December 17, 2018, 4:05pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/6 "2018-12-17T16:05:16Z")

</div>

> [@dzenanz](#):
>
> This might be a job for our resident clang-tidy master @hjmjohnson!

Indeed… I believe Hans can do it 😁

In the mean time I did have another look. It appears that _by default_, the clang-tidy option _modernize-use-default-member-init_ uses curly braces:

```
SimpleType m_Var{initialValue};

```

@dzenanz Is that OK to you as well? Otherwise the _UseAssignment_ option has to be enabled.

Still I haven’t got clang-tidy-fix working properly here, especially passing this _UseAssignment_ option seems non-obvious to me. So it would be great if Hans could do it. 🙂

---

<div class="post-metadata">

**Author:** ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)\
**Post date:** [December 17, 2018, 4:07pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/7 "2018-12-17T16:07:20Z")

</div>

Do curly braces appear in Doxygen as default values too? Otherwise I don’t have a preference.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [December 17, 2018, 4:19pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/8 "2018-12-17T16:19:21Z")

</div>

> [@dzenanz](#):
>
> Do curly braces appear in Doxygen as default values too? Otherwise I don’t have a preference.

Yes they do 😃 I just tried my local Doxygen installation (1.8.14), and it does display the initial values of the data members, in the generated HTML.

In general, curly braced initialization has more compile-time safety checking than the “assignment style” syntax. So maybe we should go for curly braces anyway.

---

<div class="post-metadata">

**Author:** ![hjmjohnson](https://discourse.itk.org/user_avatar/discourse.itk.org/hjmjohnson/32/23_2.png) [@hjmjohnson](https://discourse.itk.org/u/hjmjohnson)\
**Post date:** [December 17, 2018, 5:32pm UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/9 "2018-12-17T17:32:32Z")

</div>

> [@dzenanz](#):
>
> t least for simple types. A “go to declaration” would also inform the programmer about member’s default value, not just its type and containing class.

Challenge accepted. Initial conversion is underway. PR to arrive shortly.

---

<div class="post-metadata">

**Author:** ![Niels\_Dekker](https://discourse.itk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.itk.org/u/Niels_Dekker)\
**Post date:** [December 18, 2018, 9:24am UTC](https://discourse.itk.org/t/simpletype-m-var-initialvalue/651/10 "2018-12-18T09:24:29Z")

</div>

Great job, Hans! Thanks a lot!

> <https://github.com/InsightSoftwareConsortium/ITK/commit/b21dbbb2938911206d9259d3777b163d257031cd>

P.S. It appears that the commit added a few unnecessary empty lines to constructor implementations: [https://github.com/InsightSoftwareConsortium/ITK/pull/324/commits/a9a501e6cd7186671d5c47ab06ddc07d2cc126c7](https://github.com/InsightSoftwareConsortium/ITK/pull/324/commits/a9a501e6cd7186671d5c47ab06ddc07d2cc126c7)

Doesn’t matter much, of course. But could this be a clang-tidy bug? 🙃
