3

So I thought about this, and I don't know if it's included or not in any methodology.

I think the advantages of this coding style is that, at the lowest level, the code is extremely testable, and then the integration tests should also be very easy to build.

I also think this would make the code more readable and the UML would be understood faster.

So here's my example:

class CoolObject{
var member1; //needed for instance in lifecycle events
var member2; //same comment

   //This method could be for instance an event handler
   //Notice this contains only assignments and method calls. No library calls or lower level stuff
   [public] method high_level(params...){ 
    var local_var1;
    var local_var2;
    local_var1 = call method lower_level1(param1,param2);
    local_var2 = call method lower_level2(param1, local_var1);
    member1 = call method lower_level3(local_var2);
   }

   //Notice this contains only library calls and lower level processing
   [private] method lower_level1(param1, param2){
    return param1 + param2 + libraryXXY123.function142(current_date);
   }

   //Notice this contains only library calls and lower level processing
   [private] method lower_level2(param1, param2){
     var return_value;
     loop over param2{
        if(condition){
           add param1 to return_value;
        }
     }
     return return_value + libraryASDF123.function3132(system_user);

   }

}

Please note that this is not written in any specific language, as I only wanted to illustrate the concept.

So do you know some methodologies that use this, or that warn against it? Please elaborate on the answer, as I think this would be a good idea, and would like this either confirmed, or the opposite.

3
  • 2
    can you add what is public/private/protected in this object? Because if the lower_level1 and lower_level2 are private, this is just a normal object... Commented Oct 3, 2013 at 14:52
  • 1
    It's really unclear what you're getting at here, are you asking if it's ok to have private methods that do the work and public methods that just call them? That's basically how OO is supposed to be done. Have a look at the Single Responsibility Principle and realize that methods should in general do one thing, so it's common to create private methods that do one simple thing and public methods that compose those together to do one overarching thing Commented Oct 3, 2013 at 15:04
  • @Jean-FrançoisCôté I will, for the sake of something... but let's say i adress languages like python, or javascript where visibility modifiers are a matter of conventions. Commented Oct 3, 2013 at 15:46

3 Answers 3

8

In layman's words:

  • That's good structured programming practice.
  • OOP builds on structured programming, meaning code inside a class is usually structured programming code.
  • Your sample is not a good or bad OOP idea per se, but rather a good structured programming idea.
  • Good OOP code usually builds on good structured programming code.
  • Your code seems OK to me.
1
  • aha, ok, so the style i'm writing in is called "structured programming".. ok, i'll have a look at that. Commented Oct 3, 2013 at 15:49
2

It is called "layered design".

The idea is that your system design is separated into "layers". Each layer provides an interface for the layer above it. Each layer calls only methods from the layer immediately below it.

The canonical reference to this approach is Dijkstra's paper "The Structure of the T.H.E. Multiprogramming System". It is required reading in this racket.

2
  • For me layers have to do with classes that belong to different layers. The question of @user61852 has high- and low-level-methods within one class.
    – k3b
    Commented Oct 4, 2013 at 0:19
  • @k3b: True. If you're going to insist on doing C++, or some other allegedly object-oriented language, and you insist on organizing things into classes, then layers are disjoint sets of classes. (Sets of classes, as opposed to single classes, because you want to organize things at each layer. Disjoint, because layers are not allowed to overlap.) Commented Oct 4, 2013 at 3:25
-1

More than the actual coding, the important thing is to come up with a good programming structure. This is something your program has done, because things are well sorted out, and it follows logically from one step to another. It could lay a good foundation for similar programs, and if it needs to be changed, its good structure makes it easier to change than would otherwise be the case.

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