Skip to main content
Engineering LibreTexts

2.7: Spaces and Such

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)


    It may seem a bit picky, but you will be required to use appropriate spaces to improve the readability of your code.

    The two lines of code are the same:

    if(myAgeValue>60 && myCarSpeed>75) {
    if (myAgeValue > 60 && myCarSpeed > 75) {
       myInsuranceRate = i + k / m;              //Yes - this violates the meaningful variable name rule

    Both of these statements are valid C++ code, both will work. The first one is just much more difficult to read, because there are very few spaces in the code. To help those who are having to read the code, and perhaps change it or maintain it, add spaces after every key word, before and after a variable name, before and after any operator (=, <, etc).

    One more...

    There is the issue of parenthesis, they can be your friend, use them to not only make sure the compiler knows what you want but also those looking at the code can be sure you know what you are doing.

       myInsuranceRate = i + k / m;              //Yes - this violates the meaningful variable name rule
       myInsuranceRate = i + (k / m);              //Yes - this violates the meaningful variable name rule

    Again - both of these statements work. The mathematical order of precedence says left to right, multiplication, division, addition then subtraction. Put the parenthesis around the division to show that is what you intended to happen first.  Do not simply assume that everyone understands what you are doing.

    It is about being professional and taking the time and effort to write good code that is readable and understandable. After 40 years in the software development world, I know this to be true - and you will find promotions and bonuses easier to come by if you do a few simple things.

    This page titled 2.7: Spaces and Such is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.