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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:33:36+00:00 2026-05-23T21:33:36+00:00

I am using Python for my example, but my question is referring to programmming

  • 0

I am using Python for my example, but my question is referring to programmming languages in general.

def some_function(eggs):
    if eggs == 1:
        do_something_1()
    elif eggs == 2:
        do_something_2()
    elif eggs == 3:
        do_something_3()
    else:
        do_error()
        return
    do_something_4()
    do_something_5()
    do_something_6()

(This is just an example. My functions will not be called do_something_x.)

Would putting a return in the else like this be a bad programming practice? Would it be a better idea to put

do_something_4()
do_something_5()
do_something_6()

in each of the if/elifs?

  • 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-23T21:33:36+00:00Added an answer on May 23, 2026 at 9:33 pm

    The main issue I see with your code is that the error case is hidden more than half way down the function body. It makes the code difficult to read. Since what you are doing is validating the arguments to the function, you should do that first.

    My preference in the case of an invalid argument is to raise an appropriate exception such as ValueError. Without knowing what your function does, or what do_error does, it’s hard to say with absolute certainty that this applies to your case. But generally, getting incorrect arguments isn’t something a function can recover from. The caller gave the arguments; so put the onus on the caller to recover from that error.

    Also, here’s an idiom you can use to avoid long lists of elifs:

    funs = {1: do_something_1,
            2: do_something_2,
            3: do_something_3}
    funs[eggs]()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this question I've learned how to google-search using Python 3. But in the
Does anyone have a good example of using the HeaderParser class in Python for
We're considering using Python (IronPython, but I don't think that's relevant) to provide a
I'm thinking of switching from using PHP to Python for web applications, but I
I am using the Python code at the bottom of this question to send
Question: Is there a way to make a function object in python using strings?
Consider these examples using print in Python: >>> for i in range(4): print('.') .
Using Python I want to be able to draw text at different angles using
Using python 2.4 and the built-in ZipFile library, I cannot read very large zip
Using Python, how would I go about reading in (be from a string, file

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.