5

Did I do something wrong in my code organization ? I still find my own program difficult to follow. I tried to decompose my program on small, easy to understand classes and functions, still I find myself jumping around to follow action process.

I can't post my entire project here, so here is a sample of a process. My code is a nodejs app, using an M (model) C (controller) T (template) organization. Those are specific to the project, and I have a 4th section of "utilities" classes and functions that I reuse from project to project.

The project is a web directory where user can post links. Here is the process with all intervening actors of a user post:

  1. Controller post handler: gets the post request.
  2. Controller post handler: form.parse is executed to get field inputs
  3. Model validationResult function: instantiate model
  4. Model Validation Error: receives the instance, calls for StringValidation in utility file, and return validation errors or confirmation to move on if there are no errors.
  5. Model validationResult function: gets back the result from 4). If there are no error, returns this info to the controller
  6. Controller : slugifies the title from an utility file, then create entry by calling a database utility from the utility directory.
  7. Redirects.

So for creating an entry, lots of methods spread out in controller, model and utilities are used. When I must fix a problem or add a feature, I must track back and reunderstand the whole process between 3 files and probably twice methods and classes.

I'm a bit stuck here: I tried to create a modular architecture of small, legible and reusable parts, but I'm not sure I created something easier to maintain that a huge linear structure.

How would you have organized this structure ? If decomposing a program into small, reusable and single purpose components is what we should do as most books advocates, we inherently must jump from methods to classes and I don't see how this makes the process any easier.

  • 1
    Your seven steps sound like a good candidate for a class (or method) that encapsulates all seven steps. – Robert Harvey Jun 9 '16 at 14:20
5

The smaller the objects the more difficult to visualise program flow is.

But the problem here is not that you have objects, its that you're trying to create a program from objects in a procedural manner. Your objects should be completely self-contained black boxes to the calling code, if you can make them like that then you can start to use them without caring what they do.

Think of it like this: you use the int datatype all the time, yet I bet you never look inside the operators on it to see what it does. The addition operator for an int 'just works' and so you do not care about the data flow when it gets to that point.

The same should apply to your other objects, whether they are a controller or a Person model or whatever. (in fact, the early days of OO, I used to hear classes called 'types', as in they were considered simply user-defined complex datatypes)

I agree that many frameworks don't get this right and treat classes as bunches of methods that are called in turn,and its too bad this happens. The best you can do with such code is to create sequence diagrams to help you visualise and keep tabs on it.

  • Thanks for your answer. Sequence diagram would be a great help, never thought about it. So in my case, in the 'post' method, instead of having calls to various classes and functions, I should regroup these calls into a single class "CreateEntry" that would be responsible for the entire process, but that will still make the various calls to the utilities here and there ? – Benj Jun 9 '16 at 11:59
  • 1
    not necessarily, but you should be thinking in terms of making the entity self-contained, so you can think of making the entity and operating on it in much simpler terms, like creating it is one action, validating it another and so on. – gbjbaanb Jun 9 '16 at 12:03
  • So in this case, the entity would be the directory record. How would you design the actions around it ? For example, the creation action, the validation action. Would they be external to the entity class, isolated, functions for example ? Or classes on their own (in this case these 2 classes would not represent an entity... more an 'action') ? – Benj Jun 9 '16 at 13:15
  • 1
    I meditated on your answer and got your point. You answer really helped. I ended up designing my new classes with self contained methods. For example, the CRUD method are cointained inside the "user" model class. – Benj Jun 15 '16 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.