Lightning talk #4 – How to “read” the code and what information can you extract from it

Example 1 – In-class keywords

Impression ?

  • We have a class that holds a number, can increment its value, can fetch it and can display it as a string.

What about now ?

  • The constructor is explicit now, so previously we could have initiated it directly with an int
  • We have two const functions, which makes sense, and also inc() is non-const because it modifies the value, correct.

The compiler doesn’t care about that const – it’s a message given to others.

What is the meaning for an absence of a keyword?

  • I’ve thought about it and i don’t need it
  • i never heard of it or haven’t considered it


Example 2 – ranged-based for

Ok, the ranged-based for, cool.

Now you reevaluate the first one:

  • maybe it’s a low-cost object
  • this one is expensive to copy, or non-copyable

Now you reevaluate the second one

  • probably we change the employee inside


These are not VALID conclusions. If it’s not const, it doesn’t mean we modify it.
It does, or it doesn’t – we don’t know it yet – we need to read further.


Example 3 – Parameter passing

  • Low-cost? Did we forgot to make it reference? do we move it afterwards?

  • Do we modify it inside the function ? Should we?

Example 4 – is a raw pointer always a non-owning pointer?

  • Should we delete it ?
  • Can we change to what it points to ? Can it come back null?

  • Who manages the lifetime of the pointer? It was sent so we can peek on the message ? should it be const instead?

You cannot figure it out from here. Read the rest of the code.
Is there a lot of new and delete ? Then they do new and delete themselves.
Are there default or empty destructors? Do they use lot of smart pointers? Then you received an observable pointer only, and it’s lifetime is managed by someone else.

Example 5 – Using traditional for loop

  • Is there a reason for that?
    • Some people haven’t learned C++11 yet
    • Some people default to traditional for and use ranged-for only in some cases
    • Some people use traditional for only when it’s the case
  • Why did the traditional for was chosen ? Is there a reason not to use ranged-based for?
  • Does it touch every element or not ? Or they just like using it?
  • Why is a loop and not an algorithm (find, count, all_of, sort)?

Are there algorithms in the code? If so, your choice of a loop got my attention.

Example 6 – Class initializers

  • Why c was not initialized?
    • Maybe there’s a member initializer in the class?
    • Maybe it’s set in the braces? And if so, why there?
    • Maybe it was added later and forgot about it ? Or used only in some of the constructors?


What’s NOT in your code?

  • Think about what you’re not including or doing
  • Think of other ways you could have done this
  • Think of the other choices you could have made

Other people should understand you and your defaults. Show who you are and how you write code.


Example 7 – Naming

  • Which one is a member variable?
  • Which one is a static?
  • Which one is a parameter?
  • Which one is a local?

  • Is it easier to differentiate now.
  • All that start with m are class variables. s is for statics. _ is for parameters. none of those, for locals.


You may also like...

Leave a Reply