
#How to write computer code repeating code#
Merging duplicate code simplifies the structure of your code and makes it shorter.

If the same code is performed in all branches of a conditional expression: place the identical code outside of the condition tree by using Consolidate Duplicate Conditional Fragments. If a large number of conditional expressions are present and perform the same code (differing only in their conditions), merge these operators into a single condition using Consolidate Conditional Expression and use Extract Method to place the condition in a separate method with an easy-to-understand name. If it’s difficult or impossible to create a superclass, use Extract Class in one class and use the new component in the other. If the classes aren’t part of a hierarchy, use Extract Superclass in order to create a single superclass for these classes that maintains all the previous functionality. If duplicate code is found in two different classes: If two methods do the same thing but use different algorithms, select the best algorithm and apply Substitute Algorithm.

If the duplicate code is similar but not completely identical, use Form Template Method. If the duplicate code is inside a constructor, use Pull Up Constructor Body. Use Extract Method for both classes, followed by Pull Up Field for the fields used in the method that you’re pulling up. If the same code is found in two subclasses of the same level: If the same code is found in two or more methods in the same class: use Extract Method and place calls for the new method in both places. And in some cases, the programmer is simply too lazy to de-clutter. When rushing to meet deadlines and the existing code is “almost right” for the job, novice programmers may not be able to resist the temptation of copying and pasting the relevant code. This kind of duplication can be hard to find and fix. There’s also more subtle duplication, when specific parts of code look different but actually perform the same job. Since they’re working on different tasks, they may be unaware their colleague has already written similar code that could be repurposed for their own needs. Reasons for the Problemĭuplication usually occurs when multiple programmers are working on different parts of the same program at the same time. Two code fragments look almost identical.

If you start working on code that was written by someone else, the style should be followed./ Dispensables Duplicate Code Signs and Symptoms If you are using an IDE, it is recommended that you create a formatted config so everyone uses the same one.Įvery developer will be more comfortable with one style or another. In most of the programming languages, spaces and indentations do not affect the function. The code should be divided into logical units and it’s important to keep the style consistent throughout the whole document. Style is important, not to make code look pretty, but because it helps with reading, editing, and understanding. 11 Tips That Will Help You Write Better Code 1) Decide on the indentation and keep it that way To write better code means mainly to understand it and keep it consistent. Being a programmer doesn’t mean to just write code that works, but it must be written cleverly and well so that your team or people that work with it afterward, can understand it easily.
