1. Improving your code

1.1 Readability

One of the most important things you can do is make sure your code is readable. This means using whitespace freely, consistent indentation, etc..

This is valid C++ code:

for(int i=0;i<10;i++){cout<<"i is "<<i<<endl;}

but it looks bad. This is C++ code equivalent to that:

for (int i =0; i<10; i++)
{
    cout << "i is " << i << endl ;
}

and it looks much better. There are no wrongs or rights, but be consistent.

1.2 Fault tolerance

Check e.g. the snippet

for (Int_t i(0); i < iTracks; i++) {
    // loop over all the tracks
    AliAODTrack* track = static_cast<AliAODTrack*>(fAOD->GetTrack (i));
    // fill our histogram
    fHistPt->Fill(track->Pt());
}

We can build in fault tolerance

for (Int_t i(0); i < iTracks; i++) {
    // loop over all the tracks
    AliAODTrack* track = static_cast<AliAODTrack*>(fAOD->GetTrack (i));
    if(!track) continue;
    // fill our histogram
    fHistPt->Fill(track->Pt());
}

1.3 Comment your code

Finally, it's a very good idea to comment your code. Comments improve readability and maintainability.

Comments should be useful, though, and comments that are overly obvious can be avoided...

// no comment : bad

a ++;


// pointless comment , also not so good

a ++; // adding 1 to a


// descriptive comment , very good

a ++; // adding 1 to a to make a point during a tutorial

It's also a good idea to document your code as you're writing it - you will forget how it works and no-one will continue with uncommented tasks.

results matching ""

    No results matching ""