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

  • Home
  • SEARCH
  • 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 8852593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:26:24+00:00 2026-06-14T13:26:24+00:00

Consider a batch operation that may or may not take long time to do

  • 0

Consider a batch operation that may or may not take long time to do its job (depends on data).
A user can register an OPTIONAL listener for tracking job progress.

NOTE: listener registration is totally optional and user may want to
call the job without registering any listener.

Q: Which of the following solutions are your preference and why?

EDIT: the concern here is performance vs clean code. some say, checking null reference (solution 1) is faster compare to second solution. but second solution is cleaner and more understandable. I would like to have your opinion.

No 1: Allow null listener and always check if listener is not null, then call it.

doMyBatchJob() {
   if (listener != null) {
      listenr.progressStarted(params);
   }
   while (x) {
      if (listener != null) {
          listener.progressUpdated(current, expected)
      }
   }
   if (listener != null) {
      listenr.progressFinished(params);
   }
}

No 2: Implement a dummy listener, and register it if user didn’t pass his/her own listener.
so that can call listener without checking for null object.

DummyListener {
     public void progressStarted(params) { //DO NOTHING }
     public void progressUpdated(current, expected) { //DO NOTHING }
     public void progressFinished(params) { //DO NOTHING }
}

doMyBatchJob() {
   listener.progressStarted(params);
   while (x) {
         //Do a single unit of the batch operation
         // ... code omitted here
         listener.progressUpdated(current, expected)
   }
   listener.progressFinished(params)
}
  • 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-06-14T13:26:26+00:00Added an answer on June 14, 2026 at 1:26 pm

    You are correct in your concern that if x==null is a code smell, it definitiely is!

    There are very valid reasons for using the Null Object pattern, one is to keep from littering your code with if (x == null) noise that is usually less business related and more poor design related. NULL means an absence of a value not a default value.

    I don’t think you are taking the Null Object pattern far enough.

    Never return null and you never have to check for it

    First off never return null from a method and never have if x == null in your code. Both are sure signs of poor design. null references and NPE should be an error that should be resolved where it doesn’t happen.

    Never accept null and you never have to check for it.

    Have methods that return null return a Null Object and have things that accept null and might process null references have Null Object implemenations to process the Null References.

    In your case your Dummy object would not just not do anything, it should report to the log warnings that it has encountered a null and something should be done about it.

    Use a good JSR305 library to annotate your methods so that they dont’ accept nulls.

    I have the com.google.code.findbugs maven dependency in every Java program I create, no matter how trivial, then I can decorate every method and method parameter with @NONNULL and not have to worry about writing if x == null ever again!

    If you have 3rd party code that returns null wrap it and use the JSR305 annotations.

    Use the Guava Preconditions.checkNotNull() with a static import so you can do checkNotNull() on all parameters that are marked @Nonnnull, you can even include a descriptive error message about what was, null and why it should not be or whatever.

    And smugly think about how poorly designed their code is.

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

Sidebar

Related Questions

Assume that I have some array of data (a vector to be specific). Can
Consider this: string test = ""; somestring.ToList().Take(50).Select( delegate(char x) { test += x; return
Consider this query: SELECT table1.id, table1.review, table1.time, table2.author, table2.title FROM table1, table2 WHERE table1.id
Consider the code below. I do not want to create multiple instances of class
Let's consider this FOR loop in a Windows batch script: D:\MiLu\Dev\C++\temp :: type string.bat
Consider the following code: char* p = new char[2]; long* pi = (long*) p;
Consider that we are given an isometric grid (consider something like Diablo) of tiles.
Consider an article inside a div that has many <p> tag. Is there any
Consider simple case where user is deleting a post. This is simple HTTP DELETE/POST
Consider this code: byte b=1; long l=1000; b += l; I would expect the

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.