Clean Code, Reflection, Agile Testing and Debugging, C++ Code

If you are like most of the true software developers, you spend majority of your time at job on debugging testing troubleshooting rather than design and development.

That’s one of the major reasons why it matters to favor Code With Automated Tests over well designed code. Check out the linked pdf file, a very enjoying reading. Here we just look at some screenshots from the linked pdf file:

Repeat it: First write dirty code, then clean it!

Repeat it too: Code With Automated Tests over well designed code!

This is when Reflection comes into our sight.

Reflection, is one of the most helpful techniques used in automating development and testing. Reflection can be an important technique used in many of the software productivity stack (from the same linked pdf file):

Reflection can be used in the upper three layers in this five-layer stack.

C/C++ Reflection

Reflection exists in most of the modern programming language, only except C++. It is important for developers to have a pragmatical approach to automate the debugging and testing of their code. Whenever I am thinking about this, reflection comes always to my mind.

Below the content is copied from Morsi dot Org. (You are strongly suggested to read the original posting by Morsi, I just copy the instructions here for your convenience to copy and paste the commands or directory paths) .

C++ Reflection w/ Reflex

Submitted by mmorsi on Sat, 2009-05-09 18:58

Today I’m going to briefly discuss the C++ reflection library and corresponding utilities known as Reflex. I’m still working strong on my to be announced / released side projects, and have been using Reflex to utilize some reflection / introspection into my C++ apps. While I love C++, it has it flaws, and one of the biggest and most annoying in my opinion is the lack of reflection and the (imho subborn) insistance by the inventor to not include it. Every modern powerful language / compiler has reflection / introspection included, with the exception of C++, and until the day it’s included, C++ will never be truly complete (and honestly the external solutions such as Reflex and the others can’t come close as they are no where near as optimized as it could be if it were in the compiler (also RTTI doesn’t cut it))

Don’t get me wrong, Reflex works great, and albeit a single issue (discussed in a bit) its has done everything I’ve needed it to. You can check out Reflex via svn at the project site, and compiling is a straight forward configure / make process. I haven’t found any existing rpm,deb,etc packages for Reflex, so you’ll have to build from source, but I may look into making some especially if my projects take off. Reflex also relies on gccxml to parse the C++ source code into xml, which also needs to be checked out and compiled.

Once you compiled gccxml and reflex, use the python/genreflex/ executable to generate the reflection dictionary, necessary classes, and every else needed from your code like so:

python python/genreflex/ [input_headers]\
-o [outputdir] –gccxmlpath [gccxml_bin_dir]\
–gccxmlopt=”–gccxml-gcc-options [gccxml_options_file]” –debug 1

(there is also a executable which I’m assuming handles and updated specification of the c++ language but I’m not sure as I haven’t used it)

This will take the classes defined in any headers you specify, compile them using the gccxml in the directory you specify (if you don’t do a ‘make install’ the directory should be /build/bin/ under your gccxml checkout dir) with the gcc options as specified in gccxml_options_file (eg -Iincludepaths -g or whatever), and output the result in the -o directory you specify.

You can then compile the files in this directory into your project / include the generated class headers where needed, and utilize the Reflex API (-I[reflex_dir]/reflex/inc) to interact w/ these classes, utilizing introspection (see some examples).

Its a fairly straightforward process and with minor debugging / tweaking you should be good to go….

So I thought until the other day when I was scaling up the number of parallel tasks going on and discovered Reflex is not fully thread safe. To make a long story and lots and lots of debugging short, it seems there is an issue with Reflex where when it generates wrapper methods and functions around ones that return simple / fundamental data types (eg. int / float / etc), it statically stores the return value in the generated method and simply returns the address of that variable. Thus if two threads are trying to invoke that method, even if they have different instances of the object, the static data will be overwritten, and at least one of the threads will have the wrong data. Instead it should allocating a new instance of the simple data type on the heap and return the address of that, as it does w/ non-primitives. After a bit of debugging, I’ve come up with a simple patch (it is against the 25049 revision and currently reflex is at 28507, but it is simple) which from what I’ve seen works (and hopefully doesn’t break anything) and which I have yet to submit to the Reflex community, but will upload here for the time being (attached). If you want to debug this as well, add “AM_CXXFLAGS = -g” to src/ in your reflex checkout directory before re- configuring, compiling, linking.

Hope this all works for you and helps, I’ll look to writing more on the topic, and actually maintaining this blog 🙂

Design Matters

Ok, enough details as to how to use Reflex for C++. Now back to clean code, still from the same linked pdf file:

It simply says it is really important to have a good design. The architecture people used to say that is the only most important thing to have. Are you going to study these books, cover to cover, twice? After that, will you be able to do the best software all of a sudden?

The advice from the author, a bit surprising, is to join a Dojo. This is why:

Repeat: No good and bad code. Just your code and my code. Get it?

More readings:


About minghuasweblog

a long time coder
This entry was posted in All, C/C++, Methods and tagged , , , . Bookmark the permalink.

One Response to Clean Code, Reflection, Agile Testing and Debugging, C++ Code

  1. Pingback: Technical Topics | minghuasweblog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s