Programming Rules of Thumb (2024)

Important Programming "Rules of Thumb"

1) K.I.S.S. (Keep It Simple, Stupid)

There are many areas where this rule applies in programming. Two very important ones are:

a) Subprogram behavior andlength: Subprograms should do precisely ONE conceptual task andnomore. The length of a subprogram should allow it to be easily visuallyinspected; generally no more that one page in length. Similarlyyou should generally not mix input/output andalgorithmic logic in the same subprogram; it is alway a goal toseparate I/O from logic.

b) If a problem is can be decomposed into two or more independently solvableproblems, then solve them independently and after you have implemented and tested theindependent solutions, then combine them into the larger result.This is sometimes known as "Gall's Law":

"Acomplex system that works is invariably found to have evolved from asimple system that worked. The inverse proposition also appears to betrue: A complex system designed from scratch never works and cannot bemade to work. You have to start over, beginning with a working simplesystem."


2) "Rule of Three" (code duplication)

is a code refactoring rule of thumb todecide when a replicated piece of code should be replaced by a newprocedure. It states that you are allowed to copy and paste the codeonce, but that when the same code is replicated three times, it shouldbe extracted into a new procedure. The rule was introduced by MartinFowler in his text "Refactoring" and attributed to Don Roberts.

Duplication in programming is almost always in indication of poorlydesigned code or poor coding habits. Duplication is a bad practicebecause it makes codeharder to maintain. When the rule encoded in a replicated piece of codechanges, whoever maintains the code will have to change it in allplaces correctly. This process is error-prone and often leads toproblems. If the code exists in only one place, then it can be easilychanged there. This rule is can even be applied to small number oflines of code, or even single lines of code. For example, if you wantto call a function, and then call it again when it fails, it's OK tohave two call sites; however, if you want to try it five times beforegiving up, there should only be one call site inside a loop rather than5 independent calls.


3) Ninety-ninety rule ( failure to anticipate the hard parts)

The ninety-ninety rule is a humorous aphorism that states:

"The first 90 percent of the codeaccounts for the first 90 percent of the development time. Theremaining 10 percent of the code accounts for the other 90 percent ofthe development time."
—Tom Cargill, Bell Labs

That the total development time sums to 180% is a wry allusion to thenotorious tendency of software development projects to significantlyoverrun their original schedules. It expresses both the roughallocation of time to easy and hard portions of a programming projectand the cause of the lateness of many projects (that is, failure toanticipate the hard parts). In other words, it takes both more time andmore coding than expected to make a project work.


4) Efficiency vs. code clarity (chasing false efficiency)

Never sacrifice clarity for someperceived efficiency. One of the biggest mistakes that newprogrammers make is tweaking code to remove a couple of textual linesof high level code and replace it with a much more complex single lineof code. This is commonly called "bit twiddling". Alwaysremember that most compilers optimize code. Further, there is acorollary to the 90-90 rule known as the "Pareto Principle":

In computer science, the Pareto principlecan be applied to resource optimization by observing that 80% of theresources are typically used by 20% of the operations. In softwareengineering, it is often a better approximation that 90% of theexecution time of a computer program is spent executing 10% of the code(known as the 90/10 law in this context).

Given this knowledge, most "bit twiddling" will have no perceivableimpact on the runtime of most programs as most of them will likely bein the 90% of code that has little impact on the run-time of theprogram. The real efficiency gains come from changing the order ofcomplexity of the algorithm, such as changing from O(N^2) to O(NlogN)complexity. Keep your code clearly and cleanly written and itwill usually be reasonably efficient. Occasionally, after theprogram is written and tested, it might prove to be slower than theproblem specification calls for. On the these few occasions, andonly after you have first optimized the complexity of the algorithm,then instrument or profile the code and find the 10% or less of thecode that PROVABLY causes slow runtime and then optimize that smallcode segment.

Comments on this topic from well respected computer scientists and software engineers:

"More computing sins are committed inthe name of efficiency (without necessarily achieving it) than for anyother single reason — including blind stupidity." — W.A. Wulf

"We should forget about small efficiencies, say about 97% of the time:premature optimization is the root of all evil. Yet we should not passup our opportunities in that critical 3%. A good programmer will not belulled into complacency by such reasoning, he will be wise to lookcarefully at the critical code; but only after that code has beenidentified" — Donald Knuth

"Bottlenecks occur in surprising places, so don't try to second guessand put in a speed hack until you have proven that's where thebottleneck is." — Rob Pike

"The First Rule of Program Optimization: Don't do it. The Second Ruleof Program Optimization (for experts only!): Don't do it yet." —Michael A. Jackson


5) Naming of things (subprograms and variables)

In computer programming, a namingconvention is a set of rules for choosing the character sequence to beused for identifiers which denote variables, types and functions etc.in source code and documentation. Reasons for using a naming convention(as opposed to allowing programmers to choose any character sequence)include the following:

1) to reduce the effort needed to read and understand source code which supports its maintainability
2) to enhance source code appearance (for example, by disallowing overly long names or unclear abbreviations)

There are lots of naming conventions that are strongly argued for oragainst by various engineers; in reality these are mostly religiousarguments. However, whatever you do, you should follow someconsistent naming style. There is one thing that is common to mostall naming conventions, and that is that the name should be descriptiveof the contents, or a name that is in common programming practice forthe language (such as using i, j, k for loop and array indexes).

Commonly, subprograms should have verb/verb phrase names as asubprogram should specify one specific task (see KISS rule) whichshould be an activity description. Variables should have noun oradjective names as variables represent things or attributes ofsomething. When choosing a name, if you have difficulty in coming upwith adescriptive name this is an indication that your code needsfurther refactoring to improve the clarity of the design and the purpose of subprograms and variables.

Programming Rules of Thumb (2024)

FAQs

Programming Rules of Thumb? ›

Rule 1: Follow a consistent coding standard. Rule 2: Name things properly, long variable and function names are allowed. Rule 3: Be expressive, write code as you speak, and be optimally verbose. Rule 4: Max indent per method should be 2, in case of exceptions 3.

What are the golden rules of programming? ›

Rule 1: Follow a consistent coding standard. Rule 2: Name things properly, long variable and function names are allowed. Rule 3: Be expressive, write code as you speak, and be optimally verbose. Rule 4: Max indent per method should be 2, in case of exceptions 3.

What is the 40/20/40 rule of thumb in software engineering? ›

The 40-20-40 rule applies to software engineering application development. The rule states that 40% of the work is performed during feasibility, analysis, and design; 20% is during coding; and the remaining 40% is during testing (see Figure 18-3).

What are the basic rules of coding? ›

Coding rules and guidelines ensure that software is: Safe: It can be used without causing harm. Secure: It can't be hacked. Reliable: It functions as it should, every time.

What is the rule of 3 DRY? ›

Well, how would you know if your code isn't DRY enough? That's kind of subjective and will vary by the norms set within your organization. That said, a good rule of thumb is the Rule of Three. This rule states that the third time you encounter a certain pattern, you should probably abstract it into some reusable unit.

What is the rule of 3 refactoring? ›

The “Rule of Three” is a simple code refactoring rule of thumb. It can help us decide when we should refactor similar pieces of code. The rule states that we don't need to worry about refactoring if we only have two pieces of similar code. When we have three or more pieces, then we should refactor the code.

What is the rule of 3s programming? ›

It states that two instances of similar code do not require refactoring, but when similar code is used three times, it should be extracted into a new procedure.

What are the 5 rules of programming? ›

Rob Pike's 5 Rules of Programming
  • Rule 1. You can't tell where a program is going to spend its time. ...
  • Rule 2. Measure. ...
  • Rule 3. Fancy algorithms are slow when n is small, and n is usually small. ...
  • Rule 4. Fancy algorithms are buggier than simple ones, and they're much harder to implement. ...
  • Rule 5. Data dominates.

