Who cares ... while the result is okay?

I am a little overwhelmed by the many reactions to questions that indicate that developers care more about the compiled bytes received than about the meaning of their code. I prefer nit-pick about postfix / prefix increment, as I try to use boolean for an enumerated type with two values, as well as the proper function naming and ...

So, the question is rather a retrig survey: when can the semantics of what it writes be ignored? Where is the border?

  • operator ++ (postfix / prefix)
  • string.empty () vs. string == ""
  • vector.empty () vs. vector.size () == 0
  • enumerate {on, off} vs. boolean on = true; off = false
  • ...

Call him.

EDIT -

I did not want to ask about the need for (micro) optimization. Rather, I need opinions on how well you should know about what you are writing, and on expressions such as “but it compiles into dword anyway, so why should I do this by enumeration”? (which is an extreme case ...).

+4
source share
10 answers

I have written a lot of code in my career. Of course, not as many as many people here, but a lot of code nonetheless. During this time I learned one thing that repeated itself again and again: when you become sloppy, when you ignore details, defects creep.

Defects require time and money to repair. You can spend time writing the code clearly and clearly ahead of time when it’s cheap, or you can spend a lot of time and money then chasing a lack of code that probably doesn’t remember all this because you tossed it together at in a hurry or because it does not comply with the coding standard. The end result is that you are wasting time and money. In addition, you spend time and money with which you did not need to spend money to start with them, because this could be prevented by small upfront investments.

I have never regretted being meticulous about how I write code. He never came back to haunt me. Being sloppy always came back to haunt me.

+4
source

Define "ok". Is everything ok if it works with the initial delivery date, but every time you need to make changes, it takes another three weeks to figure out the old code?

There is your answer: as long as you can understand the code, and that will not hurt your ability to maintain it.

+9
source

When you start worrying about esoteric things that don't affect the bottom line, I think it's too far away. Discussions on how to use the ternary operator or write explicit if the statements are good for those days when you have nothing to do except sit back, raise your legs, drink beer / wine / soda and discuss the "big consequences" :)

But creating an enumeration for boolean is simply wrong.

+3
source

Depends on your cost function

Here are a couple of dimensions that people like to argue about and often come together. Indeed, the answer depends on this. What do you really appreciate?

  • Characters
  • Number of ops created
  • at runtime
  • Portability
  • maintainability
  • Clarity
  • Giftedness
  • Stability (no errors?)
  • Duration

I always strive for things of a higher order, like clarity. The loss of clarity is paid for in human cycles, which are always lacking. People rarely care about raw watches, and people who say what they do almost always optimize prematurely.

If this helps you, I like to think that we are making progress by climbing up the stack, more concerned with semantics and work, rather than driven cues and numbers. However, this is not a reason to forget the basics and remember how certain semantic bits are implemented. You don’t want your pants hooked during those rare opportunities where speed begins to matter and agreement comes out of the window.

+3
source

"when can the semantics of what he writes be ignored? Where is the boundary?"

I think the reasonable question is: "when should I ignore the semantics of what I am writing?"

Since I consider programming as a human activity, I would say that the only time you need to ignore the semantics of statements is when the system itself forces you - when some obscure statement - it just does something. Such statements are good for documentation.

+3
source

A border is when you are dealing with write-once, read-never code. Only then it ultimately does not matter what you do (in this case), because you will never use it again. Unfortunately, this does not apply to indirect feedback from practice that you would not want to repeat.

+2
source

Border is side effects.

If the encapsulated function does everything you need with 0 unexpected side effects, And the code reads that everything that matters. If you are predictable to an external user, and any “bizarre” functionality is supported domestically, it all matters.

It changes a bit if you add optimization, but since you should never prematurely optimize, then where does it end.

+1
source

I think that these days most people agree that, considering this to work correctly, readability is the most important consideration. Of course, there are exceptions - some applications should start as fast as possible, and some applications may never resolve a failure, but, in general, this is readability.

+1
source

Microoptimization is pointless in 99% of cases. For example, if your compiler puts everything "in one instance anyway, you will not increase performance in any way using String.Empty. Without having any measurable effect, as a rule, you can best hope, I saw that" optimization " it reduces performance because the compiler does a better job, and optimization interferes with it.

Most of the code does not need optimization. Determining where this should happen can only be done with diagnostics after running the code, and even then most of the time it is done using algorithmic optimization, and not for micro-optimization.

+1
source

Watching the examples you provided, Following:

operator++ (postfix / prefix)

string.empty() vs string == ""

Doesn't seem like good examples, as they compare operations other than functionality . Consequently, one does not better ignore their semantic differences.

In contrast, the following examples:

vector.empty() vs vector.size() == 0

enum erate { on , off } vs. boolean on=true ; off=false

Absolutely reasonable.

vector.empty() is preferred if the context of its use is only to determine if the vector is empty. By reducing risk, it is condescending (which I do not ): it comes down to common sense. Why ask the size of a vector only if you want to know if it is empty? It is like asking someone how much money they have in their wallet when you just want to find out if they have enough cash for Cox.

Regarding enum erate { on , off } vs boolean on=true ; off=false , ask yourself: how likely is it that you can add another value to the enumeration in the future? It seems reasonable that enum erate { on , off , indeterminate } `(or some option) may be required, so the answer may be yes. Otherwise, a simple Boolean is sufficient.

This brings me to the core of your question: what seems to exist if there is some kind of deterministic / algorithmic approach to this or that solution of such issues or their relevance? I answer that until the Turing Machines can pass the Turing Test , I would say no. This is the reason why people should develop software.

+1
source

All Articles