How to prevent z-fighting in OpenSCAD? Z-fighting is a 3D rendering artifact of coplanar surfaces (means, triangles are located in exactly the same plane, and overlap).

It can happen in OpenSCAD's preview mode when doing a difference() or union() operation. In case of difference(), the rendering artifacts can prevent seeing into a hole in the object. The "compile and render" mode in OpenSCAD does not have z-fighting issues. But since it can take some time to render an object in that mode, it is not a practical solution during development.

How best to avoid z-fighting?

If the answer involves changes to my OpenSCAD code, I would love to see an idiomatic answer / established convention of OpenSCAD coders, if that exists.
The general advice in the OpenSCAD community is to "extend your cuts and embed your joins" (source). The rendering artifacts are one thing but rather just an annoyance; however z-fighting can also cause unexpected errors during STL export (I did not experience that myself so far, just read this somewhere).

So you would change the dimensions of your objects very slightly (0.01 mm works fine) so that:


for a union(), there is overlap volume between the parts 
for a difference(), the intersector has volume both inside and outside of the intersected part


Now you could adjust both the size and position of your parts to keep the mathematically exact dimensions for the resulting part. But I found that for the purposes of 3D printing, such accuracy is not worth it because it complicates the formulas so much.

Instead, I adjust either position or size of a part, depending on what is simpler in each case. A measure in the final design will be off by 0.01 mm, which does not matter.

And I keep the 0.01 mm offset in a variable called nothing (picked that up somewhere and liked it …). This keeps the calculations intuitively understandable.

Example

To create a cylinder and cut a hole to half of its depth, I would do this:

//!OpenSCAD
nothing=0.01;
height=40;

difference(){
    cylinder(h=height, r=20, center=true);

    translate([0, 0, height/4 + nothing]) 
        cylinder(h=height/2, r=15, center=true);
}


Now the hole is nothing=0.01 less deep than half of the cylinder – that's the inaccuracy I accept.

(Note: If you don't have OpenSCAD installed, you can also try the above code online by copy &amp; pasting it into OpenJSCAD. Include the magic comment in the first line to switch it to OpenSCAD syntax.)