Linked Questions
17 questions linked to/from Where did the notion of "one return only" come from?
21
votes
7
answers
3k
views
Should a function use premature returns or wrap everything in if clauses? [duplicate]
Possible Duplicate:
Where did the notion of “one return only” come from?
Which is better? I see pros and cons for both, so I can't really decide on which one to stick to.
Wrap in if ...
2
votes
2
answers
11k
views
Python - only one return per method? [duplicate]
I'm trying to sort out whether this is just a personal preference thing or whether it's actually bad practice to do this one way or another. I did try to reference PEP8 first, and I did not see an ...
user avatar
user112358
2
votes
1
answer
937
views
What are the pros and cons of temporary variables vs multiple returns [duplicate]
Take the following examples:
public static String returnOnce() {
String resultString = null;
if (someCondition) {
resultString = "condition is true";
} else {
resultString ...
4
votes
1
answer
438
views
Should methods always return from one place? [duplicate]
Possible Duplicate:
Where did the notion of "one return only" come from?
I got into a little argument with a coworker about whether a method should be written like this:
public Thing getThing(int ...
301
votes
19
answers
245k
views
Should I return from a function early or use an if statement? [closed]
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.
public void SomeFunction(bool someCondition)
{
if (someCondition)
...
Community wiki
58
votes
11
answers
69k
views
What characteristics or features make code maintainable? [duplicate]
I used to think I knew what this was, until I really started thinking about it... "maintainable"... what exactly makes code maintainable?
To me, if code must be maintainable that means we can expect ...
23
votes
7
answers
54k
views
Avoid too complex method - Cyclomatic Complexity
Not sure how to go about this method to reduce Cyclomatic Complexity. Sonar reports 13 whereas 10 is expected. I am sure nothing harm in leaving this method as it is, however, just challenging me how ...
20
votes
7
answers
3k
views
Should "else" be used in situations where control flow renders it redundant?
I sometimes stumble upon code similar to the following example (what this function does exactly is out of the scope of this question):
function doSomething(value) {
if (check1(value)) {
return -...
10
votes
6
answers
4k
views
Why are people coding "C-style C++"?
In discussions about whether to cast the result of malloc or not when coding C, one common argument is that if you cast the result then you can compile your C code with a C++ compiler.
Why would one ...
2
votes
6
answers
1k
views
Stacking keywords on top of each other - poor style? [closed]
I have always wondered about this, especially in C/Java style languages. For example, consider the first 3 lines of this C# code:
lock (serviceLock)
using (var client = new ServiceClient())
try
{
...
3
votes
4
answers
2k
views
Alternative to nested-if on single return functions
I'd like to see if there has been any precedent on alternatives to nested-ifs--particularly for error-code returns. My workplace requires one return per function, so I cannot early exit. Here is some ...
1
vote
2
answers
925
views
What is the cleanest way of writing a function with conditional statement with many returning options?
Maybe that's simple, but I'm a little confused.
I have such a Ruby code:
def my_function(found_objects)
# ...
if found_objects.second
return CoreObjectFactory.get_object(found_objects.second,...
2
votes
4
answers
243
views
function exit condition on parameter consistency check [duplicate]
When checking for parameter consistency a the top of a function body, what is the best strategy?
This one:
protected void function(Object parameter)
if (parameter == null)
return;
...
3
votes
4
answers
2k
views
If a function contains only a switch, is it bad practise to replace the break; statements with return; statements?
Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):
bool executeSomeAction(...
1
vote
1
answer
447
views
Which statements can be considered as exit points?
There are many discussions related to whether it is better to have only one or multiple exit points per function, but it is not clear to me which statements can be considered as exit points: only ...