Language:
switch to room list switch to menu My folders
Go to page: First ... 84 85 86 87 [88] 89 90 91 92 ... Last
[#] Tue Dec 13 2011 14:56:14 EST from Spell Binder @ Uncensored

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

Ahhhh, ok. I've informally called those if-ladders.

And I had the same impression about those web sites. I looked through them trying to find some examples of the coding techniques they're suggesting, but couldn't find any. Then I realized they wanted me to sign up and pay for a course to learn those techniques.

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.
Spell

[#] Tue Dec 13 2011 18:46:17 EST from dothebart @ Uncensored

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

 

Tue Dec 13 2011 14:56:14 EST from Spell Binder @ Uncensored
Ahhhh, ok. I've informally called those if-ladders.

And I had the same impression about those web sites. I looked through them trying to find some examples of the coding techniques they're suggesting, but couldn't find any. Then I realized they wanted me to sign up and pay for a course to learn those techniques.

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.
Spell

original: ~600 lines with html in it

http://code.citadel.org/?p=citadel.git&a=blob&h=bd60ecb3d3f19c7b03f2a26d473a565cb1d94e7c&hb=d193489281ea51b1f323c1e81a775c88f8cf97ca&f=messages.c

switched version...: ~400 lines

http://code.citadel.org/?p=citadel.git&a=blob&h=ff87641bf0412999c7658d12f95b8241737c011a&hb=3539de2162e0c4bc1ea804e0cd5bc667d39aa8d1&f=webcit/messages.c

handlered version (as its now): ~150 lines

http://code.citadel.org/?p=citadel.git&a=blob&h=5ae3eb0d491601d2e4d5c6c51e01a534e715c41e&hb=fa5cd0e6595d211591cc8eebfa81874b03b6162d&f=webcit/messages.c



[#] Tue Dec 13 2011 19:02:24 EST from dothebart @ Uncensored

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

Tue Dec 13 2011 14:56:14 EST from Spell Binder @ Uncensored
Ahhhh, ok. I've informally called those if-ladders.

oh... and I must admit I created the name "rumpelstilskin lookup" as an homage to the brothers grimms fairy tale, about the little guy skipping around his fire place...

and... the guy asking it for his name with several (dumb) successless attempts to guess its name (though he knows its name)

well, and yes, that name should show a little disrespect of the authors of such beasts.



[#] Fri Dec 16 2011 13:58:09 EST from Ford II @ Uncensored

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

Anybody who has a problem with strings of ifs all toegther in a row in anything other than a really tight loop sequence has really no concept of how fucking fast cpus really are nowadays.

[#] Fri Dec 16 2011 14:21:58 EST from dothebart @ Uncensored

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

Fri Dec 16 2011 13:58:09 EST from Ford II @ Uncensored
Anybody who has a problem with strings of ifs all toegther in a row in anything other than a really tight loop sequence has really no concept of how fucking fast cpus really are nowadays.

anybody claiming this doesn't have a clue how branch prediction works.

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.

 



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

Ax25.



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

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

Ax25



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

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

http://code.citadel.org/?p=citadel.git&a=blob&h=94777828a5974c6f9e7229a97c765b7a8531beb3&hb=ae6cddd2ff16f7056db3c773a96f20bc77f5d2c3&f=citadel/event_client.c

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]

http://events.ccc.de/congress/2011/Fahrplan/events/4817.en.html

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).

Go to page: First ... 84 85 86 87 [88] 89 90 91 92 ... Last