The Artima Developer Community
Sponsored Link

C++ Community News Forum
The Problem with Programming

212 replies on 15 pages. Most recent reply: Dec 8, 2006 6:12 AM by James Watson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 212 replies on 15 pages [ « | 1 ... 5 6 7 8 9 10 11 12 13 ... 15  | » ]
James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 11:45 AM
Reply to this message Reply
Advertisement
> How about some code?
>
> Here is the point of RAII:

I understand the point very clearly.

> Now, what happens if the call to query throws an
> exception? RAII makes sure that the connection is closed
> without any "finally" blocks.
>
> Would you care to illustrate your technique?

Here's what the client does after preparing the query:
final int count = query.execute(new RowHandler()
{
    public boolean handle(Row row) throws Exception
    {
        data.populateRecord(row);
              
        return false;
    }
});


As I already stated, if the client blows up, returns after the last row or returns false (no more rows wanted) the handler does whatever cleanup is necessary.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 11:47 AM
Reply to this message Reply
> As I already stated, if the client blows up, returns after
> the last row or returns false (no more rows wanted) the
> handler does whatever cleanup is necessary.

Sorry, I meant 'manager' instead of 'handler' here.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 11:54 AM
Reply to this message Reply
> For example, one thing I've tried to do lately is reduce
> the amount of hand-written code in the system. We wrote
> several code generators, and we're planning to add a few
> more, that generate portions of our system from smaller
> input files in little grammers we devised. So we're trying
> to replace big swaths of hand-written Java code with much
> smaller pieces of hand-written code in our little
> languages. It also reduces code because we have fewer
> tests to write, as we don't bother writing tests against
> the generated code.

Can I ask why you have so much code? It kind of sounds like you are automating bad practices. I generate code sometimes but it's been either to fulfill the requirements of a foolish design or for things like creating a wrapper around ResultSet.

Dick Ford

Posts: 149
Nickname: roybatty
Registered: Sep, 2003

Re: The Problem with Programming Posted: Nov 30, 2006 12:14 PM
Reply to this message Reply
> > I am already seeing the leading edge of the Java
> continent
> > crumbling into the onrushing sea, so I hope to figure
> out
> > what comes next
>

Look no further than C# 3.0. The question is who is going to step up to the plate for the JVM. And I wouldn't even think of throwing the name Sun out there. They're too busy keeping their head above water.

> Out of curiosity, what are your thoughts on this, if any?
>
> > so I can stave off my own inevitable
> > obsolescence.
>
> I appreciate the humility and all but give me a break.
> You clearly have skills that are much more long-lasting
> g than knowing how to use language X, as I hope I also do.
> These skills are more rare and valuable and are what a
> a programming language should attempt to harness most
> efficiently. It's clear to me that C++ fails miserably in
> this regard. Java, while a good step forward is also
> falling into glued together crustiness lately.

I'd start reading the Haskell/Ocaml tutorials/books, so when these hybrid languages come along, you're not totally in the dark.

Dick Ford

Posts: 149
Nickname: roybatty
Registered: Sep, 2003

Re: The Problem with Programming Posted: Nov 30, 2006 12:21 PM
Reply to this message Reply
> > As it currently stands, C/C++ beats the heck out of
> > everyone else:
> >
> >
> http://media.corporate-ir.net/media_files/priv/pr_130608/Di
>
> > ceAug06.pdf
> >
> http://media.corporate-ir.net/media_files/priv/pr_130608/Di
>
> > ceNov06.pdf
>
> Since we are at it. Has anyone an explanation for the
> growth of interest in VB? After all there seems to be a
> silent majority that seems not to affected by the
> discussions of frustrated, mostly idealist programmers.

A wild guess, but the VB6 revolt against was successful.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 12:21 PM
Reply to this message Reply
> I'd start reading the Haskell/Ocaml tutorials/books, so
> when these hybrid languages come along, you're not totally
> in the dark.

What makes you think I am in the dark?



Posts: 19
Nickname: pmd
Registered: Oct, 2004

