Skip to main content
added 70 characters in body
Source Link
Engineert
  • 929
  • 1
  • 6
  • 18

There are many countable reason why you should not to do that wayyou should not to do that way. I try to explain it what's wrong and what will you need to do also if you do your way:

1. DRY

When you do that way, it's against to Don't Repeat Yourself(DRY). You copy and paste same code for each type. By a good design, you can do it just one method which doesn't need to know type of data and there will be just one place to change when needed.

2. Limitation per Type

Another reason why you shouldn't do that way, you restrict your code by one logic per type. Assume that you need to get all Capital. When you check object type in your code and do your logic, you can not give flexibility to call another service.

public interface ICapitalService
{
    List<Capital> GetAll();
}

public class LocalCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from db.
    }
}

public class RemoteCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from web service
    }
}

Someone get capitals from remote service and others may want to get from db. But you restrict it by checking type of Capital and doing your specific logic in your code. You can't add another option for Capital.

3. You need to ensure just child class type is checking.

When you check type by instanceof, it will also return true for parent class. So, you may not reach deserved code block.

if (list.get(0) instanceof Car) {
   //...
}
else if (list.get(0) instanceof Volvo)
{
  //..
}

Because of Volvo is a car, you can't reach Volvo block and you need to manage manually.

4. Software Language Limitation

You are checking List of item types by getting first item of list.(list.get(0)). If the first item of list is null, instanceof will return false and logic won't work as expected and last else block will work.

There are many countable reason why you should not to do that way. I try to explain it:

1. DRY

When you do that way, it's against to Don't Repeat Yourself(DRY). You copy and paste same code for each type. By a good design, you can do it just one method which doesn't need to know type of data and there will be just one place to change when needed.

2. Limitation per Type

Another reason why you shouldn't do that way, you restrict your code by one logic per type. Assume that you need to get all Capital. When you check object type in your code and do your logic, you can not give flexibility to call another service.

public interface ICapitalService
{
    List<Capital> GetAll();
}

public class LocalCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from db.
    }
}

public class RemoteCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from web service
    }
}

Someone get capitals from remote service and others may want to get from db. But you restrict it by checking type of Capital and doing your specific logic in your code. You can't add another option for Capital.

3. You need to ensure just child class type is checking.

When you check type by instanceof, it will also return true for parent class. So, you may not reach deserved code block.

if (list.get(0) instanceof Car) {
   //...
}
else if (list.get(0) instanceof Volvo)
{
  //..
}

Because of Volvo is a car, you can't reach Volvo block and you need to manage manually.

4. Software Language Limitation

You are checking List of item types by getting first item of list.(list.get(0)). If the first item of list is null, instanceof will return false and logic won't work as expected and last else block will work.

There are many countable reason why you should not to do that way. I try to explain it what's wrong and what will you need to do also if you do your way:

1. DRY

When you do that way, it's against to Don't Repeat Yourself(DRY). You copy and paste same code for each type. By a good design, you can do it just one method which doesn't need to know type of data and there will be just one place to change when needed.

2. Limitation per Type

Another reason why you shouldn't do that way, you restrict your code by one logic per type. Assume that you need to get all Capital. When you check object type in your code and do your logic, you can not give flexibility to call another service.

public interface ICapitalService
{
    List<Capital> GetAll();
}

public class LocalCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from db.
    }
}

public class RemoteCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from web service
    }
}

Someone get capitals from remote service and others may want to get from db. But you restrict it by checking type of Capital and doing your specific logic in your code. You can't add another option for Capital.

3. You need to ensure just child class type is checking.

When you check type by instanceof, it will also return true for parent class. So, you may not reach deserved code block.

if (list.get(0) instanceof Car) {
   //...
}
else if (list.get(0) instanceof Volvo)
{
  //..
}

Because of Volvo is a car, you can't reach Volvo block and you need to manage manually.

4. Software Language Limitation

You are checking List of item types by getting first item of list.(list.get(0)). If the first item of list is null, instanceof will return false and logic won't work as expected and last else block will work.

Source Link
Engineert
  • 929
  • 1
  • 6
  • 18

There are many countable reason why you should not to do that way. I try to explain it:

1. DRY

When you do that way, it's against to Don't Repeat Yourself(DRY). You copy and paste same code for each type. By a good design, you can do it just one method which doesn't need to know type of data and there will be just one place to change when needed.

2. Limitation per Type

Another reason why you shouldn't do that way, you restrict your code by one logic per type. Assume that you need to get all Capital. When you check object type in your code and do your logic, you can not give flexibility to call another service.

public interface ICapitalService
{
    List<Capital> GetAll();
}

public class LocalCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from db.
    }
}

public class RemoteCapitalService : ICapitalService
{
    public List<Capital> GetAll()
    {
        // get from web service
    }
}

Someone get capitals from remote service and others may want to get from db. But you restrict it by checking type of Capital and doing your specific logic in your code. You can't add another option for Capital.

3. You need to ensure just child class type is checking.

When you check type by instanceof, it will also return true for parent class. So, you may not reach deserved code block.

if (list.get(0) instanceof Car) {
   //...
}
else if (list.get(0) instanceof Volvo)
{
  //..
}

Because of Volvo is a car, you can't reach Volvo block and you need to manage manually.

4. Software Language Limitation

You are checking List of item types by getting first item of list.(list.get(0)). If the first item of list is null, instanceof will return false and logic won't work as expected and last else block will work.