New answers tagged

0

This a tricky one. Before we jump into the details, just a small remark on your post itself: The title of your question has already suggest a direction or your favour. The same would be true if you would name it like this: Microservices Guarded Endpoint Design. It seems like that you have already made the decision just looking for justification. But this is ...


-3

Common usages of macros Macro for defining constants // Bad #define PI 3.14 // Good const double PI = 3.14; Always use const instead. This makes sure that you explicitly type your constant. Function-like macro If you can use function to achieve what you want, always use a function. Explicitly declaring this function inline is not necessary. As you ...


4

You see an absolute rule, and things don't work that way. You also seem to be focussed on macros vs. inline functions which is very rarely the question. Macros can be extremely useful if you know how to use them. They can also be anything from misleading to dangerous if you don't know what you're doing. I haven't used the two projects that you mention, but I ...


1

I think there are two ways I would consider implementing this: Using an enum This is usually the appropriate design, in particular when you know all possible types up front. A benefit is the strong type checking, as you will be warned if a match forgets to handle a particular type. pub enum Cell { Hall, Filled, Empty, } impl Cell { pub fn ...


0

Efficiency aside, there is indeed a downside to learning anything just by doing. Only the most talented people will recognize what they lack, know what next thing to look into and take their skill to the next level on their own. Most just hit a way that works for them and stick to that. They end up in a rut, being one-trick ponies. "Better" people ...


2

Efficiency isn't an absolute measurement, it's relative. Take MethodA for example. I just developed this now. When you call it, it takes two seconds to complete. Is that efficient? The simple answer is that you cannot judge that. If this method adds two numbers together, the efficiency is atrocious. If this method brute forces a complex encryption, the ...


3

There is very little code that needs to be efficient. Assume you are one of 20 million developers who need to sort an array. 19,999,999 of them don’t need to know how to do it efficiently, only one needs to know.


6

How will beginners learn to write efficient code when they dive head first? Beginners learn to write efficient code when they learn to write readable code and show it to someone else. That someone usually tells them no one cares about their efficiency concern. New programmers have a persistent problem in that when they finally get something working they ...


2

Note that since C17/18 a subtle addition occurred: If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned to indicate an error, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. § 7.22.3 1 Now when malloc(0) ...


0

Multiple returns introduce implicit paths in the code to just past the end of the function (or method) that are visible neither at the individual return statements, nor at the end of the function. This can cause a reviewer to misinterpret the behaviour of the code through no fault of their own. Not all functions can be inspected at a glance and having to ...


-1

Today, there is one practical reason: To make debugging easier. If you want to see which value a function returns, it is often easiest to store the result in a variable which is returned at the very end, so you can set a single breakpoint and check the return value. Or print it for debugging. If you have a debugger that can do better, good for you. Would be ...


0

Is there a historical reason why this convention came about? The answer I learnt - at university, in the early 1980's - was that single entry and exit were pre-conditions to mathematical proof of correctness of code. This was deemed sufficiently important that it was part of the course. This is a somewhat more recent (2004) writeup from Cornell in the ...


6

In my opinion, that is a horrible paradigm. I see absolutely no pros and at least three substantial cons. Needless code complexity Since malloc(0) can return NULL, the code has to be written to handle that anyway. And since malloc(0) can also produce a non-NULL result, the code also has to be written in a way to handle a non-NULL pointer. Pointer ...


Top 50 recent answers are included