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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:05:31+00:00 2026-05-16T02:05:31+00:00

I was just inquiring about standard practices of argument validation syntax. I’m working with

  • 0

I was just inquiring about standard practices of argument validation syntax. I’m working with PHP as of currently, but anything that’s considered a platform/language agnostic solution would be great.

As of currently, I’m aware of two pretty common syntax. For example (my preference):

function foo($a, $b, $c){
    if(!$a){ throw new Exception(); }
    if($b < 0){ throw new Exception(); }
    if(!check($c)){ throw new Exception(); }

    //do stuff with $a, $b, and $c

}

And alternatively:

function foo($a, $b, $c){
    if($a){
        if($b >= 0){
            if(check($c)){

                //do stuff with $a, $b, and $c

            }else{ throw new Exception(); }
        }else{ throw new Exception(); }
    }else{ throw new Exception(); }
}

In any case, if anyone knows of documentation that illustrates any sort of standards, or common practices regarding this, I would greatly appreciate a reference.

Of course, personal preferences and opinions are more then welcome, but reasoning for your choices would be an asset.

  • 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-16T02:05:32+00:00Added an answer on May 16, 2026 at 2:05 am

    I prefer the former example you give for a number of reasons:

    • Code doesn’t get unnecessarily indented as it does with nested if statements.
    • Nested if statements can add a heap of complicated branches to the logic flow which become hard to manage
    • Pre-conditions get moved down lower by nature of if statements. I prefer to have all pre-conditions immediately at the start of the method

    Design by contract is the general approach to ensuring certain conditions are met (usually through assertions) such as:

    Assert.IsNotNull($a, '$a must not be null');
    

    If the assertion fails, an exception is thrown with the specified message.

    Using the same approach you can make certain assertions at the end of the method (post-conditions) to ensure the method executed as expected.

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

Sidebar

Related Questions

Just looking for the first step basic solution here that keeps the honest people
Just how much slower are events? I have written a streaming XML parser (that
Just what the title says, I need to change the password for an existing
just a quick question: I am a CS undergrad and have only had experience
Just bought a 2.4GHz Intel Core 2 Duo iMac with 2GB of memory and
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview
Just installed the latest SDK for iPhone 2.1. When I go to File ->
Just like it reads.
Just found this out, so i am answering my own question :) Use a

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.