DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

How to Bring Pseudo Code to Life

Zemian Deng user avatar by
Zemian Deng
·
May. 05, 13 · Interview
Like (0)
Save
Tweet
Share
5.71K Views

Join the DZone community and get the full member experience.

Join For Free

When designing a piece of software, I like to start with some pseudo code first, then work my way out. I tend to try mapping out the normal workflow (one that runs without any corner cases) with high level of actions as series of steps. Then the detailed implementation of the code can be filled in at later time.

Now with Java, you can easily do this with pseudo code, bring it to life, and still make it very maintainable. Try this out.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public class PseudoCodeToLife {
    public static void main(String[] args) {
        CreditCardProcessor creditCardProcessor = new CreditCardProcessor()
                .step(webServiceRequest())
                .step(validateCardNumber())
                .step(validateSecurityCode())
                .step(checkAvailableCredit())
                .step(chargeCard())
                .step(webServiceResponse());

        creditCardProcessor.run();
    }
}

Now that's as high level as you can get, but it still let me fill in the implementation without losing the overall requirement flow. To implement such workflow, we need a contract that allow processor to move data from one step to another, and yet it needs to perform some action along the way. How about an interface like this.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public interface StepAction {
    public void onAction(Exchange exchange);
}

Next is how to wire steps together. For now we can just do it inside CreditCardProcessor, but you can easily abstract that into a re-usable base class.

package deng.javademo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zemian Deng
 */
public class CreditCardProcessor {
    List<StepAction> steps = new ArrayList<StepAction>();

    public CreditCardProcessor step(StepAction stepAction) {
        steps.add(stepAction);
        return this;
    }

    public void run() {
        Exchange exchange = new Exchange();
        for (StepAction step : steps) {
            step.onAction(exchange);
        }
    }
}

Pretty straight forward. Here I used an Exchange as message, passing between each steps to allow the workflow to exchange data. You can wrap just about any real payload and/or add a Map of header properties.

Now what we are missing is just filling in each step of actions. Since we have an explicit step name when defining the workflow, we can just generate these method and fill in the actions per step required. I will add them all inside the PseudoCodeToLife and reprint it here.

package deng.javademo;

/**
 * @author Zemian Deng
 */
public class PseudoCodeToLife {
    public static void main(String[] args) {
        CreditCardProcessor creditCardProcessor = new CreditCardProcessor()
                .step(webServiceRequest())
                .step(validateCardNumber())
                .step(validateSecurityCode())
                .step(checkAvailableCredit())
                .step(chargeCard())
                .step(webServiceResponse());

        creditCardProcessor.run();
    }

    private static StepAction webServiceRequest() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("webServiceRequest step.");
            }
        };
    }

    private static StepAction validateCardNumber() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("validateCardNumber step.");
            }
        };
    }

    private static StepAction validateSecurityCode() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("validateSecurityCode step.");
            }
        };
    }

    private static StepAction checkAvailableCredit() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("checkAvailableCredit step.");
            }
        };
    }

    private static StepAction chargeCard() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("chargeCard step.");
            }
        };
    }

    private static StepAction webServiceResponse() {
        return new StepAction() {
            @Override
            public void onAction(Exchange exchange) {
                System.out.println("webServiceResponse step.");
            }
        };
    }
}


When designing a piece of software, I like to start with some pseudo code first, then work my way out.

That's all to this. What do you think? I think it's a simple way to tackle business problems. Simple software make less bugs and easier to maintain.

Published at DZone with permission of Zemian Deng, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Spring Cloud Kubernetes
  • Shift-Left: A Developer's Pipe(line) Dream?
  • 5 Steps for Getting Started in Deep Learning
  • OpenVPN With Radius and Multi-Factor Authentication

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: