Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:08:49+00:00 2026-05-27T19:08:49+00:00

I have a method where I want to factor out some code into its

  • 0

I have a method where I want to factor out some code into its own method
This is what I have:

public class TD0301AssignmentForm extends Form {  
 public TD0301AssignmentForm(TD0301AssignmentDAO dao, STKUser authenticatedUser) {
    this.dao = dao;
    this.authenticatedUser = authenticatedUser;
 }

  public Object insert(HttpServletRequest request) {
    TD0301Assignment tdas = new TD0301Assignment();
    TD0301Assignment tdas_orig = null;
    Date dateNow = new Date();

    try {
        // Get the inuput from HTML form
        tdas.setCalc_num(FormUtil.getFieldValue(request, FIELD_CALC_NUM));
        processDate(request, tdas);
        tdas.setCalc_dept(FormUtil.getFieldValue(request, FIELD_CALC_DEPT));
        tdas.setYear_oi(Integer.toString(DateUtil.getIntYear(dateNow)));
        processCalcSafetyRequirements(request, tdas);
        ...etc...
        if (isSucces()) {
             // Instantiate a base work flow instance!
             WorkflowInstance wfi = new WorkflowInstance();
             WorkflowInstanceDAO wfiDAO = new WorkflowInstanceDAO();
             wfi.setWorkflow_class_id(tdas.getCalc_level());
             wfi.setStarted_by(authenticatedUser.getBadge());
             wfi.setStatus("0");
             wfi.setLast_date(dateNow);
             // Insert the WorkFlowInstance into the database, db sets returned sequence number into the wfi object.
             wfiDAO.insert(wfi, authenticatedUser);

             // Insert the TD0301Assignment into the db
             tdas.setWorkflow_instance_id(wfi.getWorkflow_instance_id());
          } 

I’d like to remove the WorkflowInstance code out into its own method (still in this Class) like this:

        if (isSucces()) {
            insertWorkFlowInstance(request, tdas);
            tdas.setWorkflow_instance_id(wfi.getWorkflow_instance_id());

but wfi is now marked by Eclipse as not available. Should I do something like this to fix the error so that I can still get the wfi.getWorkflow_instance_id() in the isSuccess block above? I know it removes the error, but I am trying to apply best practices.

public class TD0301AssignmentForm extends Form {
 private WorkflowInstance wfi = new WorkflowInstance();
 private WorkflowInstanceDAO wfiDAO = new WorkflowInstanceDAO();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:08:50+00:00Added an answer on May 27, 2026 at 7:08 pm

    Instance variables (“properties” or “fields”) are not necessarily the way to go if they’re not used throughout the entire class.

    Variables should have the smallest scope possible–this makes code easier to reason about.

    With some noise elided, and also guessing, it seems like the WorkflowInstance and WorkflowInstanceDao could be localized (names changed to match Java conventions):

    public class TD0301AssignmentForm extends Form {  
    
        public Object insert(HttpServletRequest request) {
            TD0301Assignment tdas = new TD0301Assignment();
    
            try {
                tdas.setCalcNum(FormUtil.getFieldValue(request, FIELD_CALC_NUM));
                processDate(request, tdas);
    
                tdas.setCalcDept(FormUtil.getFieldValue(request, FIELD_CALC_DEPT));
                tdas.setYearOi(Integer.toString(DateUtil.getIntYear(dateNow)));
                processCalcSafetyRequirements(request, tdas);
    
                if (isSuccess()) {
                    WorkflowInstance wf = buildWorkflow(tdas);
                    tdas.setWorkflowInstanceId(wf.getId());
                }
            }
        }
    
        private buildWorkflow(TD0301Assignment tdas) {
            WorkflowInstance wfi = new WorkflowInstance();
            wfi.setWorkflowClassId(tdas.getCalcLevel());
            wfi.setStartedBy(authenticatedUser.getBadge());
            wfi.setStatus("0");
            wfi.setLastDate(new Date());
    
            WorkflowInstanceDao wfiDao = new WorkflowInstanceDao();
            wfiDao.insert(wfi, authenticatedUser);
        } 
    
    }
    

    Whether or not this is appropriate depends on how/if the WorkflowInstance is used in the rest of the method snippet you show. The DAO is almost certainly able to be localized.

    As methods become smaller and easier to think about, they become more testable.

    For example, buildWorkflow is almost easy to test, except that the DAO is instantiated “manually”. This means that testing the method will either (a) depend on having a working DAO layer, or (b) it must be mocked by a framework that can mock static utility methods (several can).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method on an interface that looks like this and I want
Say I have some special class, WrappedDataTable , and I want to associate each
I'm trying to figure out the best way to unit test this class: public
I have this factory method in java: public static Properties getConfigFactory() throws ClassNotFoundException, IOException
I have a generic object factory FactoryBase<T> with a factory method: public abstract class
I have a method I want to call using AJAX in rails but I
I have a method I want to import from a DLL and it has
Eg. I have following delegate method I want to use as a callback function
Say I want to have a method that takes any kind of number, is
I have a method that where I want to redirect the user back to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.