Showing posts with label languages. Show all posts
Showing posts with label languages. 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, 10 February 2017

Programming : Obscure Languages I Learned

This year marks the twenty fifth anniversary of my learning a proper programming language (that is a language which was not BBC or Commodore BASIC, but required a compiler or interpreter and a code file to run).

This is longer than two or my team have been alive... Which blows them away, but which I take as quite worrying.  I worry as I wonder if my skills are out of date, despite my regular passed through modern papers & books on new technica... This is related to my "Talk the Talk" post scheduled for Sunday, so come back to that.... However, back to the here and now...

During my time as a programmer I have learned, mostly for academic or edge case scenarios, some strange languages, here are my top three strange, annoying or otherwise pointless languages...


3.  Haskell
Controversial to come, but I hate Haskell, it was to be learned as part of a  course module on "Functional Programming" during my degree; the difficulty being I was very much in a different linear mind-set for my then main language (Pascal) and learning to apply the Object Orientated Paradigm with C++ and Java.

I didn't like the language, found the interpreter to be very slow, and there seemed to be no clear connection between the book we had been set (which I still have somewhere) and the version of the language the course defined interpreter required us to use.

The task assigned was to quad-tree compress an image, I don't remember finishing, but have created image processing tools since with C++ and the CImg library.


2. Occam
Set as part of a course on parallel processing during my degree Occam was meant to teach us how to strip a process down to it's bare minimum and then run this in parallel with another process.  The problem?  The machines we had at hand to perform the course were old 386 PC's, whilst the Occam compiler was for the Motorola 68000, the solution... Cross compile and upload the Occam into a 68K daughter-board which was perched on top of the PC case tethered from flying off by a flaky ribbon cable.

The up shot was more time spent in the sweltering hot room, fanning these over heated, junks clunky boards than actually cutting code or learning anything... Perhaps I should revisit Occam?

If only the lecturer had let us use the just released Linux OS and pthreads!?!?!


1. Z
No, not Z++, Z... A formal description language!?!? I hear you cry, but no I have a book titled "The Z Programming Language" and we had the honour of trying to decipher this system description language into a series of function testing calls.  Not a true programming language per say, but still a horrible, horrible thing to behold.

I just have to mention "Z" to any of my peers from that same Uni class and they shudder, not least as we learned the following year would not study "Z" at all... Again, I still have the book, I may take a re-read, but for now Z languishes as the most unloved language I ever had to touch; really, I much rather had used FoxPro and SSADM, to that's how much I hated Z.



Coming up, my exploration of using the "R" language for data processing, in regards to which I have just had a massive book!  Yay.