These three concepts are very important to keep in mind as well as good practices to have when it comes to software design. In programming we should be always trying to increase the level of all these design principles when writing code.
Abstraction
Abstraction is the opposite of concrete. So trying to increase the abstraction of your code means trying to keep it as generic as possible in terms of complexity and readability. Abstracting code can make it easier to read and understand, as it can help to eliminate unnecessary details and focus on the core functionality.
Below is an example of a not very abstract code vs a more abstract code. Let's say you want to calculate the area of a rectangle.
# not very abstract code
length = 10
width = 5
area = length * width
print(area)
# more abstract code
def calculate_area(length, width):
area = length * width
return area
print(calculate_area(10, 5))
So keeping the code abstract helps to make it more modular, readable, maintainable, reusable and flexible.
Reusability
Reusability refers to the ability to reuse existing code in different contexts, rather than writing new code every time a similar task needs to be performed. Reusable code saves time and effort (which in turn increases productivity in most cases), facilitates collaboration once you're not the only one that can reuse the code, and avoids code repetition.
As an example we can think in a visual component such as a React component that will be used in many different pages or components. The idea is to make this component just once and reuse it wherever is needed, right? So keeping the code reusable also helps to make it more modular and maintainable since if a change is requested you can change it in just one place, in one piece that will reflect in the other pieces that are being reused.
Flexibility
The idea here is trying to go against practices that make your code highly coupled and trying to make good use of the previously mentioned practices above, because they are also responsible for increasing the code's flexibility. This one allows us to make changes to software without having to completely rewrite it from scratch, which saves time and effort especially when there are new user needs or new business requirements. It's an important aspect of programming that can help ensure that software remains adaptable, scalable, integrable, maintainable, and user-friendly over time.
Good practices to increase the flexibility of your code: avoiding hard-coded code (which means using variables and constants), using functions to help modularize the code, increasing the cohesion, increasing the abstraction and the reusability as mentioned above.
Conclusion
In software design, abstraction, reusability, and flexibility are closely related concepts that are essential for building high-quality, scalable, adaptable and maintainable software.
Top comments (3)
Sobre a abstração do código, você se refere não apenas, mas a construtores e "ligamento" de classes ? ou algo que possa ser mais reutilizável ?
Afinal a reutilização é aplicada de forma agregável á própria abstração ?
Gostei de mais de seu post!
Obrigada pelo comentário, @joaoarruda101 :)
Construtores e ligamento de classes podem ser parte do processo de abstração, uma vez que podem ajudar a encapsular e abstrair detalhes de implementação do código. No entanto, a abstração também se refere a conceitos de mais alto nível, como por exemplo a criação de componentes modulares e reutilizáveis, ou até mesmo o design de interfaces que abstraem detalhes de sua implementação. Ou seja, a abstração pode estar presente em todos os níveis.
E sim, acho que podemos dizer que a reutilização é um aspecto fundamental da própria abstração, já que aumentar o nível de abstração do código significa torná-lo mais modular, mais reutilizável, mais flexível.
Espero ter ajudado.
Tentei desenvolver um exemplo deste conceito em Java utilizando matrize
Uso o for também para deixar tudo mais flexível