Subj : Re: dangers of operator overloading To : comp.programming From : rem642b Date : Thu Aug 11 2005 07:41 am > From: makc.the.gr...@gmail.com > So, I chose 1-byte field and simple name enumeration, which forced me > to implement funcs > func1(string s) { > if(str == "blah") return 1; else > if(str == "duhh") return 2; else > ... 100+ lines of same c# code here... > and > func2(int i) { > if(i == 1) return "blah"; else > if(i == 2) return "duhh"; else > ... etc... Why didn't you use a decent programming language, such as Lisp, where you could directly write source-code syntax for a data structure which was a map between the two versions of the data: (defparameter namint '(("blah" . 1) ("duhh" . 2) ... )) and then func1 and func2 would be nothing more than wrappers around assoc and rassoc respectively? Then if you ever discovered you needed the table to be runtime configurable by simply editing a text file, you could cut&paste the list of pairs from the source file to a standalone configuration-data file, and a simple READ and assignment to namint would put the latest version into use, not a line of actual code (func1 or func2) would need to be changed. And if the list got very long and started taking significant time to search linearily down the list, you could "compile" the table into a simple lookup array in one direction and a binary-search array in the other direction, without changing the syntax of the configuration-data file hence not needing to re-train any staff whose duties included maintaining that table. > So I sat and wrote small JavaScript script (under 10 lines) to walk > through typical xml data, collect names and generate complete c# > mapping functions code. If you were programming in Lisp, you wouldn't have needed to write a reader, because Lisp already has a built-in reader for s-expressions. Also XML is horribly verbose compared to s-expressions for such simple uses as this kind of lookup table. Compare whatever you had to write in XML with what I showed you in Lisp (s-expression syntax) there. Imagine how much labor would be saved with s-expressions instead of XML, every time somebody has to go into a text editor to add some new entries to the table? > my point is that even for c# (which generally allows MUCH shorter > code vc c++, which in it's turn allows MUCH shorter code vc c) I had to > write 10- lines in yet another language to generate 200+ lines of code, > or go and type it myself. Would I be a philosopher, I 'd say I don't > understand why do I have to take time and write code, which only makes > yet another code - these 10 lines are all that I ideally should write > in "high-level" language, not 200. I agree. That's what's so nice about LIsp, on two counts: First, the built-in language is already so high level, with built-in syntax for expressing arbitrary nested lists of ordinary data (strings, numbers mostly, although Lisp has many other data types these are the two primary ones used in business applications). Second, you have structured macros which allow you to build new levels of abstraction on top of the built-in abstractions to thereby build a domain-specific meta-language that compiles directly to the built-in language. (By "structured macros" I mean that both the input and output of such a macro is a parse tree, i.e. nested lists, so you pick parts of the input based on exact position within the tree instead of what you have to do with string macros such as in C of somehow devising a way of delimiting the sub-strings and quoting any delimiter you want used as data, a horrible pain that usually fails in inscrutable ways.) > PS: always Ctrl+A+Ctrl+C before submit anything. What's that supposed to mean? Here when I want to save my edit, if I'm on Macintosh I just do Clover-S, and if in emacs on Unix or Linux I do Ctrl-X Ctrl-C, and if in VI on Unix inside lynx I do :w! tmp I'm curious what strange system you're on that has Ctrl-A Ctrl-C? .