switch to room list switch to menu My folders
Go to page: 1 [2] 3 4 5 6 ... Last
[#] Mon Dec 19 2011 23:50:48 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Depending on the situation, I'll either use a switch statement or function overloading.

If I have to choose a path of execution based on some kind of number, I use the switch statement. It's not too hard to read, fairly fast to execute, and just works.

If I have to choose a path of execution based on a kind of object, I use function overloading. I write the same function several times for a particular object, but use different parameters. So, while it looks like you're always calling the same function in code, you're actually calling a number of different related functions that do mostly the same thing, but tailored for each type of object (which, of course, is the point of function overloading).

If I have to choose a path of execution based on a string, though... ugh.
I normally find some way to convert the string to a number, then do a switch statement. All those if/elses don't improve legibility in this case.

[#] Mon Dec 19 2011 23:52:10 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

I've found using a series of if statements doesn't help with legibility.
When I have to work with the code later, it just looks like junk to me. Hard to wash through it and get at what you want to do.

A switch statement that calls one of a number of tailored functions lets you get right to where you need to go, is very legible, and is quite fast.

[#] Mon Dec 19 2011 23:56:22 EST from ax25 @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Would love to do a day of pair programming with you fleeb!  (Hope that does not sound to gay!)--- PPS - hope you are not gay,  I am from a generation and don't think it is.


[#] Tue Dec 20 2011 00:17:21 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Hilariously, I am notoriously gay. But I've been in a long term monogamous relationship, so don't worry about it.

I've just been programming regularly for over a decade, while keeping up with based C++ techniques and advances on the language. After a while, you get a feel for things.

[#] Tue Dec 20 2011 02:23:06 EST from dothebart @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

*giggles* yea bingo ;)

I think the size of the switch statement mustn't exceed a certain size, else callback functions are the better choice.

i'd say here we have some edge cases *chuckles*

also some code who could use some review. so, comments welcome.

[#] Tue Dec 20 2011 22:18:40 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Oh, ever since boost::bind (or tr1::bind, if you prefer), I love doing callbacks, and use them quite often. You can do some amazingly creative things with bind.

[#] Tue Dec 27 2011 13:44:33 EST from dothebart @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

whew. creating sort of a programming language backend, that takes format string exploits from carefuly handcrafting to an automated generation of code therefore..

[#] Tue Dec 27 2011 16:59:39 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

I do not use printf-style formattings (outgoing or incoming) for this very reason in my C++ work. I prefer to use input streams, regular expressions, and/or a kind of BNF parser that boost offers to parse input, depending on what I need to do.

[#] Tue Dec 27 2011 17:12:01 EST from dothebart @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

the webcit templating engine also offers an effective way around.

it merely uses hard coded formatstrings to add a %d to strings.

though i'm not yet done with templating everything ;-)

[#] Wed Dec 28 2011 09:55:45 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Well, that's another thing. I try not to have any formatting strings 'formatted' that come from users, unless I do so with intention... and even then, I use my own formatting language (usually simple replacement, like ${VARNAME} getting translated to the value of VARNAME).

[#] Mon Jan 09 2012 22:49:30 EST from IGnatius T Foobar @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Arbitrary format strings just invite abuse, of course. I think some of the compilers now throw a warning if you try to supply a variable as a format string.

[#] Wed Jan 11 2012 18:49:21 EST from dothebart @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

heh, just found in the RSS feed:

As mobile devices become a major development platform, scripting languages are beginning to fall out of favor.

though there still are c-carpet and java on phones. well. I wouldn't exactly call that "scripting languages are beginning to fall out of favour"

[#] Thu Jan 12 2012 14:53:47 EST from Ford II @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

it also depends on the cost of the decision evaluated by the if; i.e.
strcmp's aren't that cheap if its a dozend of them in a row.

ahhh... see... but they are.
count the cycles of the worst case scenario, then divide that by the processor speed and see how many zeroes are to the right of the decimal point. You might be surprised.

[#] Thu Jan 12 2012 14:55:52 EST from Ford II @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Basically, you'd encapsulate the behavior for each different if-else

into a separate object, one for each "branch." Then you'd just pass
the appropriate object into your readloop() function. The problem I

this would end up about the same as the hash lookup, except it's broken into two parts. The lookup in the hash would be like the lookup in the virtual function table and making the right object, would be like building the hash.

[#] Thu Jan 12 2012 14:57:51 EST from Ford II @ Uncensored

Subject: Re: if statements == goto?

[Reply] [ReplyQuoted] [Headers] [Print]

Agreed.  A switch statement is less evil compiled.

A switch statement is brilliantly efficient, but I thought we were talking about a pile of string compare ifs.

[#] Thu Jan 12 2012 14:59:48 EST from Ford II @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Dec 19 2011 11:56pm from ax25 @uncnsrd
Would love to do a day of pair programming with you fleeb!  (Hope
that does not sound to gay!)--- PPS - hope you are not gay,  I am
from a generation and don't think it is.


Sorry, that had me laughing a lot. :-)

[#] Thu Jan 12 2012 15:03:25 EST from Ford II @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

A last work about ifs versus switch, the downside of switch is that it has to be one unbroken code block.
A string of ifs can be broken out into separate functions grouped by category.
if (handlemathfunctions() == 0)
if (handlestringfunctions() == 0)
if (handlekitchensinkfunctions() == 0)
return "command not found.";

and each of those three functions can have strings of ifs in them.

[#] Thu Jan 12 2012 15:39:57 EST from dothebart @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

hm, ford a little late to the party? or some bug?

[#] Thu Jan 12 2012 15:44:05 EST from LoanShark @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Ford is a bug.

[#] Thu Jan 12 2012 16:17:26 EST from fleeb @ Uncensored

[Reply] [ReplyQuoted] [Headers] [Print]

Volkswagon's Ford Bug. Test drive one today.

Go to page: 1 [2] 3 4 5 6 ... Last