[M3devel] saving indentation?

Jay K jay.krell at cornell.edu
Sun Sep 2 09:34:56 CEST 2012


1) Was my post cut off, or you did that?


2) I've spoken to a number of C++ programmers who want/wanted try/finally.
The answer has often been, that they "just" need to expand their library slightly, and use destructors instead.
Indeed, I think it is inarguable that destructors allow for smaller source code, and just as readable.
The cleanup is written once, per type, not many times, per instance.

You know that for every fopen, or every FILE* variable, you need fclose somewhere.
Somewhere generally being when the variable goes out of scope. How about the compiler puts it in for you, in the easy/obvious place?


However I did forget a style. That I have never used. Because I could never quite grok how to implement it, and/or it depends on the preprocessor.


There is something you can do in C++ something like this:


FILE* from = fopen(from_path, "rb");
if (!from)
  return error_unable_to_open_from;
AT_EXIT(fclose(from));

FILE* to = fopen(to_path, "rb");

if (!to)

  return error_unable_to_open_fto;

AT_EXIT(fclose(to);


Where this "AT_EXIT" thing is some magic.


One of the points to consider is how close to initialization can one state the cleanup.
That is a good thing about this "AT_EXIT".


Let me give you a little more C++-salesmanship.
First, the opposite. Or rather, "classic C++ salesmanship".
Some people speak of "smart pointers" in C++.
These are typically classes with operator= and such, that upon assignment will increment a reference count, and upon destruction will decrement. They are generally "drop-in replacements" for "regular pointers".

I claim this is kind of close to the point, but also kind of misses the point.
These types should be viewed as "regular types", with appropriate constructor/destructor/operator= that can be freely conveniently efficiently thrown around like anything else, and they always do the right thing.


"regular pointers" should be termed more like "dumb pointers".


There is no place in "modern C++" for a plain "dumb pointer", except at low levels to implement "regular pointers".
This is kind of a shift of mindset.
One that many people resist.
But it seems good and right.


There is more to my usual C++ pitch.
Some languages have garbage collection. So "pointers" don't need such "fanciness" as reference counting, "smart pointers". But instead there is a big complicatched machinery behind the scenes.
Similarly, some languages have "synchronized" or "lock". Here again, the syntax is approximately perfect and you can't fail to release a lock.


However these are both special purpose mechanisms, for memory and for locks.


What about closing files, network connections, database connections?
C++ does a good job of providing a general purpose mechanism, rather than having the compiler and runtime special-case two specific resources. Operator overloading fits here too. C, Java, Modula-3 special case a few types -- int, float, string, and provide some infix operators. No user-defined type is given this power. In C++, user-defined types and language-defined types have approximately equal powers.



 - Jay


From: dragisha at m3w.org
Date: Sun, 2 Sep 2012 09:22:32 +0200
To: jay.krell at cornell.edu
CC: m3devel at elegosoft.com
Subject: Re: [M3devel] saving indentation?

Excellent post. I may use it to explain to friends Why Modula-3 :).
Point remains, Modula-3 does not need any of this sugar, because it has TRY-FINALLY.
If you really have complex code in need of continue, why don't add inner loop where EXIT will, in fact, just finish inner body and return immediately to outer body (your loop you wish to continue)? It surely is extra level of indentation, but is better than deep IF.
dd
--Divided by a common language
Dragiša Durićdragisha at m3w.org



On Sep 2, 2012, at 7:27 AM, Jay K wrote:goto is good.
At least if you don't have try/finally and/or destructors.
  Otherwise goto is indeed rarely needed.

Multiple returns per function are bad, if you don't have
try/finally and/or destructors -- otherwise multiple returns aren't needed.


Modula-3 does have "exit" which is like "break".
But it is lacking "cotinue".

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20120902/6cf13845/attachment-0002.html>


More information about the M3devel mailing list