# Improve const correctness of vtkAbstractDataArray, vtkPoints and derived classes

**URL:** https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404
**Category:** Development
**Tags:** proposal
**Created:** [March 5, 2019, 1:30pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404 "2019-03-05T13:30:45Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [March 5, 2019, 1:30pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/1 "2019-03-05T13:30:45Z")

</div>

vtkPoints::GetNumberOfPoints(), vtkAbstractDataArray::GetNumberOfTuples are not constant.

> <https://github.com/Kitware/VTK/blob/a7909e67d7df88dcf81984923296442c7ccaca5b/Common/Core/vtkPoints.h#L123-L126>

  

> <https://github.com/Kitware/VTK/blob/a7909e67d7df88dcf81984923296442c7ccaca5b/Common/Core/vtkAbstractArray.h#L171-L173>

This impedes to be const correct,

```cpp
 void ReadPoints(const vtkPoints *read_only_points)
{
  auto npoints = read_only_points->GetNumberOfPoints(); // Compile error, GetNumberOfPoints() is not constant.
}

```

Appending const to the functions will do.

```cpp
vtkIdType GetNumberOfTuples() const;
vtkIdType GetNumberOfPoints() const;

```

I guess it will be a little painful to update quite a few functions, but const correctness is important, specially when reasoning about code and in multi-thread environments.

Not sure if this has been already discussed, what do you think?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 5, 2019, 1:55pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/2 "2019-03-05T13:55:15Z")

</div>

My experience is that const correctness is often an all or nothing decision. If you start adding const keywords then things will break until you make sweeping changes in the entire code base and also in external code that use it.

Const correctness helps with documentation and avoiding some bugs, so it is good (I would not say it is important), but introducing it would be a huge change, which has to be planned carefully. Also note that any change that forces application developers to modify their code is extremely frustrating if there is no proportional gain (significant new features, better performance, etc.).

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [March 5, 2019, 2:15pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/3 "2019-03-05T14:15:29Z")

</div>

I see the pain @lassoan, thanks for your input.

However, in this case, just adding a const and a non-const version will do, no breaking changes. And to avoid duplications:

```cpp
vtkIdType GetNumberOfPoints() const
{
  return this->Data->GetNumberOfTuples();
}

vtkIdType GetNumberOfPoints()
{
  return static_cast<const vtkPoints &>(*this).GetNumberOfPoints();
}

```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 5, 2019, 2:28pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/4 "2019-03-05T14:28:54Z")

</div>

If there are no breaking changes then it’s fine.

GetNumberOfPoints may be called at many places, very frequently, so I would not risk any performance impact by adding one more level of indirection. The non-duplicating code is longer and harder to read. So, I would leave the current implementation of the non-const GetNumberOfPoints as is.

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [March 5, 2019, 6:18pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/5 "2019-03-05T18:18:02Z")

</div>

> I see the pain @lassoan, thanks for your input.
> 
> However, in this case, just adding a const and a non-const version will do, no breaking changes. And to avoid duplications:
> 
> vtkIdType GetNumberOfPoints() const  
> {  
> return this-\>Data-\>GetNumberOfTuples();  
> }
> 
> vtkIdType GetNumberOfPoints()  
> {  
> return static\_cast\<const vtkPoints &\>(\*this).GetNumberOfPoints());  
> }

+1

Several times I’ve been disappointed when I wanted to make some  
function in our code const-correct, but could not since it calls some  
VTK function on a member, and VTK is not const-correct. I understand  
of course that a sweeping refactor of VTK would create a lot of pain,  
but I would welcome baby steps like these where feasible. I think in  
library code one should always strive to be const-correct, because it  
leaves the library user a choice, whereas “everything non-const” does  
not.

Elvis

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 5, 2019, 8:49pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/6 "2019-03-05T20:49:07Z")

</div>

If I had to choose what VTK developers should spend their time with, const correctness would be pretty low on the priority list compared to improving performance or adding features (oriented image data, robust polydata Boolean operations, GPU-accelerated filters, etc.).

---

<div class="post-metadata">

### Author: ![estan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/estan/32/460_2.png) [@estan](https://discourse.vtk.org/u/estan)
#### Post date: [March 5, 2019, 8:56pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/7 "2019-03-05T20:56:41Z")

</div>

Yes absolutely, not disputing that! It’s just a little sad that const was not kept in mind from the get go.

---

<div class="post-metadata">

### Author: ![seanm](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@seanm](https://discourse.vtk.org/u/seanm)
#### Post date: [March 6, 2019, 1:42am UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/8 "2019-03-06T01:42:11Z")

</div>

Pablo,

I occasionally work on improving VTK const correctness, including some of the API you mentioned.

See:  
[https://gitlab.kitware.com/vtk/vtk/merge\_requests/1455/diffs?commit\_id=3127c6e1388cc3a8ae1244b7a799b17b7ba7cb1e](https://gitlab.kitware.com/vtk/vtk/merge_requests/1455/diffs?commit_id=3127c6e1388cc3a8ae1244b7a799b17b7ba7cb1e)

If you could do a review of that MR, it could be helpful.

Sean

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/phcerdan/32/54_2.png) [@phcerdan](https://discourse.vtk.org/u/phcerdan)
#### Post date: [March 6, 2019, 1:33pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/9 "2019-03-06T13:33:50Z")

</div>

Hi @seanm, that’s great!!

Heading there asap, thanks

---

<div class="post-metadata">

### Author: ![Niels\_Dekker](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/9d8465/32.png) [@Niels\_Dekker](https://discourse.vtk.org/u/Niels_Dekker)
#### Post date: [March 13, 2019, 3:45pm UTC](https://discourse.vtk.org/t/improve-const-correctness-of-vtkabstractdataarray-vtkpoints-and-derived-classes/404/10 "2019-03-13T15:45:49Z")

</div>

Somewhat related: Last week, I submitted a merge request to improve const correctness of `vtkTriangle` member function parameters:

_“Add const to vtkTriangle function parameters”_  
[https://gitlab.kitware.com/vtk/vtk/merge\_requests/5278](https://gitlab.kitware.com/vtk/vtk/merge_requests/5278)

Please have a look! Is there anything more I could do to get it merged?

_Update (15 March 2019):_ @ken-martin just did the merge: [https://gitlab.kitware.com/vtk/vtk/commit/08b3fd3e49d040db640b785b6261ef7985d052ef](https://gitlab.kitware.com/vtk/vtk/commit/08b3fd3e49d040db640b785b6261ef7985d052ef)🙂