Re: The Problem with Programming Posted: Nov 30, 2006 12:25 PM
Reply to this message Reply
> How about some code?
>
> Here is the point of RAII:
>
> if (some condition) {
> db_connection conn(some_connection_string);
> conn.query(some_query);
> // conn.close(); // unnecessary, but possible
> } // conn gets destructed here and the connection to db
> closed
>
> Now, what happens if the call to query throws an
> exception? RAII makes sure that the connection is closed
> without any "finally" blocks.

More generally, if the code execution leaves the scope of the conn object for any reason, the resource (db connection) is freed immediately and with no other conditions to code (because it part of the stack cleanup). There's no other object that has to know about conn going away in order to clean up after it. The advantage to RAII is a lot more apparent in more complex code where there are many possible returns out from the current scope and trying to explicitly free a resource at every possible point of return is not only messy but a maintenance nightmare.

You can design around this without RAII, I suppose, but why would you want to unless you had to? What could be easier than simply creating an object that manages a resource when it's needed and letting the clean up take care of itself precisely when it is no longer needed? The use of the db_connection in this example is very simple and elegant.

Carsten Saager

Posts: 17
Nickname: csar
Registered: Apr, 2006

Re: The Problem with Programming Posted: Nov 30, 2006 12:27 PM
Reply to this message Reply
> RAII has been very useful to me in C++.
> Is there a good way to do that in Java?

Not really - relying on the garbage collector in Java is a beginners error, if you have enough memory and you ely on your DB-connection to be closed, you are assured to run out of DB-connections (seen in 1999).

C# has the resource concept with the using construct, which makes it a bit cleaner than the try/finally common in Java.

In both languages the programmer has to take care that a "close" or "release" method gets called.

In Java you can experiment with SoftReferences etc. - I would not store Files by that

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 12:29 PM
Reply to this message Reply
> More generally, if the code execution leaves the scope of
> the conn object for any reason, the resource (db
> connection) is freed immediately and with no other
> conditions to code (because it part of the stack cleanup).

Just as in my solution.

> There's no other object that has to know about conn going
> g away in order to clean up after it.

Awesome.

> The advantage to
> RAII is a lot more apparent in more complex code where
> there are many possible returns out from the current scope
> and trying to explicitly free a resource at every possible
> point of return is not only messy but a maintenance
> nightmare.

Same here.

> You can design around this without RAII, I suppose, but
> why would you want to unless you had to?

Because of the problem with RAII that you have still failed to address.

> What
> could be easier than simply creating an object that
> manages a resource when it's needed and letting the clean
> up take care of itself precisely when it is no longer
> needed?

Not having to think about the connection at all, as it is in the solution I am describing.



Posts: 19
Nickname: pmd
Registered: Oct, 2004

Re: The Problem with Programming Posted: Nov 30, 2006 12:37 PM
Reply to this message Reply
>
> > You can design around this without RAII, I suppose, but
> > why would you want to unless you had to?
>
> Because of the problem with RAII that you have still
> failed to address.

... which I still fail to see.

>
> > What
> > could be easier than simply creating an object that
> > manages a resource when it's needed and letting the
> clean
> > up take care of itself precisely when it is no longer
> > needed?
>
> Not having to think about the connection at all, as it is
> in the solution I am describing.

"There is no connection [Neo], its only your mind."

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 12:43 PM
Reply to this message Reply
> >
> > > You can design around this without RAII, I suppose,
> but
> > > why would you want to unless you had to?
> >
> > Because of the problem with RAII that you have still
> > failed to address.
>
> ... which I still fail to see.

If you use the destructor to cleanup the DB connection, what happens (or rather, what does not happen) if the Object is never released?

Cameron Purdy

Posts: 186
Nickname: cpurdy
Registered: Dec, 2004

Re: The Problem with Programming Posted: Nov 30, 2006 12:54 PM
Reply to this message Reply
> > Because of the problem with RAII that you have still
> > failed to address.

> ... which I still fail to see.

The results. The results from the query have to survive the scope of the connection. The results can be used from 100 different points within the application. They are basically cached. Not reference counted. When do they go away? How do you avoid running out of memory?

It's possible in C++ (I have written it .. many tens of thousands of lines of code).

It's a no-op in Java or C# or any other memory-managed language.

Peace,

