C ++ if alternatives to operator

Is this me, or does C ++ seem to be asking for more use of the if statement, and then C #?

I have this code base and it contains many things, such as:

if (strcmp((char*)type,"double")==0)

I wondered if this is not a "code smell" when there are too many if statements?

I'm not saying this is bad, but things like comparing strings with a lot of strings involved, could they not be done differently?

Is there an alternative to just writing sequences of if statements?

THIS IS JUST AN EXAMPLE, THIS MAY BE ANY KIND OF STATEMENTS instead of:

if (string a == "blah") then bla
if (string b == "blah") then blo
+3
source share
8 answers

, if (strcmp((char*)type,"double")==0), , "double" case-expression switch. , , std::map<std::string, int> - , , switch.

, std::map<std::string, int (Handler::*)(void)>, , YMMV.

EDIT: : , , , ( ) . , , , .

+13

.

, ( ) std::string s. :

#include <string>
// [...]
std::string type = "whatever";
// [...]
if (type == "double")

#: # include std::.

, , char * ++, ( , , ).

: ( :)).

+5

, ++ "ifs", #. if . ifs , , , , . ++, #. , , , # ++.

, "". , , . : - DFSA. .

+1

, ; 10 ++ 4 # ! , if , # ++?

+1

, . .

+1

"bla" "blo" std:: map .

+1

if - , switch...case. if.

, # , ++ ?

0

, if (strcmp...).

-.

Another design may include a chain of responsibility, where a string is passed to a chain of objects that decide whether they have a match or skip it.

I don't know anything about C ++, which makes it more prone to "if abused" than any other language.

0
source

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


All Articles