Credit card companies are responsible for a high volume of highly sensitive global network traffic per minute with no margin for error. These companies need to ensure they are not wasting resources processing unnecessary requests. When a credit card is run, the processor has to look up the account to ensure it exists, then query the balance to ensure the amount requested is available. While an individual transaction is cheap and small, the scales involved are enormous.
Credit Card Payment Method
Identifying Credit Card Types
Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together.
Add the sum to the sum of the digits that weren’t multiplied by 2.
If the total’s last digit is 0 then the number is valid!
Take for example the following American Express number, 378734493671000. Starting from the second-to-last-digit, multiply the last number by 2.
72 + 72 + 42 + 92 + 62 + 12 + 0*2
14 + 14 + 8 + 18 +12 + 2 + 0
Adding the product digits:
1 + 4 + 1 + 4 + 8 + 1 + 8 +1 + 2 + 2 + 0 = 32
Finally add the digits that were not multiplied to the sum
32 + 3 + 8 + 3 + 4 + 3 + 7 + 0 + 0 = 60
The checksum 60 ends with the number 0, therefore it a syntactically sound credit card number
Aside from the checksum, credit card number numbers also identify the type of credit card company. Visa cards start with the number 4. MasterCards start with the number 51, 52, 53, 54, or 55. American Express starts with the number 34 or 37.The Big Picture
The solution can be broken down into two parts:
Let’s take a look at the syntax for C and walkthrough the code.Card Length Validation
To check if the card number is valid, there is a preliminary check that we can check for before calculating the checksum. We know that a credit card number can only be either be 13, 15, or 16. We can do that with a simple while or for loop.
The user’s card number is stored in the variable card number and on each iteration of the length of the number the last digit will be removed and counted. The count of the length of the number will then be checked with if it is either 13, 15, or 16 digits.
After passing the first test, the next step would be to see if the checksum is valid. Again we, will take a look at the syntax in C.
In this example, the array number is declared and the card numbers are enumerated through and each digit is saved in the array number. The digits that are stored in the array using this method is reversed from the original card number because of the operation of removing the last digit first and storing it in the first index.
Take for example the credit card number is 4012 8888 8888 1881. Using the modulo and divide by 10 method to store the array the resulting array would be [1,8,8,1,8,8,8,8,8,8,8,8,2,1,0,4].
The checksum for C was cleaner to implement for this reason that there were less type conversions needed to manipulate the data. The array used simple for loops and conditional statements to validate the number.
In the C code above, a new array was created to clone the number array and starting from the second-to-last value the value was multiplied by 2.
The bulk of the validation occurs in this nested if statement. First the length of the card number is determined. Then the arrays digits are added up and checked if the checksum is valid in this one line.
sumdigit = (number[i] % 10) + (number[i]/10 % 10);
The type of card is validated by checking the first and second index of the array. In this example, Visa cards start with the number 4.
cardarray == 4 && accumulator % 10 == 0
The array that has been multiplied by 2 is then summed and added into the array that has not being multiplied using the reduce method. If the checksum passes, then the first two digits of the card array is sliced to check what type of card. The implementation of the card type check is similar to the C syntax. By using a conditional statement the digits of the card array is then evaluated for each type.
This post is more of a self reflection on the differences in programming in a lower level language and a higher level language. In attempting to create a credit card checker, I have found that the lower level language syntax to be more concise in getting to the solution, whereas using the higher level language requires data type conversions to use the higher order functions.
I’m still learning more about code every day. I would love to hear from you, if you have any tips or suggestions.
Thank you !
In this post, you'll see the difference between Rust and C/C++ in a developer’s perspective
Originally published by Maourice Gonzalez at https://www.onmsft.com
C++ is an incredibly fast and efficient programming language. Its versatility knows no bounds and its maturity ensures support and reliability are second to none. Code developed in C++ is also extremely portable, all major operating systems support it. Many developers begin their coding journey with the language, and this is no coincidence. Being object-oriented means it does a very good job of teaching concepts like classes, inheritance, abstraction, encapsulation and polymorphism. Its concepts and syntax can be found in modern languages like C#, Java and Rust. It provides a great foundation that serves as a high speed on ramp to the more popular, easier to use and modern alternatives.
Now it’s not all rosy. C++ has a very steep learning curve and requires developers to apply best practices to the letter or risk ending up with unsafe and/or poor performing code. The small footprint of the standard library, while most times considered a benefit, also adds to the level of difficulty. This means successfully using C++ to create useful complex libraries and applications can be challenging. There is also very little offered in terms of memory management, developers must do this themselves. Novice programmers could end up with debugging nightmares as their lack of experience leads to memory corruption and other sticky situations. This last point has lead many companies to explore fast performing, safe and equally powerful alternatives to C++. For today’s Microsoft that means Rust.
The majority of vulnerabilities fixed and with a CVE [Common Vulnerabilities and Exposures] assigned are caused by developers inadvertently inserting memory corruption bugs into their C and C++ code - Gavin Thomas, Microsoft Security Response Center
Rust began as a personal project by a Mozilla employee named Graydon Hoare sometime in 2006. This ambitious project was in pre-release development for almost a decade, finally launching version 1.0 in May 2015. In what seems to be the blink of an eye it has stolen the hearts of hordes of developers going as far as being voted the most loved language four years straight since 2016 in the Stack Overflow Developer Survey.
The hard work has definitely paid off. The end result is very efficient language which is characteristically object oriented. The fact that it was designed to be syntactically similar to C++ makes it very easy to approach. But unlike the aforementioned it was also designed to be memory safe while also employing a form of memory management without the explicit use of garbage collection.
The ugly truth is software development is very much a trial and error endeavor. With that said Rust has gone above and beyond to help us debug our code. The compiler produces extremely intuitive and user friendly error messages along with great direct linking to relevant documentation to aid with troubleshooting. This means if the problem is not evident, most times the answer is a click away. I’ve found myself rarely having to fire up my browser to look for solutions outside of what the Rust compiler offers in terms of explanation and documentation.
Rust does not have a garbage collector but most times still allocates and release memory for you. It’s also designed to be memory safe, unlike C++ which very easily lets you get into trouble with dangling pointers and data races. In contrast Rust employs concepts which help you prevent and avoid such issues.
To how fast and predictable its performance is. Everything in our lives evolves. Our smartphones, our cars, our home appliances, our own bodies. C++ while still incredibly powerful, fast and versatile can only take us so far. There is no harm in exploring alternatives, especially one as exceptional and with as much promise as Rust.
What do you guys think? Have you or would you give Rust a try? Let us know your thoughts in the comments section below.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Well… there were a few well-thought reasons behind this decision.
Python is a great language. I love it! It’s free of clutter and is simple to use. It also has better support for OOP features. So, why didn’t I choose Python? Because based on my research, Python is often used among people in the data science community, rather than professional software engineers.
Yes, you we use Python to build desktop and mobile apps as well as backends. However, Python is not the language of choice for building mobile or desktop apps. These days, the majority of people are building mobile apps using cross-platform solutions such as React Native or Flutter. Others do native development in Java (for Android) or Swift (for iOS). The same applies to building desktop applications with Python. Python is not very well known for that reason.Why not C#?
C# and Java are very similar in terms of syntax and they both have great support for OOP features. So, why didn’t I choose C# given that I have several C# courses?
Well… I looked at different sources to see the popularity of Java vs C#. Google Trends is a great resource. As you saw earlier in the post, Java is way more popular than C#. Does it mean it’s a better language? Well, it depends on _how _we define “better”.
What is better? A Ferrari or a truck? It depends on what you want to do. Ferrari is a super-fast car but it’s useless if you want to use it to move house. Programming languages are the same. There is no such thing as the best programming language. Every language has pros and cons. C++ is well known for its speed. Python is popular for its simple and clean syntax as well as tons of resources for machine learning.
Just because Java is more popular, it doesn’t mean it’s a better language than C#. In my opinion, C# is a more feature-rich language but Java was marketed better.
C# and Java have been rivals since day one. Many of the features we’ve had in C# for a long time were introduced in Java years later. Some features don’t exist to this day! For example, in Java, we don’t have optional parameters which are very handy. We have to overload a method. Ugly! Jave interfaces are also bad.
I personally prefer C#. But as I said, Java has had better marketing and that’s why it’s always been more popular than C#.Why Java?
So, I chose Java because it’s used more widely than C# amongst professional software engineers. There are more Java developers out there than C# developers. Most universities and colleges also tend to teach Java, rather than C#.
Now, all that aside, C# and Java are very similar in terms of the syntax. Recently I posted this tweet asking my students if this is Java or C# code?
The reality is I wrote this code in IntelliJ (a popular Java IDE). But the code is valid C# and Java code. Someone argued that this is an over-simplified example and shouldn’t be used as a basis for comparison. That is true. But my whole point is that if you understand this code, you can perfectly take my Data Structures or Design Patterns courses.What You Should Take Away
In my opinion, Java in the programming world, is like the English language in the world we live in. If Italian or Hindi is the only language you speak, you’re probably not gonna get very far ahead.
If you understand English, you have access to tons of valuable resources. Whether you want to learn programming, or painting, or piano, if you “understand” English, you have tons of resources in front of you. You don’t need to be fluent in speaking English. All you need is to just understand it.