DEV Community

Jessica Alves
Jessica Alves

Posted on

Abstraction, reusability and flexibility in software design

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))
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
joaoarruda101 profile image
João Victor F. Arruda

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!

Collapse
 
alvesjessica profile image
Jessica Alves

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.

Collapse
 
joaoarruda101 profile image
João Victor F. Arruda • Edited

Tentei desenvolver um exemplo deste conceito em Java utilizando matrize
Uso o for também para deixar tudo mais flexível

import java.util.Scanner;

public class matrizes {
    public static void main(String[] args) {

        Scanner read = new Scanner(System.in);
        System.out.println("Coloque o número de Linha = x e Colunas = y da Matriz \n");

        String[] option;
        option = new String[4];
        menu(option);
        System.out.print("R:");
        String esc = read.nextLine();

    }

    public void _Matriz01(int linhas, int colunas) {
        int[][] matriz = new int[linhas][colunas];
        for (int i = 0; i < linhas; i++) {
            for (int j = 0; j < colunas; i++) {
                System.out.print(matriz[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void menu(String[] option) {
        for (int i = 0; i < option.length; i++) {
            option[0] = "[" + i + "] - Sair \n";
            option[1] = "[" + i + "] - Adicionar paramêtro da matriz \n";
            option[2] = "[" + i + "] - Adicionar indeces no banco de dados \n";
            option[3] = "[" + i + "] - Procurar dados em matriz \n";
            System.out.print(option[(i)]);
        }
    }

    public static void insertParametersMatriz(String linhas, String colunas) {
        Scanner read = new Scanner(System.in);
        System.out.println("Coloque o número de Linha = x e Colunas = y da Matriz \n");

        System.out.print("X:");
        linhas = read.nextLine();
        System.out.print("Y:");
        System.out.print("Y:");

        colunas = read.nextLine();

        int[][] matriz = new int[Integer.parseInt(linhas)][Integer.parseInt(colunas)];

        for (int i = 0; i < Integer.parseInt(linhas); i++) {
            for (int j = 0; j < Integer.parseInt(colunas); j++) {
                System.out.print(matriz[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode