LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 12:21 pm

I broadly agree with Peter O and would say if you aren't prepared to deal with pedagogy and syntax then C/C++ is not a language you should be learning. There are a multitude of later developed languages and many at a higher level that are much more forgiving to learn if you just want to throw some code around.

I use C/C++ not because I think it is the best language but because it is the only language that is fully supported in all the fields I work in. That is a commercial reality and something that isn't going to change anytime soon or even inside my control. What some poster on the internet makes of C/C++ is even less important that what I make or think of the language :-)

Microsoft would have you on C# and there would be no C++ if they could, but even they can't push and control the programming market.

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 12:36 pm

LdB wrote:I broadly agree with Peter O and would say if you aren't prepared to deal with pedagogy and syntax then C/C++ is not a language you should be learning.
I would put it in another way : " if you aren't prepared to deal with unpedagogy and syntax then C/C++ is not a language you should be learning."
Even if not telling the truth when saying that because there's now plenty of very good books and website that deals with C programming with a very good pedagogy.
But well my point was only to show that it's common sense to not knock out newbies with info they don't need. In the end writing code (and moreover clean) is hard -period-.
The only thing I wanna say is that most experienced C devellopers seems to forget the long and painfull path that get them to be experimented, and they seem to think that things that seems to them as evidences aren't for people that wanna learn.
That's the truth for instance, when you start doing stuff in the kernel there's very few documentation, and the documention you'll find is mostly uncomplete and seem to believe that you actually already know what they are explaining to you. It the same even when you ''man'' stuff, okay it's very nice to have a documentation but when it's not userfriendly as it is, a direct link to google would be most of the time better than the man pages.

I could go further in explaining all what I think about those anti-pedagogical and anti-progress way of thinking, but I don't really have time and energy to go further. But the traditionnal dev community really do have a problem with passing knowledge to newbies. Maybe it's also because the whole field is relatively young.

YCN-

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26059
Joined: Sat Jul 30, 2011 7:41 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:05 pm

Firstly, please refrain from ad hominem attacks.


Secondly, the linked 'learning C thread' got some stick, basically because it was not very well thought through, contained semantic and syntax errors, and implied that you could learn C quickly. Not a good place to be for a 'teaching thread'.

Thirdly, pedantry is useful in C, (or any computer language). Making sure code is clearly written, contains comments, and shows no warnings or errors when compiled is the RIGHT THING TO DO. It's not pedantry.

If I see C code that fires off compile warnings (not errors), to me that indicates there is something wrong with the code. I've been bitten many times when leaving warnings to find that they were the reason the code didn't work correctly. So always compile with -Wall, if not some of the other flags mentioned above.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:12 pm

YCN You totally lost me.

C is old (circa 1970) being helpful and newbie friendly was not in it's design. It exist today because of a commercial reality that is the widest most supported language in certain fields. There isn't really a dev community around the language save a few groups that make suggested changes that may or may not be adopted by various compiler producers.

Then you start talking about doing stuff in the kernel??? What kernel and what has that got to do with C/C++???? You don't get to change the C/C++ compiler or it isn't a C/C++ compiler anymore.

You keep saying the dev community ... what dev community are you even talking about?
Last edited by LdB on Tue Jun 06, 2017 1:15 pm, edited 1 time in total.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26059
Joined: Sat Jul 30, 2011 7:41 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:15 pm

Well, the Linux kernel is written in C, and you are unlikely to meet a more rigidly applied set of coding standards. If you think this thread is being overly strict, I suggect never trying to upstream code to the Linux kernel.

The language itself is ultimately controlled by an ISO committee (prior it was ANSI controlled) - not a 'few groups'....We are currently at C11, ratified in 2011.

https://en.wikipedia.org/wiki/C_(programming_language)
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:20 pm

jamesh wrote:The language itself is ultimately controlled by an ISO committee (prior it was ANSI controlled) - not a 'few groups'....
In your dreams it is try telling that to Microsoft, Intel or many of the other microcontroller manufacturers who won't support half the junk the ISO standard comittee came up with.

I actually don't think a full implementation of C11++ exists even in GCC it's limited last I looked. Visual Studio blatantly gives the fingers up to half the C11++ spec. MS has it's own standards C14++ and C17++ which if you read the commentary around it you will find is because the customers demanded certain features many not compatible with C11. Offically MS supports a limited subset of C11++ core features.

The compiler editors set the specification not some ISO comittee and the compiler manufacturers respond to market forces.

The ISO committee still has not sorted out "volatile" or a "write only" register for embedded industry and a number of compiler manufacturers are going there own way because of it. It's really cute when you can read from a write only register and the compiler gives no warning because you still can't define a write only register in C/C++.

I think that picture says everything you need to know about the ISO committee.
Last edited by LdB on Tue Jun 06, 2017 1:54 pm, edited 12 times in total.

