Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Changing Software : Sprout Method
DevByJESUS
DevByJESUS

Posted on

     

Changing Software : Sprout Method

Hello Everybody 😄
It is me again ; we continue talking about legacy code .
Today we are going to talk about the questionI Don't have much time and I have to Change It when working with legacy.
Let's go 😜

What it costs us

We are not gonna lie , with testing developers are going to write much more additional code , yes with tests. Laura comes and sayi do not have much time and i have to write it 😒 , and Paul responds I understand you but think about it , if you do not write it now and sayi will come later , we all know that once the feature has been added no one is going to look back to that mess in the code .

😉
Look at what M. Feathers says:

The truth is, the work that you do to break dependencies and write tests for your changes is going to take some time, but in most cases, you are going to end up saving time—and a lot of frustration. When? Well, it depends on the project. In some cases, you might write tests for some code that you need to change, and it takes you two hours to do that. The change that you make afterward might take 15 minutes. When you look back on the experience, you might say, “I just wasted two hours—was it worth it?” It depends. You don’t know how long that work might have taken you if you hadn’t written the tests.

Paul : we can go now Laura ? 👀
Laura : Yes show me how to introduce this feature by being clean and cover it with test. 😒

Let's go

Paul : I will introduce to you theSprout Method
let's start 😇

Paul : small example , look here Laura

public void postEntries(List entries) {   for (Iterator it = entries.iterator(); it.hasNext(); ) {     Entry entry = (Entry)it.next();     entry.postDate();   }   transactionBundle.getListManager().add(entries);}
Enter fullscreen modeExit fullscreen mode

Paul : If the next feature isverify that none of the new entries are already in transactionBundle before we post their dates and add them
I know you Laura 😂 , the most easy solution will be to write this

Bad 💔

public void postEntries(List entries) {    List entriesToAdd = new LinkedList();    for (Iterator it = entries.iterator(); it.hasNext(); ) {     Entry entry = (Entry)it.next();     if (!transactionBundle.getListManager().hasEntry(entry)      {        entry.postDate();     entriesToAdd.add(entry);     }  }  transactionBundle.getListManager().add(entriesToAdd);}
Enter fullscreen modeExit fullscreen mode

Paul : But Laura why not cover your new code with test and make a separation with the old code and the new one ?

Laura : How ? show me 😑

Paul : The process is simple 😇

  1. Identify where you need to make your code change.

  2. If the change can be formulated as a single sequence of statements in one place in a method, write down a call for a new method that will do the work involved and then comment it out.

  3. Determine what local variables you need from the source method, and make them arguments to the call.

  4. Determine whether the sprouted method will need to return values to source method. If so, change the call so that its return value is assigned to a variable.

  5. Develop the sprout method using test-driven development

Paul : i know it can be hard the first time , but look at how the code looks like when we follow this process

Paul : First we know we can create a new method for the requested feature and why not doing it with Test Driven Development .

Paul : So the result is like below , our new code for the new requested feature , and developed with TDD.

List uniqueEntries(List entries) {   List result = new ArrayList();   for (Iterator it = entries.iterator(); it.hasNext(); ) {     Entry entry = (Entry)it.next();     if (!transactionBundle.getListManager().hasEntry(entry)        {            result.add(entry);       }   }   return result;}
Enter fullscreen modeExit fullscreen mode

Laura : But it is without value Here 😪

Paul : Yes , the interesting part is here , we can now use our newly created method in the place where we need it , in the old code .

public void postEntries(List entries) {   List entriesToAdd = uniqueEntries(entries);   for (Iterator it = entriesToAdd.iterator(); it.hasNext();     ) {        Entry entry = (Entry)it.next();        entry.postDate();     }    transactionBundle.getListManager().add(entriesToAdd);}
Enter fullscreen modeExit fullscreen mode

Laura : Hum , it seems pretty clean , and i can reuse theuniqueEntries method if there is a new feature that needs it 😏 .

Paul : You start to understand 😍

Paul : This is the Sprout Method.

Laura : Really , Thanks Paul .

Paul : you're welcome but do not thank me . All this is from the book of Michael FeathersWorking Effectively With Legacy Code.

Sprout Method : Disadvantages

Like you have seen we separate the old from the new one , and here only the new code is under test , and this is the downside of theSprout Method .

Sprout Method : Advantages

What M. Feathers says :

When you use Sprout Method, you are clearly separating new code from old code. Even if you can’t get the old code under test immediately, you can at least see your changes separately and have a clean interface between the new
code and the old code. You see all of the variables affected, and this can make it easier to determine whether the code is right in context.

Thanks for Reading 😃
Email Me
YouTube Channel
Twitter
Instagram

JESUS loves you ❤️

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I want to change the world with code by having fun with it . #keeplearning #cleancode #tdd #atdd #bdd #ddd
  • Location
    Dakar , Senegal , Africa
  • Joined

More fromDevByJESUS

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp