9 bad programming habits introduced by programmers inadvertently

We have all done this before: when the mother does not pay attention, secretly eat candy snacks, and then lead to tooth decay. In the same way, we have violated some of the basic rules of programming, and will firmly say that this behavior is not advisable. But we are secretly loving these bad programming habits.

We sneered at the so-called programming rules and the output code was bad - but we are still alive. Programming God did not strike lightning to kill us, our computers did not explode. In fact, as long as we can compile and release the code, the customer seems to be very satisfied.

This is because poor programming is not as harmful as installing a circuit or touching a tiger's butt. Most of the time it works. Rules are usually used as a guide or a format suggestion, and there is no hard and fast rule to follow, nor will the code die immediately. Of course, your code may be ridiculed, and maybe even everyone laughs at you publicly. However, this kind of challenging practice can add a little subtlety to the traditional pleasure, even if it is inadvertently.

In order to make the problem more complicated, sometimes it is better to violate the rules. (Ordinary people I don't tell him!) The code coming out will be cleaner and may even be faster and simpler. Rules are often too broad, and skilled programmers can improve the code by breaking these rules. Don't tell your boss, it will make a lot of sense for your coding career.

The following nine coding habits are refuted in the programming rules, but many of us will use them involuntarily.

Programming habit No. 1: Use goto

The ban on the use of goto can be traced back to the days when many structured programming tools were not available. If the programmer wants to create a loop or jump to another program, they need to enter goto followed by a line number. After a few years, the compiler team asked the programmer to replace the line number with a string tag. This was considered a hot new feature at the time.

Some people think this will lead to "spaghetti code." The code becomes unreadable and it is difficult to understand the execution path of the code. The thread is confusing and tangled up to the horizon. Edsger Dijkstra said that he should ban this order, and he has a witty manuscript entitled "Goto is not harmful."

But the absolute branch is no problem. This makes people tangled. In general, clever break statements and return statements provide a very clean statement about what the code does at that time. Sometimes adding a goto to case statement is easier to understand than a more appropriate multi-level nested if-then-else block.

There are also counterexamples. The "goto fail" security vulnerability in Apple's SSL stack is one of the best examples. However, if we can carefully avoid some of the embarrassing problems of case statements and loops, then we can embed good absolute transfer, making it easier for people reading the code to understand what is going on. We can insert break and return statements to make everyone feel cleaner and more enjoyable - possibly in addition to the hostile of goto.

Programming habit No. 2: Successfully avoiding documents

One of my friends has a very savvy boss who has never written any code, but insists on the idea that every function must be included in the documentation. Which programmer does not provide comments, then he will be punished. So, my friend has a little bit like artificial intelligence in his editor, so every function of his has a few lines of "documents". Because this savvy boss is not smart enough to understand these notes, it doesn't mean anything, so my friend escaped. His code is often used as an official document. I think he should be promoted!

Many function methods, even some classes, are more or less self-documenting. Functions with names such as insertReservation or cancelReservation or deleteAll do not need to explain their role. It is often sufficient to take a correct name for the function. In fact, this is better than writing a long comment, because the function name can appear elsewhere in the code. And the document can only stay in a corner silently. Self-documenting function names can improve every file they appear.

In some cases, writing a document can even cause the situation to get worse. For example, when the code changes rapidly and the team refactors like crazy, the documentation will be divided. The code is written like this, but the documentation explains the situation before the four or five versions. This type of "outdated" documentation is usually at the top of the code, and some people will give a good summary of what the code should do here. Therefore, although the refactoring team has carefully revised the relevant comments, it still misses the "good summary" at the top of the file.

When the code and text are divergent, the comments become worthless and even misleading. In this case, good self-documenting code clearly wins.

Programming habit No. 3: One line writes too much code

The boss suddenly sent a nasty email to the team: in order to enforce very strict style regulations, we all have to rewrite our code. The most magical requirement is that each action or step or clause must be in a separate line. You cannot call a function continuously using dot syntax. In a branch statement, you cannot have two or more clauses that return a Boolean value. If you want to define a variable, start another line. If you are doing a complicated calculation, don't use parentheses. Each clip is also a self-contained line.

He believes that his decree will make debugging easier. Just as you step through the code, the debugger will move forward with one action and one action. This will not get stuck in a certain line. And it's easier to implement.

But in this case, the Enter key on the keyboard is annoying because I need to constantly insert rows. And I'm sure the boss can still boast about how many lines his team can write.

Well, sometimes it's easier to declare a bunch of variables on the same line; sometimes it's easier to put all the Boolean clauses together - everything can be more compact. That also means that we can see more logic on the screen without having to scroll the mouse. Being easier to read means faster understanding. This is the essence of simplicity.

Programming habit No. 4: Do not declare type

Those who love typed languages ​​think that if you add an explicit data type declaration for each variable, you can write better, error-free code. Taking a moment to spell the type can help the compiler mark stupid errors before the code starts running. It can be painful but helpful. This is a prepared way to stop bugs in programming.

But the times have changed. Many newer compilers can intelligently infer types by looking at the code. They will browse the code backwards and forwards until it is certain whether the variable is a string or an int, or something else. If these viewed types are not queued, the error flag will illuminate. Therefore, we no longer need to enter the type of the variable.

This means that we can now omit some of the simplest declarations in our code. The code is cleaner, and the person reading the code also guesses that the variable named i in the for loop represents an integer.

Programming habit No. 5: swinging code

Some programmers are particularly indecisive in the code, hesitating. First, the value is stored as a string and then parsed into an integer. Then convert back to the string. This is very inefficient, and you can even feel the CPU is roaring this wasteful behavior. Smart programmers can code quickly because they design the architecture in advance to minimize conversions. Their code runs faster because they have a good plan.

But, whether you believe it or not, this wavering code sometimes makes sense. For example, you have a great library that can do countless smart things in its proprietary black box. If the library needs string data, then you give it a string, even if you just converted it to an integer.

Of course, you can rewrite all the code to minimize the conversion, but it takes time. Moreover, sometimes it takes a little extra time to run the code to run it, because rewriting the code takes us more time. Sometimes, carrying such technical debt is less costly than building it right from the start.

Sometimes libraries are not proprietary code, but the code you wrote all about yourself is unique to you. Sometimes, converting data again is much faster than rewriting all the code in the library. So let it be like this, let the code sway.

Programming Habits No. 6: Write your own data structure

There is a standard rule that programmers should not write code for storing data in the second year of completing a data structure course. Basically all the data structures we need have been written, and their code has been tested and retested for many years. It is bundled with language and is often free. Your code can only make bugs.

But sometimes you will find the data structure library a bit slow. Sometimes they force us to use the standard, but our code is the wrong structure. Sometimes the library pushes us to the point of reconfiguring the data before using the structure. Sometimes the library will contain some so-called protection features, such as thread locks, but in fact our code does not.

If this is the case, then you should write our own data structure. This may allow you to do it faster and do more. And the code will get cleaner, because we won't include the extra code that is used to format the data to do some functionality.

Programming Habit No. 7: Breaking the loop in the middle

There is a rulemaking team that declares that each loop should have a "constant", which means that when the logic statement is true, the loop executes. The loop will end when the constant must not be true. This is a good way to think about complex loops, but it can lead to stupid bans -- for example, prohibiting us from using return and break statements in the middle of a loop. This one is also included in the rule that prohibits the goto statement.

This theory is good, but it usually leads to more complicated code. Take a look at this simple case, loop through the array, pass the found elements to the test function, and return the element:

While (i

"Circular constants" enthusiasts will ask us to add a Boolean variable named notFound and then use it like this:

While ((notFound) && (i

If this Boolean value can be reasonably named, then this is a great self-documenting code that is easier for everyone to understand. But it also adds complexity. This means that you need to allocate another local variable and block the register, because the compiler may not be smart enough to solve the problem.

Sometimes a goto statement or a jump will be cleaner.

Programming habit No. 8: Using short variable names (i and x and and are also meaningful)

Edgar Allan Poe, the poet and novelist, once said that every word in a story should have connotations. The coding rules also emphasize this. The variable name should indicate what this variable does. Java programmers who use the camel case to write variable names to express the details of the variables are so convinced that variable names of one crazy length are released. Some programmers write variable names that combine five or six or more words.

But sometimes it is more convenient to use a single letter as the variable name. Sometimes it is easier to just use i or j in loop iterations. Sometimes the letter a is used for array, and l is more convenient for lists, even if the letters l and 1 seem difficult to distinguish.

As encouraged earlier in this article, self-documenting code is not a long comment. In the above case, the variable name of a single letter is also self-documenting. The letter i is a generic iterator. As long as the programmer will understand immediately.

Programming Habits No. 9: Redefining Operators and Functions

Some of the most interesting programming languages ​​allow you to do something weird, such as redefining the value of an element, just like a constant. For example, Python, you can type TRUE=FALSE (in versions 2.7 and earlier). This does not create a logical breakdown, or the end of the universe - just the meaning of TRUE and FALSE. You can also play dangerous games like this in the C preprocessor and some other languages. There are also languages ​​that allow you to redefine operators, such as plus signs.

Of course this is an extension, but there is a point that in a large block of code, the speed will be faster when redefining one or more so-called constants. Sometimes the boss will ask the code to do something very different. Of course, you can modify every event of the code, or you can redefine it. This makes you look like a genius. You don't have to rewrite a huge library, just flip it and you can do the opposite.

These 9 habits are all here. Don't try it easily, no matter how big it looks. Too dangerous - really, this is the truth.

Box Header Connector

Box Header Connector,Dual Row R-Type Box Header Connector,Box Header Straight Elevator Connector,Smt Box Header 2.0Mm Connector

Dongguan Yangyue Metal Technology Co., Ltd , https://www.yyconnector.com