Hard-coding For Beginners

What is Hard-coding

Hard-coding refers to the practice of embedding specific values or data directly into the source code of a program or application instead of using a more flexible and dynamic approach.

This includes writing and using values directly in the code, typically by assigning them to variables or using them in calculations or comparisons.

Why Hard-coding Should not be Used to Answer Coding Questions

While hard-coding may provide a quick and simple solution in certain situations, it has several drawbacks. One of the main disadvantages is the lack of flexibility. Hard-coded values are rigid and fixed, making it difficult to modify or adapt the program without changing the source code. Any changes to the values require manual code modifications, recompiling, and redeploying the application.

What Is An Example of Hard-coding

function addTwoNumbers(x, y) {
  return 5 + 10;
}

The addTwoNumbers function above will always return 15 as it does not use its inputs to return the correct answer.

While the multiplyTwoNumbers below will only the return the correct product if a is 1 and b is 2, or a is 2 and b is 3.

function multiplyTwoNumbers(a, b) {
   if (a == 1 && b == 2) {
      return 2;
   } else if( a == 2 && b == 3) {
      return 6;
   } 
   // And so on
}

Hard-coding May Delay Your Learning

The point of trying to write code is to solve problems. If you attempt to short-circuit this process by hard-coding the testcases for example, you will not gain the skills needed to write flexible code that works for any input. You will also fail to learn how to write usable code in the real world where there are many possible problems to solve.

When Should You Hard-code

There is a time and place for hard-coding though. For example, when you need to quickly debug your logic or your solution. Say that you’re writing a recursive function and you want to test it, you can hardcode some test-cases to confirm it is working. Or if you are writing code that relies on apps that you do not control, you can test your code with hard-coded responses just to confirm that it works as you would expect.

To conclude, here’s a joke you may enjoy (credit: @plamoni):

  • Knock knock.
  • Who’s there?
  • 472
  • 472 who?
  • 472
  • What?
  • 472
  • I don’t get this joke…
  • 472
  • Wait, did you just hard-code your solution to always respond 472?
  • 472
13 Likes
Build a Telephone Number Validator Project - Build a Telephone Number Validator
Build a Palindrome Check Project
Build a Budget App Project - "Your code raised an error before any tests could run..."
Some of the tests pass and some don't. I know this isn't the way to actually do it but I wanted to see if it would work anyways and oddly it didn't
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build a Palindrome Checker Project - Build a Palindrome Checker
Build a Cash Register Project - Build a Cash Register
Build a Cash Register Project - Build a Cash Register
Build a Palindrome Checker Project - Build a Palindrome Checker
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build a Palindrome Checker Project - Build a Palindrome Checker
Learn Special Methods by Building a Vector Space - Step 10
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build a Cash Register Project - Build a Cash Register
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build a Lunch Picker Program - Build a Lunch Picker Program
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build a Palindrome Checker Project - Build a Palindrome Checker
Build a Markdown to HTML Converter - Build a Markdown to HTML Converter
Build a Palindrome Checker Project - Build a Palindrome Checker
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an Inventory Management Program - Build an Inventory Management Program
Build an Email Masker - Build an Email Masker
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG character - Build an RPG Character
Build an RPG Character - Build an RPG Character
Build an Email Masker - Build an Email Masker
Build an Email Masker - Build an Email Masker
Project Euler Problems 1 to 100 - Problem 99: Largest exponential
Build a Movie Ticket Booking Calculator - Step 21
Build a User Configuration Manager - Build a User Configuration Manager
Build an RPG Character - Build an RPG Character
Build a Prime Number Sum Calculator - Build a Prime Number Sum Calculator
Build a Discount Calculator - Build a Discount Calculator
Build a Set of Football Team Cards - Build a Set of Football Team Cards
Build a Number Pattern Generator - Build a Number Pattern Generator
Build an Email Masker - Build an Email Masker
Build an Apply Discount Function - Build an Apply Discount Function
Build an Apply Discount Function - Build an Apply Discount Function
Build an RPG Character - Build an RPG Character
Build an RPG Character - Build an RPG Character
Build an RPG Character - Build an RPG Character
Build an RPG Creature Search App Project - Build an RPG Creature Search App
Build an RPG Character - Build an RPG Character
Review JavaScript Fundamentals by Building a Gradebook App - Step 4
Build an Email Masker - Build an Email Masker
Build a Palindrome Checker Project - Build a Palindrome Checker
My project is really frustrating me
Review JavaScript Fundamentals by Building a Gradebook App - Step 1