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 ZeeshanPosted Jul 29, 2015, 3:50 AM
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 ZeeshanPosted Jul 29, 2015, 3:45 AM
Your reply is much appreciated, Vithal Wadje. Sure. :)
Vithal WadjePosted Jul 28, 2015, 11:59 PM
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 WadjePosted Jul 28, 2015, 11:55 PM
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, 8:45 AM
Nice article
Sibeesh VenuPosted Jul 28, 2015, 7:53 AM
Nice Share
Jacob RobinsonPosted Jul 28, 2015, 7:19 AM
Nice One..
Nilesh JadavPosted Jul 28, 2015, 6:25 AM
Nice one sir
Gopi ChandPosted Jul 28, 2015, 4:06 AM
Nice
Rahul Kumar SaxenaPosted Jul 28, 2015, 3:53 AM
Good Show
Neeraj KumarPosted Jul 28, 2015, 3:44 AM
Nice
RakeshPosted Jul 28, 2015, 3:18 AM
Good
Juan Carlos Ruiz PachecoPosted Jul 28, 2015, 1:16 AM
Almost good... but screenshots are from java but C# :/
Nihal AhmedPosted Jul 28, 2015, 1:14 AM
Nice One sir
Suresh MogudalaPosted Jul 28, 2015, 12:52 AM
good one thanks...
Debasis SahaPosted Jul 28, 2015, 12:44 AM
Nice One..
Rajeesh MenothPosted Jul 28, 2015, 12:18 AM
Nice One
Praveen KumarPosted Jul 27, 2015, 11:14 PM
Very nice
Pankaj Kumar ChoudharyPosted Jul 27, 2015, 8:46 PM
Nice Explain Sir......