DEV Community

Cover image for The DRY Principle
Ahmed Atef
Ahmed Atef

Posted on

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.

Top comments (0)