switch to room list switch to menu My folders
Go to page: [1] 2 3 4 5 ... Last
↑↑↑ Old messages ↑↑↑            ↓↓↓ New messages ↓↓↓
[#] Mon Dec 19 2011 11:47:22 EST from IGnatius T Foobar @ Uncensored

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

Doing a quick google about the code snippet you posted, bart, would
lead me to think that the code could be redone by using foo as an index

into a table of function pointers.

That's exactly what dothebart likes to do when confronted with multiple screenfuls of if-else. He seems to be fond of hash tables whose values are function pointers. I have to admit that it does offer an additional amount of modularity; when new branches are introduced one only needs to "register" additional functions into the system.

I prefer to only see that coding style used for very large and homogenous lookups. It is a style that can be (and is) overused at times. One must weigh the performance benefits against the complexity. A "Rumpelstiltskin lookup" is not as efficient but it is quite easy to follow when reading the code.

[#] Mon Dec 19 2011 16:24:21 EST from Spell Binder @ Uncensored

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

I can imagine an alternative approach using some object-oriented design, but I'm a bit rusty on my OOD, so it might not be a great design.

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 see is how those object get generated. If you make an object factory class, then you may end up with a similar chain of if-elses, or switch statements, inside the class to generate the appropriate objects.

Maybe LS or fleeb might have some interesting suggestions.
OO Binder

[#] Mon Dec 19 2011 17:37:16 EST from LoanShark @ Uncensored

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

Sorry, I missed something, is there a reason why the switch statement is inappropriate?

Anyway, in Java, you might use an enum class that implements a method, or its close cousin, the so-called typesafe enum design pattern.

But it really depends on the problem at hand. There are tons of ways you can use the polymorphism of OO languages, and they are not all created equal; other examples are the command-chain pattern, which is sort of a special case of the composite pattern, and the visitor pattern.

In many cases, functional languages can express these same patterns more concisely.

[#] Mon Dec 19 2011 17:42:26 EST from LoanShark @ Uncensored

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

(use and misuse, I should say.)

a hashtable (or for non-sparse integer keys, just a vector) pointing to an object that implements a particular interface can be a way to go. (When you use an enum in java, you automatically get such an ordinal-indexed array of objects, and you also automatically get a way to lookup the enum object instance that correspondsto a particular name. and then you could invoke some abstract method on it.

but if your problem is too small for this, you need to ask why not a switch statement

[#] Mon Dec 19 2011 23:48:40 EST from ax25 @ Uncensored

Subject: if statements == goto?

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

Agreed.  A switch statement is less evil compiled.

Not sure why a series of if statements is evil per say, (seems like a goto of old....)


[#] 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.

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