Skip to main content
Tweeted twitter.com/StackSoftEng/status/874958807276949504
added 4 characters in body
Source Link
Camo
  • 133
  • 1
  • 6

Sometimes when I write a class or piece of code that has to deal with parsing or processing of data, I have to ask myself, if there might be a better solution to the problem.

Example 1: In a recent exam we had to write a simluation for a carsharing service. We were given a city which was divided into several (n*n) districts. Each district worked independently and had two functions. The first one represented the amount of cars that were borrowed and the second one the amount of cars that were returned (in this specific district, per hour). Now we had to calculate some values for each district.

Obviously I had a City-class and a District-class. The question is: Where do i so the simulation part? I could write a function inside the district class or I could write a dedicated (simulator-)class which handles all the processing.

So basically:

City.Simulate()
   foreach District in City
        District.Simulate
return results

or

Simulator(City)
   Simulate
      Get all district objects
         do simulation & collect results
   return results

In the end I decided to go with the first option, but I'm not really satisfied with this approach.

Example 2: Maybe the problem gets clearer with this example. I'm currently writing an MQTT Broker and am currently working on the packet parser. There is one class for each packet type (Connect, Publish, Subscribe, etc.). Where do I do the parsing?

For this project I made a dedicated class for parsing which has a function for each type. However in a previous (similar) project, each packet class provided a custom parsing function.


So a more general question would be: At which point do I outsource the processing of data/to which point do I process the data inside the class itself?So a more general question would be: At which point do I outsource the processing of data/to which point do I process the data inside the class itself?

Sometimes when I write a class or piece of code that has to deal with parsing or processing of data, I have to ask myself, if there might be a better solution to the problem.

Example 1: In a recent exam we had to write a simluation for a carsharing service. We were given a city which was divided into several (n*n) districts. Each district worked independently and had two functions. The first one represented the amount of cars that were borrowed and the second one the amount of cars that were returned (in this specific district, per hour). Now we had to calculate some values for each district.

Obviously I had a City-class and a District-class. The question is: Where do i so the simulation part? I could write a function inside the district class or I could write a dedicated (simulator-)class which handles all the processing.

So basically:

City.Simulate()
   foreach District in City
        District.Simulate
return results

or

Simulator(City)
   Simulate
      Get all district objects
         do simulation & collect results
   return results

In the end I decided to go with the first option, but I'm not really satisfied with this approach.

Example 2: Maybe the problem gets clearer with this example. I'm currently writing an MQTT Broker and am currently working on the packet parser. There is one class for each packet type (Connect, Publish, Subscribe, etc.). Where do I do the parsing?

For this project I made a dedicated class for parsing which has a function for each type. However in a previous (similar) project, each packet class provided a custom parsing function.


So a more general question would be: At which point do I outsource the processing of data/to which point do I process the data inside the class itself?

Sometimes when I write a class or piece of code that has to deal with parsing or processing of data, I have to ask myself, if there might be a better solution to the problem.

Example 1: In a recent exam we had to write a simluation for a carsharing service. We were given a city which was divided into several (n*n) districts. Each district worked independently and had two functions. The first one represented the amount of cars that were borrowed and the second one the amount of cars that were returned (in this specific district, per hour). Now we had to calculate some values for each district.

Obviously I had a City-class and a District-class. The question is: Where do i so the simulation part? I could write a function inside the district class or I could write a dedicated (simulator-)class which handles all the processing.

So basically:

City.Simulate()
   foreach District in City
        District.Simulate
return results

or

Simulator(City)
   Simulate
      Get all district objects
         do simulation & collect results
   return results

In the end I decided to go with the first option, but I'm not really satisfied with this approach.

Example 2: Maybe the problem gets clearer with this example. I'm currently writing an MQTT Broker and am currently working on the packet parser. There is one class for each packet type (Connect, Publish, Subscribe, etc.). Where do I do the parsing?

For this project I made a dedicated class for parsing which has a function for each type. However in a previous (similar) project, each packet class provided a custom parsing function.


So a more general question would be: At which point do I outsource the processing of data/to which point do I process the data inside the class itself?

Source Link
Camo
  • 133
  • 1
  • 6

Design patterns for processing/manipulating data

Sometimes when I write a class or piece of code that has to deal with parsing or processing of data, I have to ask myself, if there might be a better solution to the problem.

Example 1: In a recent exam we had to write a simluation for a carsharing service. We were given a city which was divided into several (n*n) districts. Each district worked independently and had two functions. The first one represented the amount of cars that were borrowed and the second one the amount of cars that were returned (in this specific district, per hour). Now we had to calculate some values for each district.

Obviously I had a City-class and a District-class. The question is: Where do i so the simulation part? I could write a function inside the district class or I could write a dedicated (simulator-)class which handles all the processing.

So basically:

City.Simulate()
   foreach District in City
        District.Simulate
return results

or

Simulator(City)
   Simulate
      Get all district objects
         do simulation & collect results
   return results

In the end I decided to go with the first option, but I'm not really satisfied with this approach.

Example 2: Maybe the problem gets clearer with this example. I'm currently writing an MQTT Broker and am currently working on the packet parser. There is one class for each packet type (Connect, Publish, Subscribe, etc.). Where do I do the parsing?

For this project I made a dedicated class for parsing which has a function for each type. However in a previous (similar) project, each packet class provided a custom parsing function.


So a more general question would be: At which point do I outsource the processing of data/to which point do I process the data inside the class itself?