Constants

Hello all.

Well, I'm still quite new at programming and utilizing self-study at home with Prata's C++ Primer Plus 6E, and on chapter 3, exercise 1. Well, I'll leave the code. The header comments give the directions, and my closing comments largely cover my thoughts on the exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 1HeightConversion.cpp -- convert inches to feet and inches.
// Inputs inches in integer, uses underscore for input, and
// utilizes a symbolic constant conversion factor.

#include <iostream>

int main()
{
	std::cout << "Please enter your height in inches:__\b\b";
	int inches;
	std::cin >> inches;
	std::cin.get();
	// Conversion for feet.
	const int feet = inches / 12;
	// Modulus for leftover inches.
	const int loinches = inches % 12;
	std::cout << "Your height is " << feet << " feet, " << loinches << " inches.";
	std::cin.get();
	return 0;
}

// The constants could have just as easily been replaced by simple integers.
// It feels as though I've either done this wrong despite the code working properly,
// am incorrectly utilizing const, and this entire exercise could and probably would
// have been done without using const at all. 


So, I'm trying not to get into bad habits, which is why you don't see namespaces (I read about that) and attempts are always made to use logical naming conventions, and spaced out for readability. I've done a bit of homework here, and the code does what it's supposed to do - no question there. I'm just concerned I'm doing something that could get me in trouble later, and I'm also thinking that, despite it being an exercise, declaring and initializing those two constants arbitrary and didn't serve much educational purpose.

I need people that know about this more than myself. Originally, I thought, "I'll just make the constant the entire conversion in one line, output the text where needed. It should accept integers and cout commands, right?" I was wrong, and if that's even possible it's well beyond my level.
Sutter and Alexandrescu in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices'
15. Use const proactively.

Summary

const is your friend. Immutable values are easier to understand, track, and reason about, so prefer constants over variables wherever it is sensible and make const your default choice when you define a value. It's safe, it's checked at compile time, and it's integrated with C++'s type system. ...

Discussion

Constants simplify code because you only have to look at where the constant is defined to know its value everywhere. Consider this code:

1
2
3
4
5
void Fun( vector<int>& v)
{  //...
   const size_t len = v.size();
   //... 30 more lines ...
}


When seeing len's definition above, you gain instant confidence about len's semantics throughout its scope (assuming the code doesn't cast away const, which it should not do). It's a snapshot of v's length at a specific point. Just by looking up one line of code, you know len's semantics over its whole scope. Without the const, len might be later modified, either directly or through an alias. Best of all, the compiler will help you ensure that this truth remains true.
...
Topic archived. No new replies allowed.