- not reuse copyrightable content when you can easily avoid it, but instead express your ideas in your own words, and with your own pictures and code;
- when you do want to reuse content created by someone else, ask their permission if possible;
- when basing your work on freely licensed content such as SO posts, try to follow the spirit of the license as far as possible, even if you cannot technically follow it to the letter; and
- in any case, always give credit where it's due, both for (potentially copyrightable) content and for (non-copyrightable) ideas and inspirationinspiration; and
- generally try to be nice to people whose knowledge, creativity and hard work you're building on.
Of course, following this advice won't prevent someone from suing you for copyright violation (although it might in some cases reduce their chances of prevailing, and/or the damages they can successfully claim if they do). But it makes it less likely that they'll want to.