CppCast - Clean Code
Episode Date: March 10, 2016Rob and Jason are joined by Arne Mertz to discuss Clean Coding techniques. Arne is a Software Engineer at Zühlke Engineering, a blogger and a clean code enthusiast. He has been maintaining an...d developing large financial C++ applications for several years. Arne has a diploma in physics and has written some scientific code for his degree courses in Fortran77 and C++ before he started his programming career. Currently he is broadening his view on the software development world by doing test automation, integration, requirements engineering and tooling for a large Java/JavaScript web application. To keep in touch with C++ he continues to write about it on his blog, reads other blogs and watches videos of conference talks. In his free time he sings in a choir together with his wife and enjoys playing video games. He likes to travel a lot, especially tall ship sailing. News Upcoming features in GCC 6 Core C++ lvalues and rvalues Trip Report: C++ meeting at Jacksonville Arne Mertz @arne_mertz Arne Mertz on LinkedIn Links Simplify C++ Soft Skills: The software developer's life manual
Transcript
Discussion (0)
This episode of CppCast is sponsored by Undo Software.
Debugging C++ is hard, which is why Undo Software's technology
has proven to reduce debugging time by up to two-thirds.
Memory corruptions, resource leaks, race conditions, and logic errors
can now be fixed quickly and easily.
So visit undo-software.com to find out how its next-generation
debugging technology can help you find and fix your bugs in minutes, not weeks.
CppCast is also sponsored by CppCon, the annual week-long face-to-face gathering for the entire C++ community.
Get your ticket now during early bird registration until July 1st.
Episode 48 of CppCast with guest Mertz recorded March 8th, 2016.
In this episode, we talk about news from the Jacksonville C++ Standards Meeting.
Then we talk to Arna Mertz.
Arna talks to us about clean coding for C++ developers by C++ developers.
I'm your host, Rob Irving, joined by my co-host, Jason Turner.
Jason, how are you doing today? I'm doing well, Rob. How about you?
I am doing pretty well. I don't think I have any personal news to share this week. You?
Not yet. I'm starting on some side projects.
I might want to discuss them next week a little bit.
Okay. We'll save that for next week then.
All right. Cool. So at the top of every episode, I like to read a piece of feedback.
This week, I got an interesting tweet uh to the cpp cast account uh it looks like red
bull is mentioning c++ in an advertising campaign uh i don't really understand it at all they've
got this billboard it says from c++ to c suite and i i guess anyone who's not a programmer
probably has no idea what this means.
But if you are a programmer,
do you really want to go from C++ to the C-suite?
I don't know.
Yeah.
I mean, you wouldn't be able to program anymore, you know?
You'd be some executive, some manager,
and you wouldn't be coding anymore.
I don't get this at all.
It's strange.
Yeah.
Oh, well.
Anyway, we'd love to hear your thoughts about the show.
You can always reach out to us on Facebook, Twitter, or email us at feedback at cppcast.com.
And don't forget to leave us reviews on iTunes as well.
Joining us today is Arne Mertz.
Arne is a software engineer at Zulk Engineering, a blogger, and a clean code enthusiast.
He's been maintaining and developing large financial C++ applications for several years.
Arne has a diploma in physics and has written some scientific code for his degree courses
in Fortran 77 and C++ before he started his programming career.
Currently, he's broadening his view on the software development world by doing test automation,
integration, requirements engineering, and tooling for large Java and JavaScript web
applications.
To keep in touch with C++, he continues to write about it on his blog, reads other blogs,
and watches videos of conference talks.
In his free time, he sings in a choir together with his wife and enjoys playing video games.
He likes to travel a lot, especially tall ship sailing.
Aron, welcome to the show.
Hi, thank you for having me. wow tall ship sailing uh what is that um actually we are having a break it's called it's a it's a ship of i think about 50 meters long so it's think 164 feet long and a square rig chip show two two masts and about 18
sails wow rather rather big how many people do you need to man all those sails um the bare minimum in three shifts is 16 people and we get a maximum of 44 on board wow so is that a hobby or is that
yes it's a it's a hobby yeah well that sounds pretty cool yeah very cool okay well we had a
couple of news items to go into uh the first one jace i going to let you introduce this, upcoming features in GCC 6.
Yeah, you know, I learned about the Red Hat developer blog one or two episodes ago because we covered an article there,
and there's a lot of great info there about GCC and C++ and C development.
But I'm really looking forward to some of the new warnings
that are coming in GCC 6,
like the misleading indentation is my favorite one.
So if you forget to put braces around code
in an if block or something
and then have the next line indented in
as if it were part of the if statement,
you'll get a warning now.
Okay.
Sounds good to me.
Yes, I'm always looking forward to compiler warnings
that give us more static analysis of our code.
You know, Red Hat has a pretty big presence in my area.
Maybe we could get someone from there on the show sometime.
Yeah, that'd be cool.
I have no idea what their official involvement is
in GCC development or not.
I have no idea.
Yeah.
Arne, was there anything you wanted to talk about with this article?
Well, I too was very interested in the warnings they put on
and the additional diagnostics.
So, yeah, I think it's a bunch of cool features coming up there.
Yeah.
Oh, it's also important to note, I believe, the...
Where is it? It's somewhere in this article, I believe,
says the default will now be C++14 instead of C++98. Important to note, I believe, the... Where is it? It's somewhere in this article, I believe, says
the default will now be C++14 instead of C++98.
Okay, that's a nice move.
Yes.
That's somewhere in this article.
Yeah, I see it down at the bottom.
Languages and runtime libraries.
GCC6 now defaults to C++14.
Yep.
There we go.
And includes C++ concepts.
Yes. That's's interesting we'll finally
start to get some real world experience with it apparently yeah um this next article is uh
from uh this from anthony williams right just software solutions yep yeah and this is about L values and R values. And it's a really, really well done read if you're not familiar with the differences between L values and R values.
And it goes into X values and PR values.
And I've also read Effective Modern C++ from Scott Myers.
That was another good one that really goes over the differences between
the different L-value and R-value types.
Was there anything else you wanted
to mention about this one, Jason?
No, it's just
it looks like a good article. I only got about
two-thirds of the way through it before
we joined
for the podcast today.
There's a lot of info
in it, and I've always found all the different
terms confusing, because it's about glValue, rValue,
lValue, xValue,
and prValues.
And he's got plenty of good
little code examples here, too.
I think it's
interesting to have
read it one or two times, but
I'm not so sure
how important
it is to know everything from
the top of your head in the
daily business.
Knowing the difference between L-values and R-values
of course is important,
but all the tiny differences
between GL-values and
X-values,
I'm not
yet come across a situation
where it was really so important to have this knowledge.
That's true.
The compiler will just give you an error
if you're using one of them wrong anyhow.
Right.
Exactly.
That is a good point.
Okay, this next article is kind of the news of the week in C++ world.
The C++17 trip reports are coming out,
and it's a little disappointing. C++17 is not what many of us hoped it would be. This article
is starting out with just a list of what's not going to be in C++17. And if you haven't heard, modules, concepts, ranges, and coroutines
are not going to make it into the C++17 spec.
But they are all aimed for TSs, right?
Right. Which means compilers can start implementing it
in an experimental fashion, I guess.
But if you work for a company that doesn't
want to use, you know, something that might be considered experimental, you're probably not going
to see any of these until C++ 20, I would think. So it's been a while since we talked to Eric,
but I'm wondering if his current ranges code proposal has been updated to work with the
current state of concepts in Gcc6 that'd be interesting
to know because then we could really start seeing how well all these things will work together
yeah we should definitely reach out to him to uh find out about that um to talk about what is going
to make it into c++ 17 there will be uh the new parallelism TS got approved to go into C++ 17.
And also File System.
Those are kind of the two major ones, right, Jason?
Yeah, that sounds right.
File System I've been looking forward to for a long time.
Right, although it is just very, very similar to Boost File System, right?
Boost File System version 3, yeah, specifically.
Do you have any comments, Arne?
No, I think I've read some very good arguments
about why it is maybe the right decision
to not put all those stuff into C++17
because, as we just said,
GCC is publishing concepts now, for example,
and so we don't have so much real-life experience in using it.
So we still don't know if there might be some kinks in the language that we have to get out.
So, for example, I think about the
auto and initialization
lists, initializer lists,
stuff that got into the language
and then had to be twisted
around and still
we didn't get it right.
But maybe sometimes it's better
to wait for two or
three more years until we get things
into the language and standardized.
Yeah.
There's some interesting stuff here
too that I wasn't expecting,
like generalized for range loop.
That looks like it could
possibly clean up some stuff with ranges.
See that one, Rob?
Yeah. Yeah, I see that.
The range-based for loops, that would be nice
to have.
The one that's completely lost on me is hexadecimal floating point literals.
I really don't know what the use case for that is.
I feel like I'm missing something.
Yeah, I'm not really sure what the point of that one would be either. Maybe, arguably, you can specify floating point literals
without any loss of precision
because you know exactly what bits you're passing in.
I was also curious about,
do you see this one about fall-through switch statements?
Yes.
Is that going to be the actual syntax used,
or is that kind of placeholder
syntax? Cause it's double square bracket fall through and square brackets, semicolon. And
that just doesn't look like C++ to me. Yeah. That's the general attribute feature that was
added in C++ 11, but hasn't really been used for a lot of things yet. Okay. So that is what
it's going to look like. I believe so.
There's some other new features using that sort of
syntax. There was no discard
and maybe unused.
So if you have a variable
that you don't want
a compiler warning about, you can put
maybe unused in front of it.
Yeah, exactly.
Interesting.
Okay, well is there anything else you guys want to talk about with c++ 17 before we move on no i guess one concern i have is
do you think all these features that didn't make into c++ 17 is that going to be it for c++ 20 or
do you think they might be adding additional features as well?
Because if that was the case, my concern might be that we don't get these for C++20,
because there would be more things on the plate and nothing gets in.
I don't know.
I don't know.
Hopefully, after a couple years of experience with these as TSs, they'll make it in for real.
I guess I would really like to get uh someone who attended jacksonville on the show soon to do kind of a trip report episode and maybe dig into this in a little
more detail we could probably find someone for that yeah yeah okay arn so let's start talking
about clean code that's kind of what your blog revolves around right exactly yes um what uh what are kind
of your focus when you talk about clean code well my focus is mainly about readability
maintainability so that um especially code that has to be used and modified and and read often
should be as readable as possible.
Because that's where developers spend the most time in trying to understand code that anyone else has written.
And if that's not readable, it just takes unnecessary time.
Or even code that you wrote three months ago.
Exactly, yes.
Do you have any specific examples of clean code that you would like to point out
or what is not clean code yeah well for example if you have some convoluted thousand thousand
line of code function with tons of variables and you can't see where the variable was declared, where it was assigned,
and it just gets used a few hundred lines later.
So you have to scroll up and down
and try to figure out what the function does.
And it does multiple things at a time,
so it's just too much to keep in mind at all.
So some people might argue, well, that kind of thing isn't that critical
because that's why we have modern IDEs.
You can just right-click on the variable and find out where it was used and defined.
What would you say to them?
But it's still easier to read a function of maybe 10 lines or 20 lines
and have it all in your head and not having to to use the
ide to to find the uh the point of origin of of some values okay let's talk about your blog for
a moment um it's simply titled simplify c++ uh what made you decide to start this blog? I started this blog after, on my last job,
some of my colleagues asked me to write some down simple notes about,
for example, a summary of what the different casts do,
when they should use which cast.
And I first wrote some internal wiki pages
for a company and then I thought
why just write them
for five or six developers?
I could easily start a blog
as well.
I had just read
the book about
soft skills from
John Sonnes.
He writes for your career it can be important about soft skills from John Sonnes. And he writes,
for your career, it can be important to start a blog.
So I just figured I could try and start one.
So I haven't read that book.
What is meant by soft skills?
John writes actually about his life as a software developer,
about anything except coding itself,
so about blogging, about job interviews,
about his diet, about working out and everything.
So it's an interesting read.
Some things obviously don't apply to everyone,
but he just writes his view about what is or can be important for a software developer besides knowing how to code.
Okay, so like the rest of life, basically.
Exactly.
Sometimes we can forget that.
Software developers' life manual.
Yes.
So what would you say the goal of your your blog is and have you been achieving that goal
um well my blog the goal of my blog is i think to show people that um c++ code doesn't need to be
complicated and um because i have many people seen uh writing for example clean java or clean
c-sharp and when they switched to C++,
they immediately thought
C++ is complicated, so C++
code has to look complicated.
And that's not the case, of course.
So I think that's
my goal, and
hopefully some people
learned from the
blog some pieces that are
not so obvious if you haven't seen them before.
So do you have a favorite article that you've written that outlines some of these
more interesting clean pieces?
Not about specific clean pieces, but I think my favorite article was the one about clean code versus performance
because often people do a lot of things in the name of performance,
especially sacrificing anything that could be named clean code.
So they just write some mumbo-jumbo of C++
thinking that this would boost
their performance without even
profiling it first and without
knowing if it really improves their performance.
So that was my
favorite article, I think.
And the one that was
most discussed in the
comments and I think on Reddit too.
So you're getting some
good feedback then too.
Yes.
I have not noticed.
I see your tweets about your blogs, and I've read some of them,
but I have not looked at the Reddit comments personally.
Reddit can be a scary place sometimes.
Yes, exactly.
So not too many haters then.
It's different.
Some of the posts are very controversial
and they just write like
completely different opinions
and not in the nicest words.
But yeah.
I saw...
I was reading one of your posts
that you put up just a few weeks ago about getting rid of cheat sheets.
Do you want to talk about that one for a minute?
Yeah, well, that one was triggered, I think, by a photo that STL711views posted of his computer screen where he had, I think, run some tests.
And below the screen, he had a little sheet
with the C++ operator precedences.
And I just thought if you need a cheat sheet
to write this code,
then anybody who has to read the same code
needs basically the same cheat sheet as well.
Because if you leave out, in this case,
the parentheses to group the expressions,
then if it is not very clear
in which order the expressions are evaluated,
then everyone who has to read the code
has to look it up as well.
Yeah, and I imagine
adding the parentheses doesn't
harm the compiler's ability
to optimize or anything in those cases.
I don't think so, as I know.
I couldn't imagine it would.
Well, it's one or two
percentages more to parse for the
parser, but that's it.
Yeah, if that is it about. Yeah.
If that is the main slowdown in your compilation,
then you have other problems.
I wanted to interrupt this discussion for just a moment to bring you a word
from our sponsors.
You have an extensive test suite,
right?
You're using TDD,
continuous integration and other best practices,
but do all your tests pass all the time getting
to the bottom of intermittent and obscure test failures is crucial if you want to get the full
value from these practices and undo software's live recorder technology allows you to easily
fix the bugs that don't otherwise get fixed capture recording of failing tests in your test
suites and debug them offline so that you can collaborate with your development teams and
customers get your software out of development and into production much more quickly, and be
confident that it is of higher quality. Visit undo-software.com to see how they can help you
find out exactly what your software really did, as opposed to what you expected it to do. And fix
your bugs in minutes, not weeks. I also saw you had an article here about using static analyzers.
I know this is one of Jason's favorite topics.
It is.
Yeah, it's basically I started off with compiler warnings.
And the article about static analyzers, about using them,
is just a continuation about getting the most warnings at compile time
about your code here.
You can get to find bugs early.
I think that's the gist of it.
So you like to rely on using the compiler's analysis for a lot.
Do you use any other CBB check or Clang Analyze
or anything like that?
Well, when I was still writing code for myself, I did.
But in my past jobs, we weren't able to use them because the last job I had was using Embarcadero C++ builder,
which had some extensions
and to coexist with Delphi code.
So we couldn't use any other compiler
or static analysis on this code
because they just would complain about the extensions.
Wow, and correct me if I'm wrong,
but does that mean you were stuck on pre-C++11 at the time then also?
Yes. Wow. We were stuck on
C++03. I think it had something like
R value references already implemented, but if
you tried to use them with the STL,
you couldn't use the boost that came with the compiler
because it somehow did the lookup wrong
and pulled in another version of the STL
that had no R-value references implemented
and it just blew in your face
when you tried to use R-value references.
So I understand you'll be speaking at a conference soon.
Is that right?
Yes.
End of April,
it's called Advanced Developer C++ Conference
near Munich.
And they invited me to give a talk.
And actually, I got to choose the topic and it will be
about bringing C++ to
clean C++ to large scale legacy C++
applications. So basically the stuff
I have been done the last seven years more or less.
Do you have any particular point you would like to bring up
that you plan to talk about, maybe as a teaser for our listeners?
Well, there will be different points, I think.
But in my experience, the most critical point on doing this
on clean code into legacy applications is getting the team behind you
because if you try to bring C++ into the code
and you have like three or four developers
that are copy-paste heroes,
you won't have any success with it.
So is that something you've had success with
on your previous teams, getting the team behind you?
What kind of challenges are involved?
The challenge often is that,
especially if you are younger and not as experienced,
if you have someone on the team who has done development for 20 or more years
and has always done it like he's doing it,
persuading him that it might be time to change
and to try something new.
I can see that.
Do you find any of the more modern C++ 11 or 14 features
help in writing your code cleaner?
I think there are a lot of
features that
allow you to write
less code, for example
auto and range
based for loops that are just
more concise and
cleaner than, for example,
having to write std vector
of int iterator begin
and all the long and very versions
of the old for loops. I think there's a lot, for example, landers and to just to insert
them where they're needed and not having to define a functor elsewhere is also a great
feature.
Yeah, definitely.
Are there any features of C++ you say should be completely avoided to maintain clean code?
Well, I'm not aware of any now.
Okay.
Of course, you have to use any feature with care.
We have the access to low-level to C code
because sometimes we may
need it to tune
our performance, but
I think the
bigger danger is that people
use them where they're not appropriate
but they still
have the merit to it and
there's no feature in the language
that should be left out.
Okay.
I've been having a hard time getting an answer to that question from our interviewees.
No one wants to completely remove any particular feature of C++.
It's not from Scott Myers.
Right.
Yes.
I think, actually, he has a point.
If we can get rid of old features that only are like like slowing us
down um and can do so automatically why not yeah and there might be um more room for growth with
things like clang modernized to to do even more advanced code refactoring automatically yes
i think clang is is a really powerful powerful library and I hope we can see more tools
coming out of it soon. Have you tried to use the library yourself
for analysis? Yes, once or twice
for testing. If you could use it with an old
compiler on my last employer, but
not really in production now.
Okay.
That's been on my to-do list for, I think, over a year now, to play with LibClang.
So you said your talk's going to be about large legacy codebases.
Do you have any guidelines you're going to go over about refactoring in large codebases?
Yeah, it should be small and focused
refactoring, I think. If you just
jump into a file
and start to change everything you see
that could need some
changes, you're getting
nowhere, actually. If you have
some focus, for example, say
clean up
the innards of a function first
and the interface later,
or if your goal is to get rid of some bad patterns,
for example, some singletons
that link your whole code base together,
then you should refactor to what's this one goal
and not trying to do
everything at once.
Because usually in such large code
bases, there's so much to do that you
won't get
ready in one or two
years, so there's no point
in doing everything at the same time.
That makes a lot of sense.
Yeah, definitely.
It's definitely always tempting to just go and refactor,
but once you start changing the interfaces,
you can get into the weeds pretty quickly.
Exactly.
For us as compiled language developers,
we like to think,
the compiler will help me sort out all the differences.
Yes.
Change one line, get 20 errors. Okay, change the first error, make it right, get 20 errors okay change the first arrow make it right
get 20 more errors and that's it
it does go that way sometimes okay is there anything else uh you wanted to go over before
we let you go arn or i think i think for the moment Okay, so where can people go to find you online?
Obviously, you've mentioned your blog a couple times.
Where is that exactly?
That's on arne-mertz.de.
That's my name.
And you can also find me on Twitter.
It's at arne-mertz-r-t-z as well.
And I'm on LinkedIn
as well, but if you
contact me, it might be some days
until I react because I don't look
at it too often.
And this conference that you're going to be at,
do you know if they record and post videos
online?
Actually, I don't know. I haven't found any
videos from the last conferences, so I don't think so haven't found any videos from the last conferences so
I don't think so
well hopefully they will start
this year
maybe
okay well thank you so much for your time
today
thanks for joining us
thanks so much for listening
as we chat about C++
I'd love to hear what you think
of the podcast. Please let me know if we're discussing the stuff you're interested in,
or if you have a suggestion for a topic, I'd love to hear that also. You can email all your
thoughts to feedback at cppcast.com. I'd also appreciate if you can follow CppCast on Twitter
and like CppCast on Facebook. And of course, you can find all that info and the show notes on the podcast website at cppcast.com.
Theme music for this episode is provided by podcastthemes.com.