Showing posts with label stroustrup. Show all posts
Showing posts with label stroustrup. Show all posts

Friday, 22 September 2017

Thinking C++, rather than C

I'm going to be controvertial... Nothing new about that, but here is comes... Are you ready?

If you are a C++ programmer, and you are using sprintf, you are not writing C++.

There, I've said it, I can't take it back.  Obviously now, I need to qualify this statement, so lets begin.  Obviously, I am over simplifying, and C++ programs can contain calls to sprintf and still be put through the C++ compiler to receive working object code, so I'm not discounting the use of sprintf in C++ programs.

What I'm trying to tell you is that, if you are writing C++, designing C++, and sprintf is your goto method of truncating and outputting various raw data formats together as a string, then you are NOT thinking C++.

You are infact thinking C... Lets hear why this might be a bad idea...


Its about how you are thinking, you are not thinking in the skill-set you think you are.  And though I make a point about sprintf, I am talking about anything within C, if you happen to elect to use a part of the C standard which is not supported in the C++ standard you suddenly have a problem, and potentially derail your project.

And you don't need to worry about this, you don't need to get around it, program around it or turn hoops to achieve that perfect laminar flow in your code and project, instead you simply need to understand where you are crossing the boundary.

Thats easy when we talk about integrating a C++ program with Java or Python, becuase we have to bring in a whole other runtime element or library and we know we are interlinking, however, this cross-bleeding of C into C++ with no formal unification of the two is, in my opinion, more insidious.  Again Bjarne explains why, far more succincly than I can in the above video.

So, up date your way of thinking to lift your thinking into only C++ if you are writing C++, understand when you include CStdLib or CStdIO excetera you are pulling in a different language.

Therefore, even against many other programmers wishes, I will not use sprintf in C++ I will use streams to concatenate across lines of code:

#include <sstream>
#include <string>
const unsigned int c_Age(147);
const std::string c_Name("Xelous");
std::ostringstream l_oss;
l_oss << "Hello World, I am " << c_name << " and I am " << c_Age << " years old";

Rather than sprintf with "%s %i" formatting, because my solution is "more" C++, stronger C++ if you will.

Both sprintf and ostringstream usage achieve the same notional operations here, streams may even be slower, but I remove the over head of worrying about pre-allocating a buffer for the call, I remove the need to worry about deprecated calls or using "sprintf_s" for security, I drop the three decades of evolution in C and jump straight to the current C++ standard in 2017.


P.S. Some of you have asked where I've been for so long without any posts, well I've been in the deep dark depths of Wales, firstly with work, and then ironically I returned with the wife and dogs on a holiday, which was totally disconnected, no mobile, no internet, I read a few books, watched a lot of DVD's and TV, and I recharged my cognitive batteries after eighteen months of strife, but I'm back!

Friday, 28 June 2013

Bjarne Linked to Here...

I'm stunned, and a little honoured, that my tiny - and not very good - review of the latest C++ from Bjarne Stroustrup actually caught his attention and he linked my review on his site... I'm truly shattered I got noticed.... I wish I'd done a proper review now.

I have continued to read the book, its such a delight to behold too.  My preferred approach is to go down the references at the end, pick one of two items - sometimes at random - and then look them up.

A good one last night, which had me all excited was "to<int>", I spotted this nugget and was excited to potentially see a way to tidy my recurring use of boost::lexical_cast<int> (et al), reducing the function to something like "std::to<int>(std::string)" I was rushing page after page to get forward into the book and find the reference... Doh.

Its not a new entry in std::algorithms or anything, it was a function Dr Stroustrup had written further into the book.

So, I'm going to keep using "boost::lexical_cast" for the foreseeable future.

Yo Dr Stroustup, if you come past my blog again - I'm honoured you visited, thank you.

Monday, 17 June 2013

The C++ Programming Language Fourth Edition

I don't particularly like the copy we get out of Verity Stob over at The Register, however today she brought up a subject I was already going to blog about.

The new edition of the C++ programming language from Mr C++ himself... Bjarne Stroustrup.


I got this for my birthday - yes I'm that sad; or that dedicated to my craft - to request for my birthday large tomes all about the work I do.  I believe I'm a rare thing in the UK software industry for this - least I'm the ONLY developer with my employer who bothers to read and keep my knowledge up to date*.

However, I had other things to finish up reading, so I only just got around to reading the book last night, and in about 20 minutes reading I updated two pieces of knowledge, learned I could port some code I use from a boost header into using the standard functional header and I discovered a new form of constructor chaining.  This was a tiny delve into the book, and really that sums the book up, you can learn something new nearly every other page - even as a seasoned C++ developer I was still updating my knowledge.

I made copious notes of little snippets of code to try and am thoroughly entranced by the elegance of the book.

I'll be blogging more about the snippets I pick up as I go, depending on how important I think they might be for wider use - specifically how I use some of them - like the functional stuff I just mentioned.



* That is, I'm the only person to keep my knowledge up to date without any nefarious alternate reasons - like updating knowledge in an effort to get a different job or fudge an interview for a role on a language I know nothing about - which is something I do observe a lot.