Magic Numbers
In programming hard-coding refers to puttng something in code that cannot be changed externally. A magic number is a hard-coded number in code. You should not use magic numbers in your code since it increases noise in the code. Let me explain what exactly it means.

Which of the following would you rather read?
- Aditi went to the #101 dealer to buy #20 #555
- Aditi went to the Ferrari dealer to buy a Red Ferrai-Enzo
Let's say you are writing a function to set the age of the user as given below. The age > 21 is not so readable to another programmer because 21 can be a legal driving license age or legal age for marriage or can be anything depending on the business needs. It's a magic number or it's been hard-coded into your code. So this should be removed.

You can make it a little more readable by declaring a constant legalDrinkingAge. If you want to still remove 21 from your class then you can add it to configuration files or property files and then read it at runtime. But the entire concept is to keep it more readable.

Another good way to remove magic numbers is by using enumerations (enum) . You have written a function to set the Status of a user, where you are using the condition status == 0 . What is no# 0 , it can be active status or inactive or locked or anything. So how to write better code? An enum comes to your help.

Let's first declare an enum StatusType and then modify the dirty code to make it more readable and clean.



Afzaal Ahmad ZeeshanTOP 100Posted Jul 28, 2015, 11:50 PM EDT
I have removed my comments with *bad language*, but Vithal Wadje please tell me, after reading the article. Do you think this is C#? Also, do you think the *clean code* is actually better? What would readers learn from this article? My intention was not to make him feel bad, he is a member of community and a community helps others even with more knowledge, when they are wrong. Just let me know what do you think of the content. I have anyways, removed the content that you said was not fair.
Afzaal Ahmad ZeeshanTOP 100Posted Jul 28, 2015, 11:45 PM EDT
Your reply is much appreciated, Vithal Wadje. Sure. :)
Vithal WadjeTOP 100Posted Jul 28, 2015, 7:59 PM EDT
Afzaal Ahmad Zeeshan use better language to talk other .these are the community contributor ,they given the time to write article in their busy scheduled . If you can not be wants to appreciate their contribution don't do it. but atleast use good language and manner to convey your message . I saw many your comments on twitter n other like same . don't do this .
Vithal WadjeTOP 100Posted Jul 28, 2015, 7:55 PM EDT
Hi Afzaal Ahmad Zeeshan don't make doubt on anyone capability .use soft words while conveying your message to other . I suggest you don't repeat these words in future comment. read your own comment and tell me it right language.
Vipul MalhotraPosted Jul 28, 2015, 4:45 AM EDT
Nice article
Sibeesh VenuTOP 100Posted Jul 28, 2015, 3:53 AM EDT
Nice Share
Jacob RobinsonPosted Jul 28, 2015, 3:19 AM EDT
Nice One..
Nilesh JadavPosted Jul 28, 2015, 2:25 AM EDT
Nice one sir
Gopi ChandPosted Jul 28, 2015, 12:06 AM EDT
Nice
Rahul Kumar SaxenaTOP 100Posted Jul 27, 2015, 11:53 PM EDT
Good Show
Neeraj KumarPosted Jul 27, 2015, 11:44 PM EDT
Nice
RakeshPosted Jul 27, 2015, 11:18 PM EDT
Good
Juan Carlos Ruiz PachecoPosted Jul 27, 2015, 9:16 PM EDT
Almost good... but screenshots are from java but C# :/
Nihal AhmedPosted Jul 27, 2015, 9:14 PM EDT
Nice One sir
Suresh MogudalaPosted Jul 27, 2015, 8:52 PM EDT
good one thanks...
Debasis SahaTOP 100Posted Jul 27, 2015, 8:44 PM EDT
Nice One..
Rajeesh MenothPosted Jul 27, 2015, 8:18 PM EDT
Nice One
Praveen KumarPosted Jul 27, 2015, 7:14 PM EDT
Very nice
Pankaj Kumar ChoudharyTOP 100Posted Jul 27, 2015, 4:46 PM EDT
Nice Explain Sir......