Programming Terms: DRY (Don't Repeat Yourself)

Corey Schafer · Beginner ·💻 AI-Assisted Coding ·11y ago

Key Takeaways

The video explains the concept of DRY (Don't Repeat Yourself) in programming, demonstrating how to apply it in Python to reduce code repetition and improve maintainability.

Full Transcript

hey what's going on guys this is going to be another programming terms video and in this video we're going to take a look at dry and dry is something that stands for don't repeat yourself now this is something that you are probably going to hear a lot if you're just getting started out in programming or maybe you just got a job in in computer science um you know maybe your boss comes up and says hey our code base is a mess and the first thing I want you to do is to make this code dry so if he tells you that that means is don't repeat yourself and if we take a look at Wikipedia uh what they have on there is it says a principle of software development aimed at reducing repetition of information of all kinds okay so what exactly do they mean about this um I have this little sample project here this is in Python but uh this dry concept spans all languages uh so you know don't get tied into just what the language is doing um so anyways here we have uh some code that has a homepage an about page and a contact page and if I print this out you can see that um it prints out this HTML here it prints out a header and it prints out this paragraph here that says Welcome to our home page then it prints out a footer um with these footer links um but if you look here we have all of this Header information uh in the homepage all of it in the about page all of it in the contact page so this is being repeated over and over and over and that's what people mean whenever they say they want you to make the code dry they don't want that code to be repeated like that because it makes it hard to maintain it so say that your boss came up and he said hey I don't want this to uh or I want to add a link into the header um so you'd have to come in here and you would have to manually add uh a link into every one of these header sections um which it would be a lot nicer if that was all in one place so what would it look like if we tried to make this code dry um so first of all Let's uh you may see here that we want to print out um so we have headers being printed out and Footers so maybe we should just pull these out into their own sections here so I can do um a function to print out a header and I'll just pass that for now and then I'll do a footer here and then I'm just going to go ahead and copy these sections in and place them within these functions so now I have this in one central location um and then we'll go through and we'll replace all of the repeated code with this one function so these are all the headers here and then I'll go through and do all the Footers so now that I've replaced all this code uh if we go in here you can see that this is all cleaned up nice and neat now to where it's a lot easier to read than it was before we can see that this is the header and then we want to print out this homepage paragraph uh this is the header to this page and we want to print out this about page paragraph it's so much easier to read and not only that but if we want to make a change we only have to make that change in one spot um now actually we could even um make this more Drive because you can see that we're uh that we are repeating these menus here um so if we wanted to we could even come out here and do um you know a a nav menu function that printed out these as well then I could go through here and um change these nav menus out with this simple function here and now all that is located in one place now it is possible to get carried away with this um you know in just in this example you can see that we probably got a lot bigger benefit for doing our headers and our Footers than we did for uh just doing this little nav menu which is only um repeated one time so if something's only repeated once or twice here and there then the benefi the then the benefit may not be that large but whenever you have something that is just file after file being repeated then it's usually nice to put that in one central location so that you don't repeat yourself and that you can maintain that code in one place now just to show that that worked let me go ahead and run this code here and you can see that everything pulled out into these functions it still works as planned I'm printing out the homepage down here and we still get our header and our paragraph and our footer section there as well now let me show you a more practical example of where you might see something like this um so I have this file here called calc. py it's a python file that just has really simple functions add subtract multiply and divide now what if I was to write unit test for this um so here's a test.py if you don't know what unit testing is don't worry about it we're just focusing on um the dry principle right now um but anyways uh say that we wanted to test this code and we want to test the addition the subtraction uh multiplication and divide now you can see when we're testing this code at every test we're doing num 1al 10 num 2 equal 5 and then you know we're testing make sure that those numbers add and subtract um accordingly um but you can see that every test we have to uh put in what these numbers are equal to um so the python unit testing actually tries to take care of this for you they have a method called setup that runs before every test so if you're doing something like this where you have the same values that you're setting up for every test then you know just put it into a setup function and it makes your code more dry and then you don't don't even have to uh put that function there it's part of the framework so I can just take those out and I'll go through here and remove these and now that those are removed you can see that the code is a lot more clean it's a lot easier to see what's going on and if I want to change some of these values uh you know to different values now I can do it in one spot now in order to get this to work right for python uh technically I'd have to come in here and uh add self. num one to all of these and then go through and change these in every location where there's a num one um but you know that's not the uh what I was trying to get across I'm just trying to get across that it's easy to uh pull out this information and put it in one central location so that uh one it's not repeated and two it's a lot easier to maintain now real quick I went through and went ahead and changed these values and you can see after I change them if I run this code you can see that it ran all four test uh with no problems so this setup method is working correctly with setting up these values before each individual test so that is a quick overview of what it means for your code to be dry so now if you hear somebody saying it or if maybe your boss comes up and asks you to uh to make the code base more dry then you'll understand what they mean instead of uh just the um definition that's here on Wikipedia maybe now you'll have a better idea of what it means to uh put those values into Central locations and and to not repeat yourself so hopefully this video was useful for you guys you know if you have any questions uh feel free to ask in the comment section below uh be sure to subscribe for future programming term videos and thank you guys for watching

Original Description

In this programming terms video, we will be going over the concept of DRY (Don't Repeat Yourself). This is a term you will hear a lot if you are just getting started in Computer Science or if you've recently started a new programming job. Let's take a look as to what it means to keep your code "DRY" ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 36 of 60

1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

The video teaches the DRY principle in programming, showing how to reduce code repetition and improve maintainability in Python. It demonstrates refactoring code to make it more efficient and easier to maintain.

Key Takeaways
  1. Identify repeated code
  2. Refactor code into reusable functions
  3. Apply the DRY principle to reduce repetition
  4. Use unit testing to ensure code quality
  5. Maintain code in a central location
💡 The DRY principle helps reduce code repetition, making it easier to maintain and modify code in the future.

Related AI Lessons

When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Explore how AI coding tools impact your identity as a software engineer and learn to adapt to the changing landscape of software development
Medium · AI
When AI Writes Most of My Code: What Happens to My Identity as a Software Engineer?
Explore how AI coding tools impact software engineer identity and adapt to the changing landscape
Medium · Programming
How AI Is Changing Software Development (2023–2026)
Learn how AI is revolutionizing software development with automated coding tools and techniques, increasing productivity and efficiency
Medium · Machine Learning
How AI Is Changing Software Development (2023–2026)
Discover how AI is transforming software development with automation, code completion, and more, and why it matters for developers' productivity and efficiency
Medium · Programming
Up next
Azure Security Priorities for 2026: Identity, Governance, AI Security & Zero Trust
Valto Microsoft Specialists
Watch →