• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
cropped CodeSpecialist e1592127475203

Code Specialist

Code and Programming Blog

  • Home
  • Clean Code
  • Code Principles
  • Code Interview
  • Python

DRY Principle (Computer Science)

Do you tend to repeat code over and over again? There are several important reasons besides DRY (Don't Repeat Yourself) not to do so.

Home » Code Principles » DRY Principle (Computer Science)

June 15, 2020 by Yannic Schröer Leave a Comment

Contents hide
1 The DRY Acronym
2 What’s the problem about code repetition?
3 Example of duplicated code
4 How to apply the DRY principle?

The DRY Acronym

DRY stands for “don’t repeat yourself”. The DRY principle is one of the most important principles in software development, if not even the most important. Whenever you write or read code and find repetition in it, chances are high you or whoever planned it, didn’t plan well enough.

What’s the problem about code repetition?

Code isn’t static. When you write code twice you have to test it twice and if you have to change it afterwards you also have to change it twice, in fact we already got the four-fold effort to invest. There is also a large chance, you miss the second occurrence. Seriously, nobody can remind himself to every line or routine of code he wrote in a project, at least if it isn’t an insanely small one.

There is a word for software that isn’t changed: Hardware

Unknown author

In fact, redundant code is not only one of the most common causes for bugs, it’s also the cause for unnecessary complex or at least long code. The DRY principle helps you to avoid these problems.

Example of duplicated code

Whats wrong by changing this to:

I am aware that this is a rather simplified example and that the solution doesn’t apply to every problem, but that’s not the point. Fact is, by generalizing concepts and for example moving redundant code to functions you save time and spare potential bugs. If applied correctly it may also increase the readability of your code.

Learn more about code principles (Affiliate)

How to apply the DRY principle?

There is no general concept that allows you to always spare repetition, but there are steps that may help you to minimize it.

  1. Plan thoroughly

    By planing your program or software carefully you may identify patterns and generalized concepts in an early stage.

  2. Review frequently

    Review your code frequently, or even better let someone else review your code (That will also ensure your code doesn’t exceed a certain level of illegibility).

  3. Use tools that support you

    Depending on how large your project is, it’s nearly impossible to find repetition manually. But there are tools that allow you to find repeated code anyway. For instance PyCharm¹ offers a duplicates tool window that allows you to identify code repetition.

¹ Link to PyCharm

yannic schroeer
Yannic Schröer

I am a developer and entrepreneur from Germany. I chose studying computer science because I love building things. I am of the opinion that there isn’t one truth (especially for computer science concepts) and whoever claims so isn’t that trustworthy in my eyes. I constantly try to improve things and myself. In my spare time I support businesses by offering them most of my services for free. Beside that I am kind of addict to e-learning courses.

Book Recommendations on Clean Code (Affiliate)

Category iconCode Principles Tag iconClean Code,  Software Engineering

Related Posts

mossy stone
SOLID Principles with Python Code Examples
March 15, 2021
electrical switches
Python Switch-Statement Alternative
October 25, 2020
hands on writing machine
Meaningful Names
July 31, 2020

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published.

Primary Sidebar

Search

Categories

  • Algorithms
  • Cloud
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Hardware
  • Learn to Code
  • Nice to know
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Tags

Alternative (1) Announcement (1) Clean Code (7) Cloud Foundry (1) Cloud Native (1) Code (1) Code Interview Question (2) Code Philosophy (1) Comments (1) Computer Graphics (2) Concurrency (1) Control Characters (1) Data Science (1) Data Structures (1) decorators (2) dictionaries (3) Easter Eggs (1) funny (1) Github Actions (1) Marching Cubes (1) Meshing (1) Microservice (1) Nice to know (1) Parallelisation (1) Programming Facts (1) Pythonic (3) PyYAML (1) Readability (4) Simple is good (2) Software Engineering (2) switch (1) Syntax (1) Tricks (1) type hints (1) Ubuntu 20.04 (1) Underscores (1) Unix (1) Video (1) Virtualization with Windows (1)

Footer

Recent Posts

  • Cloud Foundry Components
  • No matching distribution found for pkg-resources==0.0.0
  • SOLID Principles with Python Code Examples
  • 5 Hardware Ideas to Upgrade your Programming Experience
  • 5 Programming facts every programmer must know

Tags

Alternative Announcement Clean Code Cloud Foundry Cloud Native Code Code Interview Question Code Philosophy Comments Computer Graphics Concurrency Control Characters Data Science Data Structures decorators dictionaries Easter Eggs funny Github Actions Marching Cubes Meshing Microservice Nice to know Parallelisation Programming Facts Pythonic PyYAML Readability Simple is good Software Engineering switch Syntax Tricks type hints Ubuntu 20.04 Underscores Unix Video Virtualization with Windows

Categories

  • Algorithms
  • Cloud
  • Code Interview
  • Code Principles
  • Environment
  • Errors
  • Hardware
  • Learn to Code
  • Nice to know
  • Object Orientation
  • Python
  • Technical Background
  • Write Better Code

Recent Comments

  • Nina on Bubble Sort in Python and how to Visualize it
  • Bhan on Concurrency in Python

Follow Us

  • Facebook
  • Instagram
  • Pinterest
  • Twitter

Quicklinks

  • About us
  • Become a writer
  • Guest Post

Privacy Policy | Legal Notice | Contact

 

All rights reserved © 2020-2021 code-specialist.com