1dot0
Posts: 430
Joined: Mon Nov 28, 2016 12:31 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:23 pm

IMO some people have some misconceptions about scientific didactics. No single pupil in the world is told how to add and substract numbers by starting at the theory of sets, rings, and fields and algebraic closure. They start by counting with fingers and with help of number lines, keeping things simple and stupid. Then they proceed.
Therein also for starting to teach a PL it has to be simple and stupid, too, not overstraining or even scaring off students right from the start. Then they proceed.
So I appreciate everyone who tries to explain things in a simplified manner to help students to get quicker into that complex materia and have faster success. In that respect a couple of compiler warnings might be tolerated.
Nonetheless things which are told must be confirmed, validated, and tested. If they aren't, the author should be corrected in a factual manner, but he must not be bashed or personally insulted, and that's another point where some people have some misconceptions about.

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 1:54 pm

LdB wrote:There isn't really a dev community around the language save a few groups that make suggested changes that may or may not be adopted by various compiler producers.
When I talk about community, I'm speaking about teachers, book writers, some forums... I agree that's not exactly a community, more a group of user, I don't really know how to define that.
LdB wrote:Then you start talking about doing stuff in the kernel??? What kernel and what has that got to do with C/C++??
That was only an example to show how innaccessible and painfully reachable ressources can be. But you're right not a lot to do with C.

Sorry to confuse you guys, to sum up what I'm trying (hardly) to say is just that : I feel like experienced C devellopers forget about how complicated learning C is, and that they often throw some more problem into newbies back, without thinking about the fact they couldn't even understand what they are talking about.
I'm just pointing the lack of pedagogy of a lot of people when it comes to learning C. And the fact that people always make things seems to be more complicated than they actually are. Be it by using all the time acronyms without defining them, by (almost always) making the hypothesis that the reader is an experienced develloper, by using exotic compilation flags [that one is free ahah], by doing hyper technical and complexe sentences...
EDIT: It's proven that complexe topics can be treated with simple and understandable words. That's the whole point of some courses at university.
I'm not saying that I'm better that all I mentionned above, but I'm trying to. (eventhough I'm not an experienced C writer yet ~4 year experience)

But that said, and I've already said it, I think that things are changing for the best. Even if there's still this massiv group of people trying to un-popularize things.

YCN-
Last edited by YCN- on Tue Jun 06, 2017 2:03 pm, edited 1 time in total.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:01 pm

I have no problem with that 1dot0 but we are stuck between jamesh who thinks some ISO committee controls C/C++ and YCN who seems to think the newbies should control it.

I am just telling them the harsh facts that those using C/C++ that are willing to put in time and money are making the decisions whether they realize it or not. Linux for example may be free but unless companies and individuals can make money they would not bother.
Last edited by LdB on Tue Jun 06, 2017 2:29 pm, edited 1 time in total.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:08 pm

YCN- wrote:I feel like experienced C devellopers forget about how complicated learning C is, and that they often throw some more problem into newbies back, without thinking about the fact they couldn't even understand what they are talking about
That is possibly true but a different problem .. what you are really talking about is lots of programmers are not good communicators :-)
YCN- wrote:But that said, and I've already said it, I think that things are changing for the best. Even if there's still this massiv group of people trying to un-popularize things.
Some things wont change because the main market base won't let it. That is like Jamesh trying to say some committee controls C/C++ well they can publish a standard which the market can laugh at or perhaps adopt. It comes down to commercial reality for the bulk of the users and some well meaning changes from the committee get thrown under the bus because they have a huge commercial impact. The C11++ standard for example was dead on arrival, some parts make sense but lots was rejected.

There is actually a full list of the various new proposed C/C++ standards and what compilers accept what parts of it
http://en.cppreference.com/w/cpp/compiler_support

I think C17++ might become the next real stable standard because it seems to be accepted by both Intel and Microsoft (and by defacto their clients) but that will be up to market acceptance. The fact Herb Sutter from MS moved in to take control of the C17++ committee meant that MS was going to be onboard with the changes to the standard.
Last edited by LdB on Tue Jun 06, 2017 2:41 pm, edited 1 time in total.

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:37 pm

There is actually a full list of the various new proposed C/C++ standards and what compilers accept what parts of it
http://en.cppreference.com/w/cpp/compiler_support
Interesting that the two free compilers, gcc and clang, alone have substantially complete support for the draft C++17.

Many years ago when I used MSVC, I remember MS stating that they had no plans to ever fully support C99. Its hard to take such a compiler seriously.

In some respects what happens is that popular new language features get added as extensions by the better compilers. In time, and if they are successful, they get adopted into the next standard.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26059
Joined: Sat Jul 30, 2011 7:41 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:39 pm

