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 7716961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:45:48+00:00 2026-06-01T02:45:48+00:00

We usually put unnecessary checks in our business logic to avoid failures. Eg. 1.

  • 0

We usually put unnecessary checks in our business logic to avoid failures.

Eg.

1. public ObjectABC funcABC(){

      ObjectABC obj = new ObjectABC;
     ..........
     ..........
     //its never set to null here.
     ..........
     return obj; 
} 

ObjectABC o = funABC();

if(o!=null){
//do something
}

Why do we need this null check if we are sure that it will never be null?
Is it a good practice or not?

2. int pplReached = funA(..,..,..);
   int totalPpl   = funB(..,..,..);

   funA() just puts a few more restriction over result of funB().


    Double percentage = (totalPpl==0||totalPpl<pplReached) ? 0.0 : pplReached/totalPpl;

Do we need 'totalPpl<pplReached' check?

The questions is: Aren’t we swallowing some fundamental issue by putting such checks? Issues which should be shown ideally, are avoided by putting these checks.

What is the recommended way?

  • 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-01T02:45:49+00:00Added an answer on June 1, 2026 at 2:45 am

    Think about your audience. A check is worthwhile when it

    1. helps you, the programmer, detect errors,
    2. helps other programmers detect errors where their code meets yours,
    3. allows the program to recover from bad input or an invalid state, or
    4. helps a maintainer avoid introducing errors later.

    If your null check above does not fall into these, or there is a simpler mechanism which would do the same, then leave it out.

    Simpler mechanisms often include,

    1. unit tests.
    2. annotations that communicate intent to the reader and can be checked by findbugs or similar tools
    3. asserts that cause code to fail early, and communicate intent without requiring you to put in error handling code that should never be reached and without confusing code coverage tools
    4. documentation or inline comments

    In this case, I would suggest adding an annotation

    public @Nonnull ObjectABC funcABC(){
    

    integrating findbugs into your build process, and maybe replacing

    if(o!=null){
    //do something
    }
    

    with

    assert o != null: "funcABC() should have allocated a new instance or failed."
    

    Aren’t we swallowing some fundamental issue by putting such checks?

    As a rule of thumb,

    1. unit tests are good for checking the behavior of a small piece of code. If you can’t write unit tests for important functions, then the fundamental issue is that you aren’t writing testable code.
    2. annotations are good for conveying intent to code reviewers, maintainers, and automated tools. If you haven’t integrated those tools into your process, then the fundamental issue is that you aren’t taking advantage of available code quality tools.
    3. asserts are good for double-checking your assumptions. If you can’t sprinkle asserts into your code and quickly tell which are being violated then your fundamental problem is that you don’t have a quick way to run your code against representative data to shake out problems.
    4. documentation and inline comments (including source control comments) are good for spreading knowledge about the system among the team — making sure that more than one person on the team can fix a problem in any part of the code. If they are constantly missing or out-of-sync, then the underlying problem is that you are not writing code with maintainers in mind.

    Finally, design by contract is a programming methodology that many have found useful for business logic code. Even if you can’t convince your team to adopt the specific tools and practices, reading up on DbC might still help you reason and explain how to enforce the important invariants in your codebase.

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

Sidebar

Related Questions

I'm starting a new django project and as usually i put south on my
I usually put MAMP MySQL /db/db-site-name folder under SVN. When MAMP starts, it gives
Can some body tell me the reason why we usually put const and &
I have a unit test project based on UnitTest++. I usually put a breakpoint
When I first started with Javascript, I usually just put whatever I needed into
I'm working through some tutorials and examples of java.util.concurrent package. Usually example authors put
Usually when I build a site, I put all the CSS into one file,
While doing performance testing on windows, i usually use perfmon. But when i put
Normaly if I make a small code snipet, I usually put it in a
Usually I can put an xml-file (of non-android format, the same would be if

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.