tags:

views:

303

answers:

8

Is it me, or does it seem like C++ asks for more use of the 'if' statement then C#?

I have this codebase and it contains lots of things such as this:

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

I wondered isn't it a bit of a 'code smell' when there's just too many if statements?

I'm not saying there bad, but things like string comparisons, with lots of strings involved, can't they be done differently?

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

THIS IS JUST AN EXAMPLE, IT CAN BE ANY KIND OF IF STATEMENTS instead of:

if (string a == "blah") then bla
if (string b == "blah") then blo
+5  A: 

This is code smell.

To minimize it, you should (in this case) use std::strings. Your code then becomes:

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

This is almost identical to the C# equivalent: to compile this sample code in C# code just remove the include and the std::.

Usually, if you find code that uses char* directly in C++ it's usually doing it wrong (except maybe for some rare exceptions).

Edit: Mike DeSimone addressed the problem of further refactoring this in his answer (so I won't mention it here :) ).

utnapistim
+1 for saying not to use char* in C++.
Brian
+1 for code smell. The old style cast is another code smell in my opinion.
Sam Miller
+1  A: 

I don't think C++ requires any more "ifs" than C#. The number of if statements in a program is really just a matter of coding style. You can always eliminate ifs through techniques like polymorphism, table driven methods, and so on. These same techniques are available in both C++ and C#. If there is a difference between programs written in these two languages, I suspect it has to do with the mentality of C# vs C++ programmers.

Note that I don't necessarily recommend "if" elimination. In my experience, if statements tend to be clearer than the alternatives. To directly address your second point: the way to eliminate chained string comparisons like that is to use a DFSA. Most of the time, however, string comparisons are perfectly suitable.

Peter Ruderman
+1 this isn't a C++ issue but a way you write code (in any language) issue
jk
+13  A: 

The reason you do if (strcmp((char*)type,"double")==0) is because you can't make "double" a case-expression and use a switch statement. That said, if you're doing a lot of these kinds of string matches, you may want to look at using a std::map<std::string, int> or something similar and then use the map to convert the string to an index which you then feed to switch.

Personally, in these cases, I'm a fan of things like std::map<std::string, int (Handler::*)(void)>, which lets me create a handler map of class methods, but YMMV.

EDIT: I forgot to mention: the other sweet thing about having a map of strings to methods is that you can alter (usually add to) it at run time. For example, a parser could change its list of keywords and their handlers at runtime after it knows what kind of file it's parsing.

Mike DeSimone
yes i think that's the best way to do the job.
Sanjay Manohar
+1. If the number of if/else statements in a function is very large, it typically calls for such solutions where we can code and maintain each branch of execution separately without maintaining some gigantic function.
+1 for map of string to methods.
Mark B
A: 

Too many if statement are code smell if you can replace them by a switch...case. Otherwise, I don't see the problem with using if.

Maybe you have used more event-driven programming in C#, while your C++ code is more sequential ?

gramm
+1  A: 

It's not something I've noticed; I've done 10 years C++ and 4 years of C# too! Surely the number of if's relates to the design of your code rather than a difference between C# and C++?

JLWarlow
That's quite possibly true, but its just something I happened to notice, however I may be wrong in this.
Tony
@JLWarlow I've seen naively-coded command execution systems in C in a way such that there were several hundred ifs/elses checking for each possible command string in a single function.
Maybe it's down to the difference in how you design C++ apps compared to C#. Just a thought - I know my style has changed over the years. I'll have to keep an eye out to see if there is a noticeable difference between the two languages on the apps I'm working on at the moment.
JLWarlow
@stinky472 That got me thinking about the classes you get in C#. For e.g. you have a string.IsNullOrWhiteSpace(...), how many if's in C++ would that save? I can't think of an equivalent in C++. Maybe C# lets you write "neater" code requiring less if's?
JLWarlow
+1  A: 

To get rid of conditional expressions in either language you can consider the Inversion of Control pattern. It has the side effect of lessening those.

John K
+1  A: 

Based on the nature of 'bla' and 'blo' you can always try to use a std::map, with the strings as keys.

Andrei Ciobanu
A: 

There are better ways to implement a string parser than an endless set of if (strcmp...) statements.

One approach could be a map between strings and function pointers or functor objects.

Another design could involve a chain of responsibility pattern where the string is passed to a chain of objects that decide if they have a match or to pass it along.

I'm not aware of anything about C++ that makes it more prone to "if abuse" than any other language.

Amardeep