DEV Community

Cover image for The DRY Principle
Ahmed Atef
Ahmed Atef

Posted on

2 1

The DRY Principle

عندما نفكر في ممارسة أفضل الحلول البرمجية أثناء برمجة الأكواد ، فعليك بشكل أساسي أن تتذكر هذا المبدأ
(Don’t Repeat Yourself) 🔄
والذي يُلخص التعبير التالي
“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”

إذاً يستهدف هذا المبدأ إلى تقليل بل تجنب التكرارات الزائدة عن الكود بلافائدة ، فلنتعجب عندما نقضي المزيد من الوقت في تكرار الكود وفي الحقيقة إن الغرض من معظم التطبيقات هو إتوماتيكية المهام والوظائف مواضع التكرار في السيستم

📢 فما الحل ؟؟؟

يكمُن الحل في استبدال الدوال أو الوظائف المكررة بأخرى مُجردة أو بتجميعها تحت مظلة وظيفة واحدة فردية ومن ثم إستدعائها في أي مكان يتطلب تنفيذ مهام هذة الدالة.
ويتحقق هذا من خلال الإقتراحات التالية:
✅Creating an API reference guide from comments
✅Automatically detecting unit tests through an annotation or naming convention
✅Generating both PDF and HTML documentation from a single markup source
✅Deriving object classes from a database schema

When following the coding best practices, remember the DRY Principle. DRY stands for Don’t Repeat Yourself, and the DRY Principle states that “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.” This is also sometimes known as DIE: Duplication Is Evil.

The Principle aims at reducing repetition and redundancies within the software engineering process. The purpose for most applications is to automate repetitive tasks, so why spend time repeating code?

This is achieved by replacing repetitions with abstractions or by grouping code into functions and Keeping a definitive version of your code in a single place, and then let this version drive all other uses. You can do this by:

✅Creating an API reference guide from comments
✅Automatically detecting unit tests through an annotation or naming convention
✅Generating both PDF and HTML documentation from a single markup source
✅Deriving object classes from a database schema

When you follow the DRY principle, you are saving yourself time and energy and following one of the most important coding best practices.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️