LdB wrote:I have no problem with that 1dot0 but we are stuck between jamesh who thinks some ISO committee controls C/C++ and YCN who seems to think the newbies should control it.

I am just telling them the harsh facts that those using C/C++ that are willing to put in time and money are making the decisions whether they realize it or not. Linux for example may be free but unless companies and individuals can make money they would not bother.
Well, I've only been using it for 30 years, what do I know.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:45 pm

I beat you by 20years I have software credits in 1982 so does that mean I win :-)

Try the first name and that was in pascal back in the day
https://www.freepascal.org/credits.var

I am not sure length of time programming counts as an indication that one gets an answer correct.

As I said the C17++ standard has a chance because everyone seems to be back on board. Herb Sutter seems to be driving that and that means MS will be on board.
Last edited by LdB on Tue Jun 06, 2017 2:48 pm, edited 1 time in total.

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:46 pm

LdB wrote: It's really cute when you can read from a write only register and the compiler gives no warning because you still can't define a write only register in C/C++.
For comparison then, what other similar languages do have the ability to define a "write only" register?

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:51 pm

LdB wrote:I beat you by 20years I have software credits in 1982 so does that mean I win :-)
Wow! You have been coding in C/C++ for 50 years!

1982 is only 35 years ago, roughly when I started my first programming job.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:52 pm

jahboater wrote:For comparison then, what other similar languages do have the ability to define a "write only" register?
Well in the embedded space you only really have assembler and C/C++ so not really a valid question :-)

The fact is we have been asking for it for about 20 years and the change of volatile or some command to be able to map to a memory space. The whole way volatile works for embedded is an abomination.

YCN-
Posts: 246
Joined: Fri Jun 10, 2016 3:18 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:55 pm

LdB wrote: The whole way volatile works for embedded is an abomination.
May I ask why ?

YCN-

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 2:57 pm

jahboater wrote:
LdB wrote:I beat you by 20years I have software credits in 1982 so does that mean I win :-)
Wow! You have been coding in C/C++ for 50 years!

1982 is only 35 years ago, roughly when I started my first programming job.
So you are about same age I am sure you didn't start programming in 1982 you had been programming enough years to get a job and work with a team. So like me you must have been at this for around 50years and my first C program was TurboC 1 and the stupid yellow screen, so let me look at that year ... 1987, pascal before that :-)
Last edited by LdB on Tue Jun 06, 2017 3:19 pm, edited 1 time in total.

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 3:00 pm

LdB wrote:
jahboater wrote:For comparison then, what other similar languages do have the ability to define a "write only" register?
Well in the embedded space you only really have assembler and C/C++ so not really a valid question :-)
Yes exactly. But its hard to complain about the slow pace of the C standards when no other languages are even remotely usable.
LdB wrote:or some command to be able to map to a memory space
mmap()? thats what is used on the Pi to access the GPIO registers.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26059
Joined: Sat Jul 30, 2011 7:41 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 3:01 pm

LdB wrote:I beat you by 20years I have software credits in 1982 so does that mean I win :-)

Try the first name and that was in pascal back in the day
https://www.freepascal.org/credits.var

I am not sure length of time programming counts as an indication that one gets an answer correct.

As I said the C17++ standard has a chance because everyone seems to be back on board. Herb Sutter seems to be driving that and that means MS will be on board.
It's not a competition. But I am correct, in case you were wondering.

Since I've been working in industry for that long, in various different areas, I've managed to use ANSI/ISO standard C in it's various forms throughout that time. Did spend some time on Microsoft C++ v6.0, which was a bit non-standard in places, but I didn't use those places. It's actually pretty easy to use standards, you just don't use extensions. Portable code turns out to be that, portable code. Of course, the real problems arise when you use libraries, which for the most part have nothing to do with the language standard itself, and in many cases veer towards OS specificity, especially early UI libraries.

So for me, the standards bodies have worked. Currently writing some C++14 stuff. Not intending to depart from the standard.

Meanwhile, I'd suggest calming down a bit. This is nothing to get het up about.

(I even used ISO Pascal on a BBC micro, got me through first year of University, then we learnt 'C')

C is 45 years old BTW, first use in '72 although development started in '69. Anyone claiming to have used it for 50 years isn't remembering correctly.
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 3:07 pm

YCN- wrote:
LdB wrote: The whole way volatile works for embedded is an abomination.
May I ask why ?

YCN-
Volatile doesn't apply to a memory space in C it's dealt with by the optimizer, if you like it's a command to do with optimizing to the type it's assigned to.

So you have to apply volatile to every which way you access the memory space. So lets give you a simple example with an array which always catches newbies out.

Code: Select all

volatile int A[20];
You would think that declares the whole space of A volatile but it doesn't. A is only volatile to the compiler if you access the whole of A ... an access to A[0] or A[1], or A[2] is not volatile.
So when you are dealing with a real register lets take a USB register on the Pi you end up with this sort of abomination

