How do I know my function is breaking SOLID POO?

Asked

Viewed 90 times

6

I left a function below that will be the subject of the question, basically I would like to know if this function breaks any principle of SOLID/Clean Code by itself ? This doubt arose because technically she does 2 things that would fetch a balance in the database and validate the return.

    public buscarSaldo = async (): Promise<boolean> => {
    const requerentes = await this.db.getAccount(this.agencia, this.bco, this.conta)
    if(typeof requerentes == "boolean")
    {
        return false;
    }else{
        this.saldo = requerentes[0].SALDO
        return true;   
    }
}

I know it sounds silly, but how do I know if my function is breaking some principle that makes it "ugly" code? There’s some trick to it ?

1 answer

7


Studying a lot, but a lot. This is the "trick". These things are taken as cake recipes by many and just follow that everything will be fine, but it’s not like that. People do it wrong all the time because they don’t go deeper, they don’t understand the real motivations, so often even adopt the SOLID may already be a mistake. Or adopting OOP may also be a mistake.

It’s common for people to even understand what these things actually mean, why they’re using all the concepts behind it. They use it because it’s fashionable. No technical reason. Of course, these tools can be useful in certain scenarios, but it’s only when one dominates what one is doing, which requires a lot of dedication and a lot of experience because only error actually teaches how to make good architectural and engineering decisions (I am not disregarding the deep study, as I said before).

I have 30 years of object-oriented programming, I study the subject a lot and I still make mistakes in concrete cases. Why does someone who is starting now think they can only see the subject over and repeat the same thing over and over again and still get it right? Things of the human being...

If you’re asking if a function breaks SOLID, my recommendation is to see what it’s all about, because a function doesn’t indicate much about it. What’s more, without knowing the requirements, there’s no way to do that analysis. It seems to me that you are so concerned with the particular technique that you are forgetting the foundation, which is what really matters. Even because co9m he can make his own decisions and become a professional with a capital P.

You give the idea that you want to know if you’re violating SOLID’s S. I can’t say without knowing the details of what you’re doing. It seems to me you’re using things you don’t need there, maybe because it’s cool do so (I doubt it is out of necessity, but I may be wrong, as I said, I don’t have the details). I would do it in a very different way, and it seems to violate the principle of sole responsibility.

Try this by separating the search from the validation. See if it looks better. I doubt, then you will see that maybe SOLID tells you to do things that make your code worse and it is better to know that it exists, to master it to apply when it is useful, but to worry more about making code better that solves the problem in the best way, without worrying if it is SOLID, OOP, or something else.

I wouldn’t even try to apply something I don’t completely master, because otherwise I’ll do it wrong for sure.

  • 1

    In case I’m just trying to learn even... I think I learn better by making code and seeing for myself the difference of the use or not of the technique, I found it very interesting its placement "maybe SOLID tells you to do things that make your code worse"for now I’m just a person trying to learn these techniques and principles to start making better decisions about writing code, I’m trying to learn what it all means before putting into practice

Browser other questions tagged

You are not signed in. Login or sign up in order to post.