When is it better to use switch/case over if statements? [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a tendency to use switch statements if I am creating a menu driven program, and I tend to use if statements when I only have a few items. I believe this has to do with the way I was taught in school, but I don't know if that is necessarily the way to go.
Are there vast differences between the two? When should you pick one over the other?
Edit: I should specify, I am mainly concerned with optimization (even if one or the other is only marginally more efficient).

If statements look like if statements. Switch statements look like switch statements. Some compilers may be mildly better at optimizing certain types of switch statements than the equivalent set of if statements, though that won’t be a significant factor in your situations. In cases where the two are both applicable, there are few practical concerns in choosing one over the other.
Use whichever fits your intended coding style better.

Related

Is there a nice naming convention for nonempty lists in Haskell? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 24 days ago.
This post was edited and submitted for review 24 days ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
For example, if I have a list of names names :: [Text], and I construct a NonEmpty Text from it, I could call it neNames, names', namesNonEmpty. Is there a standard (preferably short) convention for this?
A silly question, I know, but I'd like to know people's opinions on this. Naming is, after all, one of the two hard problems in computer science.
Edit: Apparently I'm not allowed to ask for people's opinions. However, the rest of the question still stands. Whether or not there is a standard convention for this is certainly something for which it is possible to provide citations.
Not that I'm aware of, but you might be able to borrow the convention from monadic parsers of using a 1 suffix to identify "at least one". We have sepBy and endBy when parsing zero or more things, and sepBy1 and endBy1 when parsing one or more things.
So, names and names1 might do it.
Ideally, you would try to design your datatypes so that you never have to name names at all, if an empty list of names indicates an invalid state, though I understand you might be validating an existing data structure where names can be empty into a new data structure where names1 must be non-empty.

Meaning of these particular pointers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm hesitant to consider this a valid question so close or downvote if you wish, I promise not to be offended :-)
I've just been watching Herb Sutter's excellent CppCon2018 talk on making C++ more powerful and simpler at the same time. I'd strongly suggest watching this if you haven't already.
Near the end, he references an xkcd cartoon on pointers, where the three pointers given are suspiciously ASCII in nature(a), usually a sure sign that you've somehow got yourself a corrupt pointer.
The three pointers are 0x3a28213a, 0x6339392c and 0x7363682e, which equate to the three character blocks :(!:, c99, and sch. (though endian issues may reverse the order of the characters).
Does anyone know if there's any significance to these pointers?
(a) Yes, I also noticed the ASCII in Homer Simpson's 3d episode sequence, meaning I've been in this game way too long :-)

static __forceinline or __forceinline static [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Good morning, this always made me wonder (even tho it doesn't really matter) but which one is the correct way? Or is there none and both ways are fine?
static __forceinline T some_func ( )
or
__forceinline static T some_func ( )
It is important to order modifiers alphabetically, except if you are writing code on the solstice or equinox (during which it angers the sun microsystems god).
Other than that, use whatever order you wish, so long as you are willing to live with the consequences.
Which are none.
As you said yourself, both are legal and their results are identical so it comes down on personal preference.
For what it's worth in most codebases I've worked compiler specific keywords were always used before any standard C++ keywords which makes your 2nd snippet more common.
However as always with code style, just pick one you like most and be consistent. Consistency is what's important.

To factor code in function or not? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
A little question that I know many diverge on this but I would like to know performance and sanity wise, what do you use to do:
What's best, factoring code in function (when using the same piece of code in multiple places) but then having to face the function-call cost or just keeping those pieces everywhere then having to deal with changes in different places when you have to change the logic?
Considering the fact that I need my code to be the fastest possible. Because it will run on memory/cpu restricted device.
Maybe some of you have a rule of thumb they apply, like when the code is bigger than a certain amount of lign, they gather it in a function...
Rule of thumb:
Trust the compiler, in general it has better heuristics than you whether a code should be inlined. Write clean code. Code duplication is your enemy.
Measure the performance or check the generated code, and only try to optimize if you are unhappy with the results.
If there are problems, try to utilize templates to avoid code duplication and generate code at the template instantiation location.

Is managed code slower than unmanaged code? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Its just a question out of my curiosity. Though generally considering the Framework and the steps involved in Execution, i'd say yes. Still i would also like to consider the factors like Memory/Disc access and networking which limit the performance of unmanaged code.
Quoting Herb Sutter
"First, JIT compilation isn’t the main issue. The root cause is much more fundamental: Managed languages made deliberate design tradeoffs to optimize for programmer productivity even when that was fundamentally in tension with, and at the expense of, performance efficiency."
There’s always an inescapable and fundamental difference between “prevention” and “cure” — when it comes to performance optimization, C++ always chooses “prevention,” and managed languages choose “cure” with the above-mentioned heroic efforts and many more. But the old ounce/pound saying is inescapable; you can’t beat prevention (in part because you can always add the cure after first doing the prevention, but not the reverse), and if you care about performance and control primarily then you should use a language that is designed to prioritize that up front, that’s all.
You can refer this article for more clarity
http://www.i-programmer.info/professional-programmer/i-programmer/4026-the-war-at-microsoft-managed-v-unmanaged.html