Code: Select all

/*--------------------------------------------------------------------------}
{							 USB CORE RESET STRUCTURE					    }
{--------------------------------------------------------------------------*/
struct __attribute__((__packed__)) CoreReset {
	union {
		struct {
			volatile bool CoreSoft : 1;					// @0
			volatile bool HclkSoft : 1;					// @1
			volatile bool HostFrameCounter : 1;			// @2
			volatile bool InTokenQueueFlush : 1;		// @3
			volatile bool ReceiveFifoFlush : 1;			// @4
			volatile bool TransmitFifoFlush : 1;		// @5
			volatile unsigned TransmitFifoFlushNumber : 5; // @6
			volatile unsigned _reserved11_29 : 19;		// @11
			volatile bool DmaRequestSignal : 1;			// @30
			volatile bool AhbMasterIdle : 1;			// @31
		};
		volatile uint32_t RawReset;						// Union to access all 32 bits as a uint32_t
	};
};
Basically you have to volatile any which way you access the register and if you forget one well then you get a bug.

Do you see the problem even in the above code if I accessed that 32 bit register as two 16 bit reads those 16 bit reads would not be volatile because I haven't defined a 16 bit access volatile. Byte (8bit) access would be the same. Just lucky on an ARM you wouldn't do a 16bit or 8 bit access or my code here would bug :-)

They have never fixed it and it's a pain in the butt to embedded programmers. The only other way to deal with it is always read say the 32 bits and peel the bits off which is usually slower and somewhat dangerous (you migth for example use a macro). Most microcontrollers and some processors like the ARM can do bit manipulations and you give up that speed by using that sort of access which is why we persist and write that sort of abomination.

Essentially I am telling the optimizer when accessing the register it has to assume it has changed and must go read it again, it can't use any value it read earlier. However I must also keep track of how I might access that register and make sure I define a volatile access for it.

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 3:51 pm

jahboater wrote:mmap()? thats what is used on the Pi to access the GPIO registers.
That's a linux thing nothing to do with C/C++ .. and I for one don't have any linux on my Pi :-)

The GPIO are registers at an address.

If you want to discuss access under Linux then I defer to you because I don't know and I don't do it. You have altered what we are talking about.
jahboater wrote:Many years ago when I used MSVC, I remember MS stating that they had no plans to ever fully support C99. Its hard to take such a compiler seriously.
That is easy to say if you don't write Windows applications or are happy to wait until GCC works out what they changed in XYZ update of windows and adds it . Many commercial groups won't have that option and your or my opinion is meaningless. Your right and I agree with you but that doesn't change reality for many :-)

Jamesh sort of did the same change argument. I am not saying one shouldn't program to a standard but those standards are up for dispute and do vary from industry to industry. I have to write for the MISRA C standard from time to time and that is a dog of a thing but it's required in some industries. I am sure there are those who will sing the praises of MISRA C, obviously some people love it ... just not me (I spend more time dropping to assembler to get around it) :-)

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 4:36 pm

LdB wrote: I am not saying one shouldn't program to a standard but those standards are up for dispute and do vary from industry to industry.
I don't think they do. ISO 9899:2011 (C11) is the same regardless of who you are or what industry you are in. Yes, coding standards such as CERT or MISRA may differ. (I have a copy of MISRA C:2012 in front of me - it says it is "Guidelines for the use of the C language in critical systems" - nothing whatsoever to do with the language standard). Likewise CERT, it calls itself a "Secure Coding Standard", thats all.

Strong ISO language standards helped make C the popular language it is. I can write C11 compliant code and know it will work on any conforming implementation - for me thats a very good thing!

LdB
Posts: 1530
Joined: Wed Dec 07, 2016 2:29 pm

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 4:44 pm

There are lots of things you can't do in Misra C they are forbidden like anonymous union which is valid under C11++ but not Misra C.

I always drop to assembler to get around that restriction :-)
If it isn't C the standard doesn't apply which is a lovely get out of jail card, or spend the rest of your life writing a deviation documentation.

Section 10 of the standard you have is what you need to read.

There a pile of other things you can't do that is perfectly valid in C/C++ .. got any enums in your code there are some funtimes with that read section 10 carefully :-)

Try to put a reasonable size C11++ code thru a Misra C parser and tell me how many rejections you get :-)

jahboater
Posts: 5475
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Writing clean code isn't hard!

Tue Jun 06, 2017 4:57 pm

I think you have a later copy of MISRA than mine (2012).
Mine only goes up to section 9 which is the appendices.

Edit: I just realized you probably have the C++ version which is bound to be bigger.
I have the C version.

I am in the fortunate position that I can pick and choose which parts of these coding standards (if any) to follow :)

Return to “C/C++”