Lesson 5: Hard-Coded Havoc: The Return of Clean Code
[CFL2M] ColdFusion Modernization: Avoiding the Perils of Hard-Coded Values
*Note: You're reading a lesson from our ColdFusion Legacy 2 Modernization E-Course [CFL2M]. Interested in getting the full course? Click here to sign up.
Like the One Ring in Tolkien’s Middleware-earth, hard-coded values, or "magic numbers," can wield unexpected power and chaos across your codebase.
Hard-coded values might appear as innocent values — like mysterious figures placed arbitrarily in the code. Yet, they harbor hidden meanings that can disrupt your application’s logic and flow if they change unexpectedly.
[Also- virtual high five if you completed last week’s lesson and used Commandbox to solve the bug problem!]
Some examples with the hard coded value in bold.
- <cfmail from="[email protected]"...>
- <cffile action="read" file="C:\uploads\myFile.txt" variable="fileContent">
Other pains of hard-coded values include:
- Confusion: Like a cryptic message from Gandalf, magic values leave us guessing their origins and meanings.
- Scalability Concerns: Hard-coded values do not allow the code to adapt easily to varying environments or configurations.
- Maintenance Nightmares: If a value needs updating—especially if it serves dual purposes—the task can resemble Bilbo’s journey: perilous and full of riddles.
- Security Risks: Sensitive information, when hard-coded, can end up in source control. You don’t want API keys or passwords in a public repo!
What to do about this? Simple- eliminate all magic values from the main code.
Aragorn Action Step: Seek out and list the 3 most used hard-coded values in your legacy CF app. Replace these magic numbers and values with descriptively named variables.
For example, instead of num = 2, use numOfDepartments = 2. Store these values in configuration files outside your webroot folder or save them in your database. Or, use tools for managing secrets, such as the AWS Secrets Manager.
Advanced tip: If you are a wiz at using Regular Expressions in your IDE search, they might help you find magic values in a sea of code.
Let’s not just be "hard-core" about coding standards, but smart-core about removing those mischievous hard-coded values!
Once you clear this clutter, you'll breathe easier knowing your applications are running more smoothly and securely, free from the chaos of outdated and risky practices
Best,
Michaela Light
PS Next week, we’ll tackle the essentials of source control, ensuring your changes are never lost or overwritten. We’ll also guide you through the process of undoing changes smoothly. Stay tuned!