What are the 4 basics of programming? ›

All of these elements can be found in any programming language.
  • ALGORITHMS. An algorithm is a list of steps followed in order to complete a task (run a program). ...
  • SYNTAX. ...
  • FUNCTIONS/METHODS/PARAMETERS/ARGUMENTS. ...
  • OPERATORS.
Apr 6, 2021

What are the rules for basic programming? ›

Basic ground rules for programming – function parameters and how they are used
  • Everything not defined is undefined. ...
  • All parameters must be valid. ...
  • All parameters are stable. ...
  • The correct number of parameters is passed with the correct calling convention. ...
  • Function parameter lifetime. ...
  • Input buffers. ...
  • Output buffers.
Mar 20, 2006

What not to do when coding? ›

A dirty dozen of software development pitfalls—and how to avoid these all-too-common programming blunders.
  1. Playing it fast and loose.
  2. Obsessing over details.
  3. Too much theoretical complexity.
  4. Not enough theoretical complexity.
  5. Too much faith in artificial intelligence.
  6. Not enough training data.
Mar 6, 2023

What are the 7 steps of coding? ›

7 steps of writing computer code
  • Awareness of the software solution purpose. Every software solution starts with an idea. ...
  • Designing the solution. ...
  • Writing the code. ...
  • Source code compilation. ...
  • Run the executable code. ...
  • Program debugging. ...
  • Code maintenance.
Nov 17, 2022

What is the principle in the dry? ›

The DRY principle is a fundamental principle of software development that promotes code reuse and modularization. The principle states that "every piece of knowledge or logic should have a single, unambiguous representation within a system." In other words, you should never repeat yourself in your code.

What is the meaning of the dry rule? ›

a law prohibiting the sale of alcoholic beverages.

What is the rule of 30 clean code? ›

a) Methods should not have more than an average of 30 code lines. b) A class should contain an average of less than 30 methods, resulting in up to 900 lines of code. c) A package shouldn't contain more than 30 classes, thus comprising up to 27,000 code lines.

What is the Don Roberts rule of three? ›

2) "Rule of Three" (code duplication)

It states that you are allowed to copy and paste the code once, but that when the same code is replicated three times, it should be extracted into a new procedure. The rule was introduced by Martin Fowler in his text "Refactoring" and attributed to Don Roberts.

What is the rule of three in C++? ›

The rule of three (also known as the law of the big three or the big three) is a rule of thumb in C++ (prior to C++11) that claims that if a class defines any of the following then it should probably explicitly define all three: destructor. copy constructor. copy assignment operator.

What is the golden rule of programming? ›

Make sure you understand the problem you're trying to solve. Prioritize clarity and correctness. Performance is less important, and cleverness is to be avoided.

What is the first rule in programming? ›

1st Rule Of Programming: If It Works Dont Touch It. 2nd Rule: Never Forget Rule 1. If you love writing computer programs, developing software, debugging and are a coding enthusiast, this great saying is awesome.

What is a 1 2 3 refactor? ›

Sometimes called “1, 2, refactor”, the “rule of three” is code refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that the code can be copied once, but that when the same code is used three times, it should be extracted into a new procedure.

What are the 3 basic golden rules? ›

1) Debit what comes in - credit what goes out. 2) Credit the giver and Debit the Receiver. 3) Credit all income and debit all expenses.

What is the Golden Rule in computer? ›

Golden rule 1: Handle all information with care

Most data is lost through human error.

What is the Golden Rule of compliant coding? ›

The golden rule of coding is that if it is not documented by the physician or provider, it didn't happen, Pritchett said. Documentation has never been as important as it is in today's facilities and physician's offices.

What are golden 4 rules in design process? ›

The UI design principals are: Place users in control of the interface Make it comfortable to interact with a product Reduce cognitive load Make user interfaces consistent 1.

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6236

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.