Understanding software behaviour parameterization I

Writing  code that is susceptible to change is always difficult for newbie and easy for the expert .

Understanding behavior parameterization is a software development pattern or process that allows programmers, developers and engineers to handle frequent requirement changes in a smart way.

Or
The ability to tell a method to take multiple behaviors (or strategies) as parameters and use them internally to accomplish different behaviors

Real life example of BP

Suppose in a university software system where searching for students information is highly needed, perhaps any of the university management might want to search for all students with cgpa => 4.0/5.0 in level 1

2017-05-11 at 07-52-26 - Copy - Copy

Example

public class University
{
    private int totalNoStudent;
    private int totalNoLecturer;
}

class Student
{
    private String fullName, department;
    private double cgpa;
    private int level;

    public double getCgpa() {
        return cgpa;
    }
    public void setCgpa(double cgpa) {
        this.cgpa = cgpa;
    }
    // ALL OTHER POJO HERE

    public List filter ( List studentList )
    {
        List myStudentList = new ArrayList<>();
        for ( Student myStudent : myStudentList)
            if ( myStudent.getCgpa() >= 4.0)
                myStudentList.add(myStudent);
        return myStudentList;
    }
}

Now suppose another management in the same university also want to search for all students in the university with cpga of <=2.0/5.0 in level 1 to 3. This task is quite different from the above stated task, as a result of the newly stated task, the method’s behavior is parameterized based on that block of code which is a more bit more advanced that’s
previous search method.

2017-05-11 at 07-52-26 - Copy - Copy.png

class Student
{
    private int level;
    public int getLevel() {
        return level;
    }
    public void setLevel(int level) {
        this.level = level;
    }

    public List filter ( List studentList )
    {
        List myStudentList = new ArrayList<>();
        for ( Student myStudent : myStudentList)
            if ( myStudent.getCgpa() >= 4.0  && myStudent.getLevel() <= 3)
                myStudentList.add(myStudent);
        return myStudentList;
    }
}

Now this article focus on how to make your code more flexible to requirement change

What you could do is add a parameter to your method to parameterize the data and be more flexible to such changes:

Very simple ,right ?

Suppose another management want to know the students in  department of engineering and medicine with a cgpa of =>2.5/5.0 in level 1,3,6 ( Assuming that no level 6 in engineering department )
Possibly with your exposure, you realize in advance that the management may want to vary the level and department , so you create the following method to cope with various level and department through an additional parameter:

public static List filterStudentByCgpAndDpet
(List studentList,double result,int level, String dept)
{
    List result = new ArrayList<>();
    For (Student student : studentList){
    if ( ( student.getCgpa () => 2.5 && student.getDepet().equal(dept) &&
                                                student.getLevel()) ){
        result.add(student);
    }

}
    return result;

This look like a good solution, ask yourself this simple question, how will I duplicate most of the implementation for traversing the studentList and applying the filtering criteria on each student in various level, department… . This is somewhat
disappointing because it breaks the DRY (don’t repeat yourself) principle of software
engineering. What if you want to alter the filter traversing to enhance performance? You now have to modify the implementation of all of your methods instead of a single one. This is expensive from an engineering effort perspective.

Easily you could combine the cgpa , level and department into one method called filter. But then you’d still need a way to differentiate what attribute you want to filter on. You could add a flag to differentiate
between cpga ,level and department queries. (You may try this at home, but please don’t do this in real industrial environment’s a seriously bad concept and methodology.
Seriously speaking , what you need is a better way to tell your filterStudent
method the selection criteria for student.
Do you enjoy this article ? Read the next episode of this article

Advertisements

One thought on “Understanding software behaviour parameterization I

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: