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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:14:34+00:00 2026-05-31T09:14:34+00:00

Sorry about the confusing title. I recently started doing this in my project, and

  • 0

Sorry about the confusing title.

I recently started doing this in my project, and am wondering if it’s more efficient, or not, and if it’s a terrible style to practice.

Here’s an example from a Database interface:

def register(self, user, pw):
    """Register user/pw into the database"""
    if self.isStarted():
        raise Exceptions.Started
    hashed = hashlib.sha512(pw).hexdigest()
    self._db_cur.execute('''INSERT INTO PLAYERS (name, password)
                                    values (?, ?)''', [user, hashed])
    self._db.commit()

I do it here with raising an exception, but I’ve done it in other places with a return.

I feel this allows the false-cases to exit the function at the top, instead of continuing down the function, seeing if there is any more code for them to run.

I rarely see this in code I look at: is this a bad practice, or does it not yield any performance like I imagine it to?

To help clarify, what I’m used to is:

if (somethingTrue):
    runThis()
    thisToo()
    x = andThis()
    return x
return None

and what I’ve started to do, and am iffy on:

if (!somethingTrue):
    return None
runThis()
thisToo()
x = andThis()
return x

The latter seems to give the impression (especially in functions longer than 4 lines) that the code isn’t part of a conditional, when it’s intended that way. This also makes it look nicer, while adhering to PEP-8, so I’m really in a toss-up about it.

I have a feeling this breaks something horribly sacred. Is this alright, or sacrilegious?

  • 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-31T09:14:36+00:00Added an answer on May 31, 2026 at 9:14 am

    One part of the question is about style and best practice – therefore IMHO is no ‘correct’ way.

    In my opinion the nested version (no direct return) comes from ‘historic’ programming languages like ‘C’ where the whole cleanup is done once at the right place. Artificial example to show the point only:

    int f() {
       int result = 1;
       char * buffer = (char *)malloc(77);
       if(buffer!=NULL) {
          int const fd = open("/tmp/data.log", O_RDONLY);
          if(fd!=-1) {
             ssize_t const read_cnt = read(fd, buffer, 77);
             if(read_cnt!=77) {
                /* Do something: was not possible to read 77 bytes. */
                result = 0;
             }
             close(fd);
          }
          free(buffer);
       }
       return result;
    }
    

    Here it is not correct to return from other lines than the last one – because then there might be a resource leak.

    When using only objects which are destroyed completely in their destructor – or when there is no need to clean up resources (because none were allocated), I prefer the ‘short’ path return. This make things clearer like: if the preconditions for a function is not meet, there is no way to ‘really’ execute the function body. Also: you don’t need that much indentation and it is easier to read.

    Performance: I made some tests; I was not able to measure a difference between the two ways. IMHO if you need to tune performance at this level, you might want to think about choosing another programming language. 😉

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

Sidebar

Related Questions

First of all, sorry about that title. I'm not the best at writing those
Sorry for the confusing title of the question. I am uncertain about how should
Sorry about the confusing title, I had a hard time describing the problem. I
Sorry about the confusing title. Are there any web IDEs available that can be
Sorry about that confusing title :) I have a resource, ComatosePage (used in the
Well, sorry about the confusing title but I'm having a slightly annoying problem with
Sorry about the title. I do realize it's not very descriptive. :| Here is
Sorry about a vague title, but here's what i am trying to do. I
Sorry about the title, I couldn't think of a better way to describe the
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have

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.