Skip to main content

Review - I

During code reviews, one of the things that is considered is the "Command Query Separation Principle" Violation.

Operations should either do something and return nothing or return something and do nothing, NOT both.

The common violations are
  1. Getter Methods having void as their return types and
  2. Setter methods having some return values.

Getter Methods having void as their return types.
For eg:
void GetSalary(Employee emp)
{
Money salary;
//Fetch Salary from some source.
emp.Salary = salary;
}
Generally, these violations can be fixed by renaming the method name to Update.

void UpdateSalary(Employee emp);

Setter methods having somereturn values.
For eg:
bool SetDepartment(Employee emp);
Here, it gets tricky. The programmer sets the department inside the method and wants the confirmation by the return value.

There are other ways to handle this rather than sending the return value.
  1. Having a method HasDepartment() in the Employee, to check whether it has Department.
  2. Raise the exception if no matching department can be found for the employee.
The other ways by which this principle is violated are clearly trying to do too many things in a method and has to be refactored.

But, there is a case where this violation is acceptable, which is called Fluent Interfaces. In this case, the signature will become something like the following.

Employee SetDepartment(Employee emp);
In this case also, generally the return values are the entity types and not the primitive or Value Object types.

Comments

Popular posts from this blog

Six ways to land rovers on Mars.

Six ways to land robotic rovers on Mars Mars Rover problem is a popular problem statement used by companies to check object orientation and test-driven development skills. In this article, we'll take the core problem statement and see how the solution evolves through six different levels. Knowledge of high school level maths and little python helps to follow this article. The actual Problem Statement: A squad of robotic rovers is to be landed by NASA on a plateau on Mars. This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth. A rover's position is represented by a combination of x and y coordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner facing North. In order t

The human synergy

After nearly two and half years of inactiveness, I'm reloaded back to throw more ramblings in the open space of internet. The plan is to have a weekly journal on an idea or a product, that I understand or learnt that week. Let's get started and this week's cynosure is "reCAPTCHA" We, often, see sites that ask us to enter the content of a distorted or skewed image of letters or numbers, that is known as CAPTCHA. The widely known fact is that it enables the site to distinguish a human from any automated bots or scripts. It is so reliable, that vast number of sites are using it, and about 200 million captchas are answered by humans in a day. Roughly it takes about 10 seconds per person to answer a captcha. In summation, each day more than 150,000 hours of human effort is consumed by these Captchas, which does nothing more than confirming that the detail is entered by a human. Could this human effort be used for a higher purpose? Yes. The answer

Productivity improvement for remote teams!

The typical working hours in IT companies are from 10 am to 6 pm, though it could extend beyond this time depending on the nature of the project. Usually, we expect everyone to put in about 8 hours a day. There are two broad categories to classify these eight hours: Collaboration time and Core working time.  Collaboration time is when interactions with others are needed and includes all the client meetings, standups, team huddles, and discussions. Ideally, these are the hours that enable individuals to complete their work. Individuals in the team have limited choices on when these meetings have to happen as it could involve multiple stakeholders. Core working time is when the actual work gets done and is the productive hours of the individual. The more focused the individual is, the more effective they are.  These two times overlap with regular office working hours and are not conducive to peak productivity. Some teams strive to have dedicated Core working hours when there are no