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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:40:39+00:00 2026-05-14T03:40:39+00:00

Ok this might be a bit of hack but bear with me :) The

  • 0

Ok this might be a bit of hack but bear with me 🙂 The background is that i’m tired of methods that that some if-statement to that messes up indention for the whole method, like:

public SomeClass DoStuff(string inputStr)
{
  SomeClass result =null;
  if (IsOpenFilter(inputStr))
  {
    ....
  }

  return result;
}

So I was thinking , wouldn’t it be neat if I could do something like this instead:

public SomeClass DoStuff(string inputStr)
{
  Require(IsOpenFilter(inputStr),null);


  ....

  return result;
}

This case might be covered by code contracts in some form, if so please correct me 🙂

The idea is that if the statement does not evaluates to true it would return null. If there wasn’t a return type for the method it would simply be: Require(IsOpenFilter(inputStr));

So I guess there’s two questions, can this be done somehow? I’m stumped on how to do a conditional return from calling a method.

The other question is, is this a good idea? It’s a bit weird to monkeypatch the language like this but I’d rather like the way the code reads. I would be even cleaner if it could be put into an attribute above the method: [Require(IsOpenFilter(inputStr))]

  • 1 1 Answer
  • 1 View
  • 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-14T03:40:39+00:00Added an answer on May 14, 2026 at 3:40 am

    I agree with Anders: invert the condition and return early. “Single return point” used to be valuable when you needed to explicitly do a bunch of resource clean-up at that return point; with finally blocks, using statements and garbage collection it’s not even a useful guideline any more, IMO. Any time you find yourself doing something just to get a single return point, consider whether it’s actually making the code more readable.

    I almost always find that if at some point in the code I know the return value and there are no other actions I want to take within the method, the best thing is to return immediately. Forcing readers to look through the rest of the method just in case there’s another action is neither elegant nor useful, IMO.

    I think it’s a good thing that a method can’t indicate that it should cause the caller to return – that ability sounds like a readability nightmare. Having said that, I have considered a new sort of conditional return statement:

    return? expression;
    

    This would return the evaluateed value of expression if it’s non-null; you could use it for non-nullable types like this:

    int Foo()
    {
        return? Bar(); // Where Bar() has a return type of int?
        ...
    }
    

    Basically it would be the return statement equivalent of the null-coalescing operator.

    This wouldn’t help in your case, mind you – you want to return null early…

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

Sidebar

Related Questions

This might be a bit tricky setup but... I have this Silverlight project that
This might be a bit complicated, but bear with me. I have a Windows
Bear with me—this might be a bit confusing! I have a javascript that adds
This might be a bit cheeky but I would like some advice. I'm new
This might sound a bit of an odd question but I know what I
This might seem a bit weird, but since I am new in RoR and
I know this might be a bit awkward but I am trying to modify
This might be a little bit odd question, but Im trying to figure out
This might be a little bit picky, but in the iPad SplitViewController setup, there
This might seem like a trivial question, but I'm a bit muddled in my

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.