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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:39:28+00:00 2026-05-17T17:39:28+00:00

When writing a function I always have this confusion whether to check for errors

  • 0

When writing a function I always have this confusion whether to check for errors first and declare the variables later (or) assign the parameters to local variables and then check for errors. Which of the following way is preferred and why? I usually stick to the first type.

void DoSomething1(Object x, Object y){

  // All sort of error checking goes here
  if IsError(x) return;
  if IsError(y) return;

  // Variable declaration
  int i,j;
  Object z = x;
}


void DoSomething2(Object x, Object y){

  // Variable declaration
  int i,j;
  Object z = x;

  // All sort of error checking goes here
  if IsError(z) return;
  if IsError(y) return;

}
  • 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-17T17:39:29+00:00Added an answer on May 17, 2026 at 5:39 pm

    You should follow a proximity rule and declare the variables as late as possible. This localises their creation and use. You should also check parameters for validity at the earliest possible opportunity to minimise the work performed.

    Hence I agree that your first one is better but it is subjective. There’s possibly arguments for the other approach but I’ve yet to hear convincing ones, so I consider those two guidelines as best practice.

    Since you state “language agnostic” despite the fact your code looks somehow strangely familiar :-), there are almost certainly some languages where you don’t get a choice and variables have to be declared at the top.

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

Sidebar

Related Questions

I have a very simple function which takes in a matching bitfield, a grid,
In CodeIgniter, I'm writing an XML file using write_file function. write_file('data.xml', $str_xml); But the
This is an erlang problem, it seems. I have this code to test the
I have a circular, statically allocated buffer in C, which I'm using as a
When writing code in Java, it is very helpful to embrace composition and dependency
I have the following VB.NET code (but for each loops are in most languages,
I am trying to understand windows hooks by writing a few keyboard hooks. I
I am writing an interface to a sorted collection of objects. As is the
Hy, I'm writing a class which should help me in unit tests. The class
I had some code before I moved to Unicode and Delphi 2009 that appended

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.