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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:38:32+00:00 2026-05-20T15:38:32+00:00

We got in argument with co-worker about the patterns of flow control and general

  • 0

We got in argument with co-worker about the patterns of flow control and general code design. Need your opinion – which is the better/cleaner/preferred way of writing the code ?

This is for MVC 3, but that doesn’t really matter.
Version 1:

public ActionResult Login(LoginData loginData)
{
    if (!ModelState.IsValid)
    {
        ShowGlobalError("Invalid credentials.");
        return View(loginData);
    }

    UserProfile profile = null;
    try
    {
        // see if we have profile with those credentials
        profile = this.RetrieveUserProfile(loginData.Email.Trim(), loginData.Password.Trim());  // this just goes to db and tries to get user profile. Returns null if profile isn't found
    }
    catch (Exception ex)
    {
        ShowGlobalError("DB is down");
        LogError(...);
        return View(loginData);
    }


    if (profile == null)
    {
        // nope, we don't.. ask again
        ShowGlobalError("Invalid credentials.");
        return View(loginData);
    }

    // ok, we're good
    Session["Profile"] = profile;
    FormsAuthentication.SetAuthCookie(profile.Email, false);
    FormsAuthentication.RedirectFromLoginPage(profile.Email, loginData.EnablePermanentCookie);

    return View(loginData);
}

Version 2:

public ActionResult Login(Credentials credentials){
    try{
        PersonalProfile profile = AuthenticateUser(credentials);
        SetProfileSessionstate(profile);    // this does 'Session["Profile"] = profile;'
        SetFormsAuthenticationAndRedirect(profile); 
    }
    catch(Exception ex){
            ShowGlobalError("invalid login, please try again.");
    }
    return View(credentials);
}

public void SetFormsAuthenticationAndRedirect(PersonalProfile profile){
     FormsAuthentication.SetAuthCookie(profile.Email, loginData.EnablePermanentCookie);
     FormsAuthentication.RedirectFromLoginPage(profile.Email, loginData.EnablePermanentCookie);
}

Version 1 is littered with return statements, version 2 is using try/catch for flow control.
So, which is the better way, or are we both doing it wrong, and there’s a better way you can share ?

Thanks !

  • 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-20T15:38:33+00:00Added an answer on May 20, 2026 at 3:38 pm

    I like #1 much better than #2

    no.2 is lazy coding

    no.1 explicitly capture errors

    it is not a good idea to rely on exceptions to catch logic mistakes or errors
    in #2, if profile is null, you do not check that and rely on throwing exception to catch that exceptions are expensive operations in general
    and should only be relied on for unforseen logic outcomes (Exceptions!)

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

Sidebar

Related Questions

So I got into a friendly argument with a co-worker over a piece of
I am trying to refactor some code by introducing generics, and I got stuck.
ArgumentError: Error #1063: Argument count mismatch on scripts::GamePlay(). Expected 1, got 0. at flash.display::Sprite/constructChildren()
I've currently got CC.Net setup for doing builds and it's awesome. But now I
I'm learning Python and have been trying to understand more about the details of
I am trying to understand how this piece of self-replicating code works (found here
We use ClearCase at my workplace. Part of our standard process when code is
I've got a bit of a problem with making my data loading and filtering
I'm wondering if is possible pass types by argument in Java. Let me explain
This is my code: from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from

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.