Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Application efficiency?

Application efficiency?

Scheduled Pinned Locked Moved Design and Architecture
javadatabasedesignbusinessjson
18 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Neo10101

    Where in the code do I best put object creation (stateful objects) and where not? In what layers? For example, I once put an object reference inside a Hibernate DAO class and I was told that this was incorrect because DAO classes are not supposed to have state. State should be inside the 'service layer'. I have been told that I should not create new objects on recurring method calls such as UpdateCart(). Creation of objects is costly and should not be sitting in your code everywhere. It should be sitting in initialization type methods only. For example, if an e-commerce application needs a cart, put it in the session. If it needs some general main object, put it in the initialization code. Create it once there and let the rest of the application access its instance later. Do not create this instance upon every call. I'm confused about this whole design principle. The strangest thing I heard is 'an app is not supposed to have state. State should be kept in the data layer where the database is'. Really? I'm quite new to these design concepts and I don't know where to look in order to get more educated on it. GoF? Design Patterns books? The goal is to create qualitative code (i.e. to be used in the business). Thanks

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    CsTreval wrote:

    Where in the code do I best put object creation (stateful objects) and where not?

    "As required".

    CsTreval wrote:

    Creation of objects is costly

    No, it's not! This is an incomplete statement, and a fallacy; you can create a huge amount of objects, and very quickly. Creating huge objects in a loop, that is inefficient. The statement should be "do not create costly objects in a tight loop".

    CsTreval wrote:

    and should not be sitting in your code everywhere.

    Decompile the Button class using ILSpy or something similar. You'll see efficient code, without a centralized factory. Objects should NOT come from one location, one uses a factory ONLY if appropriate. The "new" keyword isn't there just for the looks of things.

    CsTreval wrote:

    The strangest thing I heard is 'an app is not supposed to have state.

    Too much monkey business, I'd be walking out on this statement.

    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

    N 1 Reply Last reply
    0
    • L Lost User

      CsTreval wrote:

      Where in the code do I best put object creation (stateful objects) and where not?

      "As required".

      CsTreval wrote:

      Creation of objects is costly

      No, it's not! This is an incomplete statement, and a fallacy; you can create a huge amount of objects, and very quickly. Creating huge objects in a loop, that is inefficient. The statement should be "do not create costly objects in a tight loop".

      CsTreval wrote:

      and should not be sitting in your code everywhere.

      Decompile the Button class using ILSpy or something similar. You'll see efficient code, without a centralized factory. Objects should NOT come from one location, one uses a factory ONLY if appropriate. The "new" keyword isn't there just for the looks of things.

      CsTreval wrote:

      The strangest thing I heard is 'an app is not supposed to have state.

      Too much monkey business, I'd be walking out on this statement.

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      N Offline
      N Offline
      Neo10101
      wrote on last edited by
      #3

      Sorry to say this but you don't seem to be very educated. You never heard of stateless architecture?

      L J D 3 Replies Last reply
      0
      • N Neo10101

        Sorry to say this but you don't seem to be very educated. You never heard of stateless architecture?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        CsTreval wrote:

        Sorry to say this but you don't seem to be very educated

        I did not have any official schooling. Does it show? :-D

        CsTreval wrote:

        You never heard of stateless architecture?

        Heard of it, never worked on it, and I'm doin' this for a few years now. Applications usually have a state, since the components in there have a state. A connection-object is either opened or closed. What you implied is that this information (whether the database-connection is opened or closed) should only be stored in the database. Being my usual self (the sig was a warning), please educate me, show me where I'd need it. Until you can explain what it's good for, I'll judge it by it's appearance, and it appears to be an abstraction too far for the architecture.

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        N D 2 Replies Last reply
        0
        • L Lost User

          CsTreval wrote:

          Sorry to say this but you don't seem to be very educated

          I did not have any official schooling. Does it show? :-D

          CsTreval wrote:

          You never heard of stateless architecture?

          Heard of it, never worked on it, and I'm doin' this for a few years now. Applications usually have a state, since the components in there have a state. A connection-object is either opened or closed. What you implied is that this information (whether the database-connection is opened or closed) should only be stored in the database. Being my usual self (the sig was a warning), please educate me, show me where I'd need it. Until you can explain what it's good for, I'll judge it by it's appearance, and it appears to be an abstraction too far for the architecture.

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          N Offline
          N Offline
          Neo10101
          wrote on last edited by
          #5

          Yes, it shows.. unfortunately.

          L J P D 4 Replies Last reply
          0
          • N Neo10101

            Yes, it shows.. unfortunately.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            CsTreval wrote:

            Yes, it shows.. unfortunately

            You're the first to complain :-\ I could have bought an official degree a few years ago, sounded like a waste of money. What good is a degree without education?

            CsTreval wrote:

            I'm studying at a very severe university

            That does not impress me at all, especially after having to clean up behind some academics.

            CsTreval wrote:

            For example, if you create an object it has to be created in the right place or else you will be criticized on it.

            Ehr.. I'll make it worse; you'll have to be able to defend each and every letter you type. I wouldn't accept code in the base that is not completely understood by it's author.

            CsTreval wrote:

            Please see 'Arjan Tijms' answer for a very educated answer

            .NET applications are rarely pure in their architecture, and most of them will mix various idea's. A state-machine is cool when you're talking about traffic-lights, but it's not the base for most LOB-apps. And no, it is not said that the traffic-light information "should be" in a database. One should learn to think; why in a database? Afraid that you'll loose state when there's no power? Then you'd better not use an in-memory database, or learn the memento-pattern and learn to serialize that information. You still failed to answer my question; what are the benefits, or to quote Garfield; "what's in it for me?"

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            1 Reply Last reply
            0
            • N Neo10101

              Yes, it shows.. unfortunately.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #7

              CsTreval wrote:

              For example, if you create an object it has to be created in the right place or else you will be criticized on it. So eventually I want to follow these strict rules.. because if I start coding carelessly, I will fail.

              Nonsense as stated. There might be rules for the classes you are taken. They might not even explain them to you. They might even be teaching you something that is correct in limited situations. However your statements as a general rule are wrong.

              1 Reply Last reply
              0
              • N Neo10101

                Sorry to say this but you don't seem to be very educated. You never heard of stateless architecture?

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #8

                CsTreval wrote:

                You never heard of stateless architecture?

                I have. I have used it. It however is limited to very specific applications and your original post did not make it clear what you are doing. Additionally stateless architecture has little to do with object creation and much more to do with how data is used within objects. I can create a stateless architecture in C (which has no objects) using global variables (which exist for the lifetime of the application.) And MORE importantly efficiency has nothing to to with correct implementation. I can create very efficient stateful code which is incorrect because it is supposed to be stateless.

                1 Reply Last reply
                0
                • N Neo10101

                  Where in the code do I best put object creation (stateful objects) and where not? In what layers? For example, I once put an object reference inside a Hibernate DAO class and I was told that this was incorrect because DAO classes are not supposed to have state. State should be inside the 'service layer'. I have been told that I should not create new objects on recurring method calls such as UpdateCart(). Creation of objects is costly and should not be sitting in your code everywhere. It should be sitting in initialization type methods only. For example, if an e-commerce application needs a cart, put it in the session. If it needs some general main object, put it in the initialization code. Create it once there and let the rest of the application access its instance later. Do not create this instance upon every call. I'm confused about this whole design principle. The strangest thing I heard is 'an app is not supposed to have state. State should be kept in the data layer where the database is'. Really? I'm quite new to these design concepts and I don't know where to look in order to get more educated on it. GoF? Design Patterns books? The goal is to create qualitative code (i.e. to be used in the business). Thanks

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #9

                  I am hopeful that your question is confused rather than you and or whoever is providing this information to you. Retrieving data from a database is a relatively expensive operation. Period. Doesn't matter why you do it. That however doesn't mean that you can retrieve it once only. The business needs drive in what way it can be retrieved. You put database code in a database layer because that has multiple benefits. You don't put database code outside the database layer because doing that completely violates the point of having the database layer in the first place. And notice that nothing in the above has anything to do with stateful or stateless.

                  CsTreval wrote:

                  For example, I once put an object reference inside a Hibernate DAO class and I was told that this was incorrect because DAO classes are not supposed to have state. State should be inside the 'service layer'.

                  As a general statement that is nonsense. Implementing code for real businesses is not clean. You attempt to create well crafted code but only to the extent that it makes sense to meet the needs of the business. Other than that I can only suppose that you did something really wrong in terms of service separation and database layer separation and that point was being emphasized. The database layer should to database stuff. The service layer should do service layer stuff. Again normally, not exclusively. And note that none of this has anything to to with the general topic of object creation. Nor with performance.

                  CsTreval wrote:

                  Create it once there and let the rest of the application access its instance later. Do not create this instance upon every call.

                  As stated generally nonsense. It depends on what the object does. I don't need to retrieve the database connection string on every call. There is however no point in not creating a StringBuilder on every call.

                  1 Reply Last reply
                  0
                  • N Neo10101

                    Yes, it shows.. unfortunately.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #10

                    Could you let us know where this university is and what, exactly, the course is that you are doing?

                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                    1 Reply Last reply
                    0
                    • N Neo10101

                      Sorry to say this but you don't seem to be very educated. You never heard of stateless architecture?

                      D Offline
                      D Offline
                      dojohansen
                      wrote on last edited by
                      #11

                      Sorry to say this, but the OP wasn't asking about stateless architecture.

                      1 Reply Last reply
                      0
                      • L Lost User

                        CsTreval wrote:

                        Sorry to say this but you don't seem to be very educated

                        I did not have any official schooling. Does it show? :-D

                        CsTreval wrote:

                        You never heard of stateless architecture?

                        Heard of it, never worked on it, and I'm doin' this for a few years now. Applications usually have a state, since the components in there have a state. A connection-object is either opened or closed. What you implied is that this information (whether the database-connection is opened or closed) should only be stored in the database. Being my usual self (the sig was a warning), please educate me, show me where I'd need it. Until you can explain what it's good for, I'll judge it by it's appearance, and it appears to be an abstraction too far for the architecture.

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        D Offline
                        D Offline
                        dojohansen
                        wrote on last edited by
                        #12

                        Eddy Vluggen wrote:

                        What you implied is that this information (whether the database-connection is opened or closed) should only be stored in the database.

                        That's not really what's meant by stateless design. :) I'd happily agree with you that any information stored anywhere IS "state" but going by this definition we'd be talking about information-free information systems, and I suspect they'd be useless. What people actually mean when they speak of stateless is that the object instances themselves are stateless. There are no instance members. But there are method parameters and local variables, and they live on the stack. (If they are references to other objects, the objects themselves of course live on the heap. But every reference to them will be on the stack.) This leads to two properties which are sometimes desireable: You can't corrupt the heap in such a way that it affects more than one thread. A service for instance may fail under some condition, but this can't leave the server itself corrupted and subsequent requests still have a chance of succeeding. Secondly, it means the same "objects" can be shared among many threads, because they use the stack of each thread to store all the state they manipulate. Lest you now become a fan, let me just add this: Stateless design also means throwing OOP out the window! Some will argue on this point, but here's my take. What really defines OOP is 3 things: Encapsulation. Inheritance. Polymorphism. But stateless implies you are creating "objects" that really are nothing more than a bunch of routines. And although you still have inheritance and polymorphism to play with, you can't put them to really good use, because you no longer have any objects, just mini-libraries of routines and subroutines! Personally, I consider encapsulation the far most powerful - and least understood - aspect of object-oriented programming. It is what makes it object-oriented! You cannot model stuff as an object if you can't encapsulate. You could still do it without inheritance or polymorphism, though they are both *very* useful and powerful concepts. The most I would ever consider using stateless architecture for would be a small part of a server. This would ensure this part couldn't get corrupted. Then I'd make everything else properly object-oriented and build capability to "reset" the state into it. Hence the service could sort of "reboot" in case of trouble, and I could still program the vast majority of the system pro

                        L 1 Reply Last reply
                        0
                        • N Neo10101

                          Yes, it shows.. unfortunately.

                          D Offline
                          D Offline
                          dojohansen
                          wrote on last edited by
                          #13

                          Take my advice: Don't make yourself look worse than you already have.

                          1 Reply Last reply
                          0
                          • D dojohansen

                            Eddy Vluggen wrote:

                            What you implied is that this information (whether the database-connection is opened or closed) should only be stored in the database.

                            That's not really what's meant by stateless design. :) I'd happily agree with you that any information stored anywhere IS "state" but going by this definition we'd be talking about information-free information systems, and I suspect they'd be useless. What people actually mean when they speak of stateless is that the object instances themselves are stateless. There are no instance members. But there are method parameters and local variables, and they live on the stack. (If they are references to other objects, the objects themselves of course live on the heap. But every reference to them will be on the stack.) This leads to two properties which are sometimes desireable: You can't corrupt the heap in such a way that it affects more than one thread. A service for instance may fail under some condition, but this can't leave the server itself corrupted and subsequent requests still have a chance of succeeding. Secondly, it means the same "objects" can be shared among many threads, because they use the stack of each thread to store all the state they manipulate. Lest you now become a fan, let me just add this: Stateless design also means throwing OOP out the window! Some will argue on this point, but here's my take. What really defines OOP is 3 things: Encapsulation. Inheritance. Polymorphism. But stateless implies you are creating "objects" that really are nothing more than a bunch of routines. And although you still have inheritance and polymorphism to play with, you can't put them to really good use, because you no longer have any objects, just mini-libraries of routines and subroutines! Personally, I consider encapsulation the far most powerful - and least understood - aspect of object-oriented programming. It is what makes it object-oriented! You cannot model stuff as an object if you can't encapsulate. You could still do it without inheritance or polymorphism, though they are both *very* useful and powerful concepts. The most I would ever consider using stateless architecture for would be a small part of a server. This would ensure this part couldn't get corrupted. Then I'd make everything else properly object-oriented and build capability to "reset" the state into it. Hence the service could sort of "reboot" in case of trouble, and I could still program the vast majority of the system pro

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #14

                            dojohansen wrote:

                            What people actually mean when they speak of stateless is that the object instances themselves are stateless. There are no instance members.

                            This must be theoretical; I don't see this work with static (shared) members. It's a nice explanation, but at this point, there's not much I can actually do with it :^)

                            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                            D 1 Reply Last reply
                            0
                            • L Lost User

                              dojohansen wrote:

                              What people actually mean when they speak of stateless is that the object instances themselves are stateless. There are no instance members.

                              This must be theoretical; I don't see this work with static (shared) members. It's a nice explanation, but at this point, there's not much I can actually do with it :^)

                              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                              D Offline
                              D Offline
                              dojohansen
                              wrote on last edited by
                              #15

                              Actually I made a mistake; correct would be no instance *state*, i.e. no instance fields, only local variables and method parameters. But it sure can work also with only static methods, though in that case you of course *also* lose the ability to use polymorphy. Note that this doesn't imply there should be any static fields. They too would live on the heap and represent shared state. Come to think of it, "isolated state" or "local state" would actually be better names for this type of architecture than "stateless". And again, personally I'm no fan of the beast.

                              L 1 Reply Last reply
                              0
                              • D dojohansen

                                Actually I made a mistake; correct would be no instance *state*, i.e. no instance fields, only local variables and method parameters. But it sure can work also with only static methods, though in that case you of course *also* lose the ability to use polymorphy. Note that this doesn't imply there should be any static fields. They too would live on the heap and represent shared state. Come to think of it, "isolated state" or "local state" would actually be better names for this type of architecture than "stateless". And again, personally I'm no fan of the beast.

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #16

                                dojohansen wrote:

                                correct would be no instance *state*, i.e. no instance fields, only local variables and method parameters.

                                Sounds like a functional language. Something similar to F#?

                                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                D 1 Reply Last reply
                                0
                                • L Lost User

                                  dojohansen wrote:

                                  correct would be no instance *state*, i.e. no instance fields, only local variables and method parameters.

                                  Sounds like a functional language. Something similar to F#?

                                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                  D Offline
                                  D Offline
                                  dojohansen
                                  wrote on last edited by
                                  #17

                                  Now you're just teasing me...? It is nothing like a functional language. It's plain old procedural code. And you can of course write procedural code in C#, in methods in classes. But that doesn't make it OOP. :)

                                  L 1 Reply Last reply
                                  0
                                  • D dojohansen

                                    Now you're just teasing me...? It is nothing like a functional language. It's plain old procedural code. And you can of course write procedural code in C#, in methods in classes. But that doesn't make it OOP. :)

                                    L Offline
                                    L Offline
                                    Lost User
                                    wrote on last edited by
                                    #18

                                    dojohansen wrote:

                                    Now you're just teasing me...?

                                    Partly; F# is a functional language, seemed to map to having everything in a function.

                                    dojohansen wrote:

                                    It's plain old procedural code.

                                    In that case, I wrote a lot of those applications, in AMOS Basic. Somehow, that one seems to fit the previous description less than F#. It became a mess quite quickly when your app grows larger. The best thing since sliced bread was the idea to group 'em. The idea that the local variables could be shared among that group was brilliant; took a lot of repeating parameters out of the procedures. Lots of people still program in a procedural way in the new OO-environment, simply because it's easier to understand. And given recursion, you could still kill the stack.

                                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups