home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,416 of 33,346   
   Dave Harris to Michael Kilburn   
   Re: Will we ever be able to throw from a   
   18 Jun 12 13:49:03   
   
   839d3781   
   From: brangdon@cix.compulink.co.uk   
      
   crusader.mike@gmail.com (Michael Kilburn) wrote (abridged):   
   > So, In my example (with db connection/etc) you propose to move scope   
   > of life for every object (that needs a shutdown call) with a   
   > function?   
      
   It's often a good idea for each function to do one thing, rather than three   
   different things (open, use, close).   
      
      
   > This is definitely no going to fly when there are multiple objects   
   > like this -- N objects means N nested functions, if function at the   
   > bottom needs access to a variable living in topmost scope -- are you   
   > going pass a reference to it through entire chain of function calls?   
      
   In C++11 we can use lambda expressions (as I mentioned a while back).   
   Or are they what you mean by "poorly written Lisp code"?   
      
      
   > void save()   
   > {   
   >       int k = ...;   
   >       MyClass m = ...;   
   >   
   >       connection c(/* ctor args */);   
   >   
   >       {   
   >           TemporaryTable t1("XXX", c); // drops table in dtor   
   >           ...   
   >           Transaction tran(c);   
   >   
   >           ...   
   >           m.do_smth();   
   >   
   >           // better, but do you really expect user to do this?   
   >           if (...) { ...; tran.shutdown(); t1.shutdown();   
   > c.shutdown();   
   > return; }   
   >           k = ...;   
   >   
   >           TemporaryTable t2("YYY", c); // drops table in dtor   
   >           ...   
   >           if (...) { ...; t2.shutdown(); tran.shutdown();   
   > t1.shutdown(); c.shutdown(); return; }   
   >   
   >       }   
   >       c.shutdown();   
   > }   
      
   void save() {   
        int k = ...;   
        MyClass m = ...;   
      
        connection c(/* ctor args */);   
        c.open( [&] {   
             TemporaryTable t1("XXX", c);   
             t1.open( [&] {   
                 Transaction tran(c);   
                 tran.open( [&] {   
                     m.do_smth();   
                     if (...)   
                         return;   
                     k = ...;   
      
                    TemporaryTable t2("YYY", c);   
                    t2.open( [&] {   
                        ...   
                        if (...)   
                            return;   
                    } );   
                 } );   
             } );   
        } );   
   }   
      
   Or somesuch. Where each "open" is like:   
      
        void Transaction::open( const std::function &fn ) {   
            open();   
            fn();   
            close(); // Or shutdown or whatever.   
        }   
      
      
   > After you've done nesting functions, think about modifying this code   
   > -- moving code around is very simple within single scope, but when   
   > you split this scope into bunch of functions it becomes quite   
   > inconvenient.   
      
   Huge, monolithic functions that have many different concerns are always   
   going to be difficult to maintain, especially if they have meaningless   
   variable names like "c" and "t1", but I don't think my version is any   
   worse than yours.   
      
   -- Dave Harris, Nottingham, UK.   
      
      
   --   
         [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
         [ comp.lang.c++.moderated.    First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca