How to see the contra movement and its purpose?

I have a developer for the last 3 years, I used if-else or if-else if instructions in my programming habit.

And today I found a reference to This .

One obvious sample that I put here

public void doSomthing(String target, String object){
    //validate requests
    if(target != null && target.trim().length() < 1){
        //invalid request;
    }

    //further logic

}

Now I have seen such a check in all places, libraries. So, I wanted to talk about the merits of such a movement.

Please let me know your opinions.

+3
source share
6 answers

if . , , , - , - .

+9

, , , -if. --. , /.

IF i am a giraffe then eat a leaf
else IF i am a rhino eat some grass
else IF I am a monkey eat a banana
etc

. , ifs, , .

eat().

animal.eat();

. , . , Animal . .

+5

, anti-if if s. , , . if-.

+4

, ifs. . , if, , "" .

+1

. , , , " -IF campain".

IF , . / , . , // .. . : - ?

, . , , .

+1

The example you give seems somewhat reasonable. However, you should not let it crash or return null or something else, but throw an exception if it is invalid. Better yet, use statements.

Using ifs to test object types is not a bad practice, and nothing more than that regarding your classes and relationships. In a statically typed language such as Java, which should not be the norm if (instance b) all the time.

0
source

Source: https://habr.com/ru/post/1781528/


All Articles