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.
To create a cylinder and cut a hole to half of its depth, I would do this:
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 & pasting it into OpenJSCAD. Include the magic comment in the first line to switch it to OpenSCAD syntax.)