Linked Questions

10 votes
6 answers

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 ...
1 vote
1 answer

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 ...
3 votes
4 answers

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

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,...
20 votes
7 answers

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 -...
302 votes
19 answers

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) ...
4 votes
2 answers

Return type declaration and checks

I've been mainly programming PHP, and I recently started with C++. In PHP the return of a function can be of any type, so you can do checks like this: public function doSomething() { if (! this-&...
-1 votes
2 answers

Single exit of function uses goto

Apropos of What kind of bugs do "goto" statements lead to? Are there any historically significant examples? I am not that learned in C, and to me the puzzle is that a single exit of a ...
2 votes
4 answers

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(...
23 votes
7 answers

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 ...
59 votes
11 answers

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 ...
2 votes
2 answers

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 ...
2 votes
6 answers

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 { ...
2 votes
4 answers

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; ...
2 votes
1 answer

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 ...

15 30 50 per page