C++
  Home arrow C++ arrow C++ Programming Tips
Iron Speed
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

C++ Programming Tips
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-12

    Table of Contents:
  • C++ Programming Tips
  • Modifying Objects
  • Advanced Parameter Passing
  • List
  • Pointers and Dynamic Memory

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    C++ Programming Tips
    (Page 1 of 5 )

    In this article, I give you programming tips that can improve your C++ code. I will cover use of parentheses, long expressions, modifying objects, and more. By the time I'm done, you will hopefully have learned a few ways to make your code faster as well as easier to understand and maintain.

    C++ Fundamentals

    Use of parentheses

    Consider this expression without parentheses:

    a * b + c / d – 4

    and the equivalent with parentheses:

    (a * b) + (c / d) – 4

    Both expressions perform exactly the same computation, but the meaning of the second one is clearer, since parentheses are used.

    Expressions (like the two above) are evaluated by following the rules of precedence. The above expressions are the same. So from a technical point of view, you do not need parentheses. However, including parentheses makes the order of evaluation explicit to the person maintaining your code (even you).

    Long expressions (Style)

    We often need to write long expressions that will not fit on one line of the screen. For example, the expression

    cout << Mass << “grams of a hydrocarbonnwith “<< CarbonAtoms << “ carbon atom(s) and “
    << HydrogenAtoms << “ hydrogen atom(s)ncontains “ << Molecules << “molecules” << endl;

    is quite long and will not fit on one line of the screen. A good way to break a long expression into a multiline expression is to use continuation lines. These lines should always begin with an operator and be indented one space. Both of these serve to signal the reader that the line is a continuation of the previous line. So the above example can be written as follows:

    cout << Mass << “grams of a hydrocarbonnwith “<< CarbonAtoms

    << “ carbon atom(s) and “ << HydrogenAtoms << “ hydrogen atom(s)ncontains “

    << Molecules << “molecules” << endl;

    As another example, the expression

    ((Xcoord1 – Ycoord1) / 2 ) * Distance1 + ((Xcoord2 – Ycoord2) /

    2) * Distance2;

    can be better written as

    ((XCoord1 – Ycoord1) / 2 ) * Distance1

    + ((Xcoord2 – Ycoord2) / 2) * Distance2;

    Speed versus clarity and correctness

    One rule of thumb in programming is that 90 percent of a program’s running time is spent on 10 percent of the code. So without some idea of where a program spends most of its time, most changes to a program to speed it up will have little or no effect on the overall running time. While speed is important, clarity (presentation of the code) and correctness are always more important. We should never sacrifice clarity and correctness for speed.

    Hot spots are where a program spends most of its time. If speed becomes an issue, a more effective approach is to complete the writing of the program and then order a tool which can be used to identify the hot spots of the program. The hot spots can then be tuned (recoded) to reduce the running time of the program.

    More C++ Articles
    More By Chrysanthus Forcha


       · This article gives a C++ programmer confidence. After reading the article, you will...
     

    C++ ARTICLES

    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection
    - Temporary Variables: Chasing Temporaries Away
    - Temporary Variables: Temporaries Are Not Nec...
    - Temporary Variables: Keep Your Values Close,...
    - Temporary Variables: Procrastination is the ...
    - Who`s Afraid to Be Const Correct? Help Your ...
    - Persistent Data: File Input and Output


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway