Slashdot Mirror


Reuse Code Or Code It Yourself?

eldavojohn writes "I began coding for a project that had simple requirements for my employer — Web services and a test application for them. But requirements have been creeping, as they always do. Initially I had decided to use the Spring Framework with Hibernate. And I re-used a lot of libraries that made things simple and quick for me. The new requests coming in involve capabilities beyond those of the frameworks. Now, I used to be told that good programmers write code and great programmers reuse code. It's starting to look like I would have saved myself a whole lot of time if I had written the database transaction using JDBC instead of Hibernate — now that I'm married to this object model framework, some of this stuff doesn't look doable. So what is better for the majority of software projects out there: reuse code, or code from scratch? What elements or characteristics of a problem point to one option over the other?"

4 of 429 comments (clear)

  1. Re:code from scratch by AceofSpades19 · · Score: 5, Informative

    I write my own implementation of the c standard library and the C++ standard library too, because I find they are not efficient enough and I find using the standard libraries bite me in the ass too

  2. Re:Why not use both options? by MBCook · · Score: 4, Informative

    Not only can you do that, if you want to keep things simpler (stay in the same transaction, for example) Hibernate can run native SQL queries in addition to HQL. You can code your own queries but still have the hibernate call return a full managed object that you can do the normal Hibernate magic on.

    --
    Comment forecast: Bits of genius surrounded by a sea of mediocrity.
  3. Re:Why not use both options? by swillden · · Score: 3, Informative

    AFAIK, you can access a DB via both JDBC and Hibernate. Just do most of the job with the frameworks and just the little bit that isn't supported use plain JDBC.

    Actually, Hibernate gives you a range of options. You can:

    • Construct SQL queries and let Hibernate map the results into objects.
    • Construct SQL queries and get the results as arrays of scalars.
    • Bypass Hibernate completely and just operate on the raw JDBC connection. Just make sure you flush the session first if you're querying DB state that the session might have modified recently, and clear the session cache if your JDBC calls might have modified data that the session has cached.

    Hibernate is successful in large part because it gives you a lot of options, so you can adapt it to your needs.

    --
    Note to ACs: I usually delete AC replies without reading them. If you want to talk to me, log in.
  4. Reuse the well done and code the rest... by benow · · Score: 3, Informative
    There are arguments for each.

    Reuse:

    • great choice for things done well, or for things not understood
    • reuse means for more maintainable code. It'll be far easier for another developer to come in and help out.
    • more stable and optimized
    • saves time (and, possibly, money)

    Homebrew;

    • great if you have time and dedication
    • you'll learn alot, which will help you with future evaluation. You'll also become a better coder.
    • your solution will fit the situation exactly

    The reasons behind reuse should make it your first choice, but at the same time, it's hard to be a great coder without coding. Turn that desire to code loose on the situations where the existing approaches are insufficient.