title: Kafka Streams - Integral versus Separable handler for flatMapValues
body: <p>I would like help deciding one of two paths I can follow from those more experienced with Kafka Streams in JAVA. I have two working JAVA apps that can take an inbound stream of integers and perform various calculations and tasks, creating four resultant outbound streams to different topics. The actual calc/tasks is not important, I am concerned
  with the two possible methods I could use to define the handler that performs the math and any associated risks with my favorite.</p>
  
  <p><strong>Method 1</strong> uses a separately defined function that is of type <strong><em>Iterable</em></strong> and returns a <strong><em>List</em></strong> type.</p>
  
  <p><strong>Method 2</strong> uses the more common integral method that places the function within the <strong><em>KStream</em></strong> declaration.</p>
  
  <p>I am very new to Kafka Streams and do not want to head down the wrong path. I like Method 1 because the code is very readable, easy to follow, and can have the handlers tested offline without needing to invoke traffic with streams. </p>
  
  <p>Method 2 seems more common, but as the complexity grows, the code gets polluted in main(). Additionally I am boxed-in to testing algorithms using stream traffic, which slows development.</p>
  
  <h2>Method 1: Separable handlers (partial):</h2>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>// Take inbound stream from math-input and perform transformations A-D, then write out to 4 streams.
  KStream&lt;String, String&gt; source = src_builder.stream("math-input");
  source.flatMapValues(value -&gt;  transformInput_A(Arrays.asList(value.split("\\W+")))  ).to("math-output-A");
  source.flatMapValues(value -&gt;  transformInput_B(Arrays.asList(value.split("\\W+")))  ).to("math-output-B");
  source.flatMapValues(value -&gt;  transformInput_C(Arrays.asList(value.split("\\W+")))  ).to("math-output-C");
  source.flatMapValues(value -&gt;  transformInput_D(Arrays.asList(value.split("\\W+")))  ).to("math-output-D");
  
  // More code here, removed for brevity.
  
  // Transformation handlers A, B, C, and D.
  // ******************************************************************
  // Perform data transformation using method A
    public static Iterable transformInput_A (List str_array) {
         
         // Imagine some very complex math here using the integer
         // values. This could be 50+ lines of code.
        
         for (int i = 0; i &lt; str_array.size(); i++) {
           // grab values and perform ops
         }
  
        // Return results in string format
        return math_results;
      }
  
  // End of Transformation Method A
  // ******************************************************************
  // Imagine similar handlers for methods B, C, and D below.</code></pre>
  </div>
  </div>
  </p>
  
  <h2>Method 2: Handlers internal to KStream declaration (partial):</h2>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> // Take inbound stream from math-input and perform transformations A-D, then write out to 4 streams.
    KStream&lt;String, String&gt; inputStream = src_builder.stream("math-input");
    KStream&lt;String, String&gt; outputStream_A = inputStream.mapValues(new ValueMapper&lt;String, String&gt;() {
       @Override
       public String apply(String s) {
  
         // Imagine some very complex math here using the integer
         // values. This could be 50+ lines of code.
  
         for (int i = 0; i &lt; str_array.length; i++) {
           // grab values and perform ops
         }
  
         // Return results in Iterbale string format
         return math_results;
       }
    });
  
  
    // Send the data to the outbound topic A.
    outputStream_A.to("math-output-A");
  
    KStream&lt;String, String&gt; outputStream_B ....
    // Use ValueMapper in the KStream declaration just like above. 50+ lines of code
    outputStream_B.to("math-output-B");
  
    KStream&lt;String, String&gt; outputStream_C ....
    // Use ValueMapper in the KStream declaration just like above. 50+ lines of code
    outputStream_C.to("math-output-C");
  
    KStream&lt;String, String&gt; outputStream_D ....
    // Use ValueMapper in the KStream declaration just like above. 50+ lines of code
    outputStream_D.to("math-output-D");</code></pre>
  </div>
  </div>
  </p>
  
  <p>Other than my desire to keep main() neat and push the complexity out of view, am I heading in the wrong direction with Method 1?</p>