Cameron Purdy
http://www.tangosol.com/

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: The Problem with Programming Posted: Nov 30, 2006 12:57 PM
Reply to this message Reply
> > For example, one thing I've tried to do lately is
> reduce
> > the amount of hand-written code in the system. We wrote
> > several code generators, and we're planning to add a
> few
> > more, that generate portions of our system from smaller
> > input files in little grammers we devised. So we're
> trying
> > to replace big swaths of hand-written Java code with
> much
> > smaller pieces of hand-written code in our little
> > languages. It also reduces code because we have fewer
> > tests to write, as we don't bother writing tests
> against
> > the generated code.
>
> Can I ask why you have so much code? It kind of sounds
> like you are automating bad practices. I generate code
> sometimes but it's been either to fulfill the requirements
> of a foolish design or for things like creating a wrapper
> around ResultSet.
>
I have tended to do code generation on many different projects over the years. The thing that happens is that occasionally I find we are doing repetitive things that we can't eliminate with the abstractions available in the language itself, but we can if we write a code generator. The code generator makes development faster and more accurate. An example is entities. We use Hibernate for database entities, and there is a lot of code involved in that. We also version some entities, so that there's a history and a "deletion" table too. And in that case there's a stored procedure to make the history record. For a versioned entity we needed to write 3 bean-style classes, 3 XML mapping classes, a stored procedure, several persistence methods in a manager class, etc. Over and over again. So we wrote a grammer in which we can capture what it means to be a versioned entity in our system in a few short lines, and generate about 10 .java, pgsql, and XML files from that.

Another example of repetitive code is pulling parameters out of HttpServletRequest in our controllers. We can't put that in a superclass (i.e., use Java's abstraction tools), because each controller has different parameters to grab. So we created a little grammer that allows us to list the params and other information and generate code for each controller. I want to add generation of some code that makes it easier to write unit tests for those controllers, and will be adding one to generate our "view" classes, which are also quite repetitive in a way that can't easily be zapped by OO techniques, etc.

These aren't the kind of code generators you use to get started and then edit the generated code by hand after that. These are the kind whereby the source code of the system moves from the general purpose language to the little language itself, in which we can express what we want in much less code.

Nemanja Trifunovic

Posts: 172
Nickname: ntrif
Registered: Jun, 2004

Re: The Problem with Programming Posted: Nov 30, 2006 12:58 PM
Reply to this message Reply
>
> If you use the destructor to cleanup the DB connection,
> what happens (or rather, what does not happen) if the
> Object is never released?

You mean if conn is a class member or otherwise something long-lasting that you want to open and close multiple times? Then you need a specialized RAII class that would call conn.open() in the constructor and conn.close() in the destructor and use an object of this class to keep the connection open in the desired scope.

In practice, though, I would rather just design to have a connecton object live as long as the actual connection.

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: The Problem with Programming Posted: Nov 30, 2006 1:10 PM
Reply to this message Reply
> >
> > If you use the destructor to cleanup the DB connection,
> > what happens (or rather, what does not happen) if the
> > Object is never released?
>
> You mean if conn is a class member or otherwise something
> long-lasting that you want to open and close multiple
> times?

Or you want to run a indeterminate number of queries without the cost of reconnecting each time (which in many cases is one of the most time-consuming database operations). Or if it's not you writing the code and another developer (say a contractor) doesn't realize this is how the connection is released and caches it, perhaps by mistake.

> Then you need a specialized RAII class that would
> call conn.open() in the constructor and conn.close() in
> the destructor and use an object of this class to keep the
> connection open in the desired scope.

And how is this easier than what I have proposed? What if you want to change the strategy for managing the connection? For DB2 on the AS400 there are different drivers depending on whether the connection is made locally, remotely or from a stored-procedure. There's no need to cache the connection on the DB, you can use simple connection pooling locally and you need a more robust connection pooling strategy remotely. With the design I am describing, the code that uses the results can be used in any of these scenario without any changes.

> In practice, though, I would rather just design to have a
> connecton object live as long as the actual connection.

And if it lives for an indeterminate period of time, how do you close it at the appropriate time and make sure it's refreshed at the proper intervals?

Flat View: This topic has 212 replies on 15 pages [ « | 5  6  7  8  9  10  11  12  13 | » ]
Topic: Pantheios 1.0.1 full release approaching; beta 17 just released Previous Topic   Next Topic Topic: John Ousterhout on What Limits Software Growth

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use