Changes between Version 12 and Version 13 of HowtoGoodProgrammingTechniques


Ignore:
Timestamp:
Feb 19, 2013, 2:56:54 PM (12 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • HowtoGoodProgrammingTechniques

    v12 v13  
    6464When the application becomes big and start having a lot of names for variables, it is good to know what's exactly that variable, to know if we are dealing with a file, with a directory, or with a value, I personally use for that a suffix (ending in) in the variables like '''_v''' for values, '''_d''' for identifying directories, '''_f''' for identifying a file.
    6565
     66----
     67Finally, since this is the best tip that you can have when coding, I included this ''must-read'' article from makinggoodsoftware.com directly here:
     68
     69==== 1. The variable name has to be as descriptive as possible. Don´t use generic names. ====
     70
     71Good example: daysDateRange, flightNumber, carColor.
     72
     73Bad example: days, dRange, temp, data, aux…
     74
     75There are a lot of developers who would prioritize short variable names over descriptive names because that will save time when typing the code, but who cares? It doesn’t help to improve the quality of the software, it only saves a few minutes per day (maximum) per developer, by the other hand, when having descriptive variable names, the overall quality of the software will increase because it will be easier to modify and read the code.
     76
     77==== 2. The variable name has to be as short as possible. ====
     78
     79This rule goes hand by hand with the first rule, the variable names should be as short as possible but as descriptive as possible.
     80
     81This basically means that while we still try to come up with short names, we prioritize descriptive names over short names, we only pick the shorter one when they are as descriptive as the longer one.
     82
     83Bad Examples: howLonDoesItTakeToOpenTheDoor, howBigIsTheMaterial...
     84
     85Good Examples: timeToOpenTheDoor, !MaterialSize.
     86
     87==== 3. It´s OK to use abbreviations, but explain the abbreviation with a comment. ====
     88
     89Sometimes to have a good descriptive name is necessary to make very long variable names, which is fine, but the variable size can be improved by using abbreviations, just make sure that when the variable is declared there’s a comment that explains the meaning.
     90
     91==== 4. Use proper Hungarian notation when necessary. ====
     92
     93There’s a great post from Joel on software that explains what the proper Hungarian notation is and how to use it. Basically it says that when coding a method where we have variables holding the same data, but with different types, the Hungarian notation can help us.
     94
     95Example: Imagine a method that receives apples, then it classifies the apples depending in their color, and finally discards the ones that are not matured enough. We could use in this example the following variable names:  incoming_apples, red_apples, yellow_apples, notMaturedYellow_apples, red_apples, maturedGreen_apples...
     96
     97==== 5. Don´t use negative logic for your variable names. ====
     98
     99Good Example:  !IsEnabled.
     100
     101Bad Example: !IsNotEnabled.
     102
     103It’s always easier to read and undertand an statement that is expressed in a positive language that in a negative language.
     104
     105==== 6. Be consistent. ====
     106
     107If you have used a variable name called customer in a method, then don’t call it client in the next method, or if you use an abbreviation in one method, use it for all the methods the same.
     108
     109==== 7. Map the application domain terminology with your names. ====
     110
     111In different domains, different concepts have very specific and different meanings, for example, “order” doesn’t always mean the same in the different domains, try to map these specific concepts with your variable names so that when you name something with a concept from the business domain it means exactly the same.
     112
     113Example. If your customer just considers “order” an “order” that has been approved, don’t call “order” to a non approved one in your code, call it “nonApprovedOrder”.
     114
     115==== Golden rule. Spend a few minutes thinking about your variable names. ====
     116
     117If you find yourself writing variable names as soon as you need them in your code without even thinking on their name for a second, you are probably picking bad names for them.
     118
     119If you always try to have good variable names, and you have found yourself thinking a couple of minutes for the best name for a variable, even if you don’t follow the other tips, you are likely to have pretty good variable names in your code.
     120
     121----
     122
    66123
    67124== The 5 steps coding procedure ==