Programming Terms: Idempotence
Key Takeaways
This video explains the concept of idempotence in programming, using Python code examples to illustrate the concept, and discusses its application in HTTP methods such as GET, PUT, and DELETE.
Full Transcript
hey how's it going everybody this is going to be another video on programming terms and in this video we're going to look at the term idempotence so idempotence is actually a pretty easy concept to grasp once you see it in action but some of the definitions can be a little confusing if you look at Wikipedia the definition for idempotence it says the property of certain operations in mathematics and computer science that can be applied multiple times without changing the result beyond the initial application so if you're still a little confused by that then you will usually see this example here they'll say now what this example is saying is that if you have a function f and parameters X the result from f of X if you pass that into the function f again then that should equal the result of just f of X being run one time now if that's still a little bit unclear to you I think it'll clear it up once we see an example of this in some working code now the code that I have here is Python code but you know you can use any language for this it's it's language agnostic it's it's the same definition for any language that you use but just to show you what this is I'm using Python code here so I have a function here called add 10 and I have a parameter that I'm passing in to add pin that is num and if we compare this to our f of X from the last slide imagine that add 10 is our F and num is our X so now if you look down here you can see that I am printing out add 10 and I'm just going to go ahead and pass in 10 to that function so if I run this then you can see down here that my result is 20 now if we remember back to the definition of something that is idempotent it means that if we pass this result into the function again and we should get the same result that we got the first time so if I take this function and I wrap it around our result here and I run it again you can see that now we get 30 so by definition this is not idempotent because whenever we have F of 10 and we wrap that inside of F again that result we get is 30 and whenever we just do F of 10 that result is 20 so you can see if we map this to the definition here then this by definition is not item potent so let's look at an example of something that is idempotent so I have here let me comment this one out so there's a built in function in Python abs it just gives you the absolute value of a number so if I run this the absolute value of negative 10 is 10 but now if I take the absolute value of the absolute value and run it again it's 10 so I can keep doing this over and over and every time I pass in the result back into absolute the absolute value again no matter how many times you pass that value in it's still going to give you 10 every time and that's what item Potence means so hopefully that kind of clears that up and it doesn't have to be a function either I mean you can have a line in your code that is a equals 10 or something like that just an assignment and this is a line of code so it can be executed and no matter how many times you execute it in a row equals 10 equals 10 equals 10 run it over and over over every time it's going to do the exact same thing so that's an idempotent statement also so really the simple definition of idempotence is that whenever you do something over and over and over and over and over and you do the same thing that you get the same result back every time and that you're not building on top of that result now where you usually see this is in HTTP methods so a lot of these are considered idempotent so a get HTTP method is considered item potent so because if you go to a URL and you're just getting the URL for this you sir no matter how many times you reload this page and reload this page it's going to give you the same result every time it's not changing anything now put is considered idempotent also because put is usually used for updating values so say you update the same parameter to the same value every time and say you want to set you know a user equal to Cori and then you submit that if you submit the update to set user equals to user equals Cori again and again and again every time it's just going to set that user equal to Cori and it's not changing the value so that's considered an item button as well now the method that's not considered item potent is post so post is used for changing data in the background so say for example you have a website with a voting system and every time you go to a certain URL it casts a vote so every time you go to that URL and it casts a vote at Casa vote and cast a vote that's not idempotent because every time you go to it you're getting a different result back it's either going to vote up or down based on the URL that you're going to so every time you get back a different different result so by definition post is not idempotent now lastly elite is considered idempotent so this one's a little bit confusing to some people because you would think that if you deleted a user and then you tried to deleted that user again maybe you'll get a 404 because that user no longer exists but you have to think about the the state of your server and the effect that it's having so say you delete user one two three here whenever you try to read elite and read elite and read elite it's already deleted that user so you can run that multiple multiple times again and again and again and it's not changing anything additionally on your server so the state of your server is still the same no matter how many times you call in a row so after the one time you call it every time after that gives the same result so it is considered idempotent so I hope that makes that more clear just remember whenever you run a function or do an operation after the first time the value can change on the first time like for example absolute value of 10 it changes from negative 10 to 10 but if you keep passing it in and keep passing in and pass your result in over and over and over if you always get the same result that's considered idempotent so anytime you hear somebody mentioned item potent source umthe enlike that you'll know what your what they are talking about so hopefully this video is useful for you guys be sure to subscribe for future programming term videos if you have any questions just ask in the comment section below and thank you for watching
Original Description
In this programming terms video, we will be going over Idempotence. Idempotence is the property of certain operations in mathematics and computer science, that can be applied multiple times without changing the result beyond the initial application. Let's take a look at this definition in-depth and go over a few examples.
The code from this video can be found at:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Idempotence
✅ 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 · 38 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
▶
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Web fonts using CSS Font Face
Corey Schafer
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
Paver Patio Time Lapse
Corey Schafer
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
WordPress Plugins: Imsanity
Corey Schafer
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
Python Tutorial: if __name__ == '__main__'
Corey Schafer
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
Easily Resize Multiple Images Using Picasa
Corey Schafer
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
Git Tutorial: Using the Stash Command
Corey Schafer
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
Git Tutorial: Diff and Merge Tools
Corey Schafer
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
Python Tutorial: str() vs repr()
Corey Schafer
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
Programming Terms: String Interpolation
Corey Schafer
Programming Terms: Idempotence
Corey Schafer
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
Programming Terms: Mutable vs Immutable
Corey Schafer
Python Tutorial: Else Clauses on Loops
Corey Schafer
Overview of Online Learning Resources
Corey Schafer
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
Programming Terms: Combinations and Permutations
Corey Schafer
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
Setting up a Python Development Environment in Eclipse
Corey Schafer
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer
🎓
Tutor Explanation
DeepCamp AI