Abstraction is a basic concept in programming: means isolating code so that the complexity is on a separate layer. You can go deeper into abstraction layers to get to the gritty complexity, or you can go up abstraction levels so that the code is more simple. The abstraction principle is a principle from which most other principles stem. https://en.wikipedia.org/wiki/Abstraction_principle_(computer_programming)
Encapsulation may seem similar to abstraction but it's not the same: encapsulation means isolating code so that some of it is out of reach, only used internally. Examples of this are object-oriented programming or libraries that expose APIs. https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
Separation of concerns (SoC) is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program. – https://en.wikipedia.org/wiki/Separation_of_concerns
In web development:
- HTML's concern is content, semantics (html tags) and hierarchy of information.
- CSS's concern is style and presentation.
- JS's concern is behavior.
From this principle many good practices derive. Just to name a few:
- Don't use
style
prop in HTML. - Don't put image bullet points with the
<img>
element in HTML. Use:before
pseudo-element in CSS. - Don't use CSS classes in HTML with presentational information (
right-button
) and instead use a semantic concept (action-button
). - Don't render HTML inside of JS. Ditto with CSS. (Of course all this goes off the roof with React.js, but the basic principle still stands)
This principle does not apply only to web development, but to all software development and probably other engineerings and things in life.
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system. – Andrew Hunt and David Thomas in "The Pragmatic Programmer".
Replace repetitive code with abstractions or using data normalization to avoid redundancy and to avoid having to change things in different places.
Don't add code "just in case" you need it in the future. Keep it simple. Less code is less cognitive load. Less code is less bugs.
Related blog post: https://blog.codinghorror.com/kiss-and-yagni/