I’m all for standards, but there are different kinds of standardization. When it comes to C++, the language details is clearly standardized. It’s usage is not.
This weekend I spent some time with error handling. Error handling is really painful and makes code look ugly. When I started programming several years ago, resources were tight. There was no virtual memory and no hard-disks. Errors were common and they had to be dealt with carefully.
Nowadays I think that the error checking is generally more lax. However, my paranoia about the worst case scenarios isn’t going away anytime soon.
There are several ways to deal with errors. C++ notably brings the concept of exception handling. I’ve been reading about that but I didn’t come out very convinced. It seems like one of those things a bit too complicated to be used in everyday programming. Up to now, my choice of error handling has been to return errors with return values, and to use goto as a way to quickly escape from a function.
//------------------------kerror myfunction(){ if ( problem ) goto exiterr;
// … return 0;
exiterr: // free anything if necessary return -1;}
//————————if ( myfunction() < 0 ) puts( “Error !” );
Additionally, to make sure that errors aren’t forgotten at debug time, I put an assert. Actually, I have a macro that will assert and finally go to the end of the function.
//------------------------kerror myfunction(){ // if a problem occurs, do an assert(0) and goto exiterr TRAP_ERR( problem ); // … return 0;
exiterr: return -1;}
However, this means that I have to keep track constantly of every possible returned error value. Plus, most return values are dedicated to error handling (with some exceptions for when a pointer is returned, where NULL means error).
My latest experiment is with error values as states. Basically a class has an item dedicated to be an error value. Leaving the return value to more intuitive purposes, such as a value read from a file, or a boolean to express success or failure.
Because the error is an internal state of the class, then it can be queried at any time, without having to check at every function.
So here it goes:
//------------------------class MyClass{ int value; kerror error;public: kerror GetError() { return error }; int ReadIntValue();};
//————————MyClass yoyo;
a = yoyo.ReadIntValue(); b = yoyo.ReadIntValue(); c = yoyo.ReadIntValue();
if ( yoyo.GetError() ) puts( “Got an error somewhere while reading !” );
This is how the interface goes.
As far as implementing it. I’ve made some macros here too. On failure, the TRAP_ERR macro, this time sets an error value into the error member of the class. Then it exits from the function.
I’ve also tried to avoid the goto thing. TRAP_ERR simply returns false/0 in case the function has a return value. This doesn’t allow me to have a portion of the code where I can unwind the function (free resources). Possibly a problem, but time will tell, for now I’d rather avoid to have an exiterr label at the end of every function. And now my functions can look a bit nicer:
//------------------------bool MyClass::DoSomething(){ // asserts and returns false TRAP_ERR( problem );
// …
return true;}
Posted by Davide Pasca as Uncategorized at 1:40 AM EDT
2 Comments »
I’ve been pretty busy recently. Also partially because I had a guest, and it’s hard to sit down and write something when there is a friend around to talk to !
The past weekend was rather busy too. Friday night I went out but without much expectations, as I was tired from the week of work anyway. Saturday I went to the swimming pool, got a vague shade of redness, thanks to the usual sun-screen protection 20 ! In the evening I went with some friend to watch the hanabi (花火: fireworks, the first kanji, hana (花) means flower. The second kanji, read as bi (火) means fire… simple !). In summer there are several fireworks displays in Tokyo (and all over Japan I guess). They are very good, I think Japan actually has the best fireworks technology. I did some research about Japanese fireworks when I had to make a simple fireworks particle system for TGM3.
A technology that instead failed on me, was the cell phones’ one ! I got to the station with my friend. Then I proceeded to call my other friend, which was already in a place where we were supposed to meet him. He had tickets to a large space in a driving school, for people to sit outside without any structure obfuscating the view… however, I couldn’t not reach him by phone ! There was an overload of calls for that area. I managed to get to talk to him once, set a place to meet, but we failed to meet. We only met again half an hour later.. in time to see the last half hour of fireworks. Woo !
Troubles aside, the spectacle was very good. I tried to take pics and short video clips, but there is little one can do with a standard digital camera.
After the fireworks, we all went to my friends’ home. There we watched Enter the Dragon, very good movie. Was actually the first time for me, even though I’m a Bruce Lee’s fan. I’ve basically seen it in all shapes because so many other movies and parodies derived form that single movie, so, finally I closed the circle by watching the original 8)
After the movie, we all went to a club, but I somewhat regret the move, as the club was crowded as it usually is on Saturday, plus the air conditioning was worse than usual. Truly a sweatshop ! (if one considers that, us, the poor customers, actually go there to spill their guts making the rich even richer !).
Sunday I went to the gym and then to a matsuri. A matsuri is a gathering of make-shift stands where all sorts of typical foods are sold. Many girls wear the yukata (simpler dress than kimono), some may wear a kimono. Some guys wear some typical clothes too. Sounds fun, if not for the damning heat and the amazing crowd.. woo.. too much for me.
Coding, I haven’t been doing that outside work. I need to start again however, fack this social bullshit. There is a lot of code to be written out there. Every day a new tool or application comes out, I could have done something too, but, no ! I’m doing social stuff. This is not good.
Posted by Davide Pasca as Uncategorized at 1:28 AM EDT
2 Comments »
After this long new trial with C++, I feel like I’m sliding into back into C, although I’m still searching for the right balance.
It’s not that I don’t appreciate OOP, it’s rather that C++ seems to be truly messed. There is a wealth of tricks & tips, templatized this and that.
I must admit that the language is truly powerful. It allows one to do things in so many ways… but at a practical level, it has some very dull issues. I understand that C++ had to make compromises to keep the performance up. However, it doesn’t really give much hope to those that wish to do clean OOP.
Again, the single biggest issue is the fact that one must declare all members of a class in the header, including the private ones.
I think I’m right to assume that it’s a good coding practice to split larger function/methods into smaller ones. The so called “helper functions”. With C++, every time one writes an helper, he/she has to go back into the include file and add the declaration for that function.. ..for large projects, we are talking about a lot of them. So much for C++ being sold as the language for large projects !
This is a real pain for someone that has worked with Java. Java has some implicit performance issues that I don’t like, nor I like the fact that it doesn’t support callbacks. However, it’s a language truly committed to OO, and it’s nicer towards the programmer: no need to copy and paste every single function one writes !
Keeping at the practical level, the fact that one has to expose the internals of a class along with its public declaration, it means that the code that simply wants to use the class (the user-code), may have to include a lot of useless additional header files.
For example, let’s assume that I do a 3D library. Internally it uses Direct3D, some private functions will use pointers to D3D texture objects. The user-code will be forced to have Direct3D headers included. This lacks of elegance, but, most importantly, can sensibly slow down compilation times.
Many C++ promoters will promptly answer that there are ways around this. Like, for example, building a hidden base class that the user-code sees as a pointer (thanks to forward class declaration). This however adds more work to do, possibly more include files… takes time on the programmer, and, personally, makes me feel like my code is not tidy.
My apartment often ends up being messy, but my code, I really want that nice and simple. I need a streamlined way to write code that looks good.
Posted by Davide Pasca as Uncategorized at 2:00 AM EDT
9 Comments »
Italy is good, it’s fun, we have so many ruins from our glorious past, so much history, the food is good, etc etc.
The country is rotten. Much of my contemporary culture belongs to Italy. Italian language is the one I clearly speak the best, and it’s the one that can give me real good laughs (possibly the most important thing in life). I was brought up by a great family. My relatives sometimes resemble to saints. However, Italian culture is mostly wrong (principalmente sbagliata). It does not really make a point of being honest and altruist. That is left to Catholic religion.. (quello e’ lasciato alla religione cattolica) which is clearly optional. Often people that go to church are the same that commit the worst crimes.
Recently, just came back the “sport” of throwing rocks into freeways (lanciare pietre sulle autostrade). Just yesterday a person died, 5 are injured, one critically.
Some individuals are rotten (marci), but I blame (accuso) the culture. Nobody (nessuno) in school ever taught (insegnato) me to be a good person. I sometimes had classmates that were bastard robbers (ladri), their parents clearly didn’t do their job right (non hanno fatto bene il loro lavoro), teachers clearly didn’t have any instructions on how to educate the fuckers. In any case, not all teachers would have been up to their tasks (in ogni caso, non tutti gli insegnanti sarebbero potuti essere all’altezza del loro lavoro (di educare)) (I admit I may have been lucky with my teachers too, but also my parents always made sure I wouldn’t get bad teachers).
I’m ashamed to be Italian, and if one day I’ll ever be given the option to switch (cambiare) my citizenship (cittadinanza) from Italian to Japanese, I’d probably do that.
I’m clearly Italian, but most of all I belong to my family (principalmente appartengo alla mia famiglia). The elements of my family that I know, are all much better than the average Italian population (poplazione italiana media). Only one time I’ve ever stolen something. I must have been 6 or so. I did it because I must have had a bad influence. I felt bad as I did it (sono stato male gia’ facendolo), then, at the next occasion, I made sure to bring back the item (ritornare l’oggetto), even though (benche’) nobody ever saw me doing anything (at least I think). And I’ve been ashamed of myself since then.. even though no damage was done.
Yet, there are motherfuckers out there (ci sono delle teste di cazzo in giro), that are such low-lives (che sono tali vermi) that they have to try and kill other people (hanno dovuto provare ad uccidere persone), just to feel important.
Those people deserve not the death penalty (non solo la pena di morte), but the torture & death. However, one should consider their parents, which didn’t do their job right, but may have some excuses.. when you send your kid (figlio/a) to school, you never know who he/she is going to meet in the class.
The bottomline (la morale) is that the system is fucked. Some may wish to fix it (alcuni vorrebbero aggiustarlo). I just want to get out of it (much like I did when I needed to work for real). I don’t want to be Italian, the average Italian (italiano medio) doesn’t fit me, also there are too many robbers (try go visiting Naples and see what you don’t get stolen (quello che non ti fregano)), and, apparently, depressed, potential killers.
As of now (d’ora in poi), but actually for a few years already (di gia’), Italy to me, only means my relatives and my friends (significa solo i miei parenti ed amici). The rest (il rimanente), it’s what I know too well to like (quello che conosco troppo bene per piacermi). Sure, many good and great people.. but, on average (in media), a fucked up culture (una cultura ridotta a merda).
In conclusion. I really hope that those people that threw rocks onto the freeway get caught (vengano presi) as soon as possible. I also wish that someday death penalty will come back in exercize (ritorno della pena di morte), because you can’t voluntarily kill someone and not be killed yourself (non si puo’ ucciredere volontariamente, e non essere uccisi di riflesso). You got a bad education ? Too late (troppo tardi per te).
..post with Italian explanations of what I think that may be harder to read for Italians that don’t have much experience with the English language 8)
Posted by Davide Pasca as Uncategorized at 6:33 AM EDT
4 Comments »
It’s already late. No time for coding.
Tonight I watched A Beautiful Mind a movie generously passed on me by a friend.
It was more or less as long as I expected. The story however was more interesting, although a bit too dramatic for my taste. It’s based on a true story, so I can’t really complain too much (hey man change your life !).
Considering some of my recurrent topics, I had to like the parts where the protagonist logically analyzes women met at social events.
As a side effect, the movie got me thinking about intelligence, personality, life (boom !).
One thing that I don’t accept very well is the fact that memory is so unreliable. We experience things but memorize them in a very “lossy” compressed format.. and when we go pick up the memories, they could easily be overly distorted (they are already clearly distorted by definition because of the nature of the compression !).
There is always a special twist that one puts when bringing back memories, sometimes big chunks are missing, sometimes we think we remember something since our childhood, but it could well be a creation. One may doubt that a memory of some event got distorted with time, one could even try to account with that, but the memory itself could be 100% inaccurate.. a creation of some remote past that evolved with time, a creation of some recent past, or even a current creation that one thinks is an actual memory or a possible past creation.. but that is in fact just a current creation.
Simply put: I remember I was a kid and I had this toy car in my hands. Maybe I wasn’t quite like that. Maybe it’s a memory I had created in my teenager years. Maybe I’m just imagining it now. Maybe I will keep imagining it into the future. Maybe I will not.. …a potentially totally unreliable situation.. worst case scenario !
This is a very contorted way of thinking, but it’s also something that one has to account for, when striving for truth.
Posted by Davide Pasca as Uncategorized at 2:03 AM EDT
10 Comments »
Today I was weighting 72.10 Kg. It’s probably a bit less than that since I didn’t go to the restroom (cough cough). However I’m clearly fatter..
I’ve been eating more than necessary again and also I’ve been going less often to the gym. Recently I’ve been going to the gym near work. Before I used to go more often to the one in Shibuya, where they had this rubber model of 1 Kg of fat which looked big and nasty.. rather inspirational !
Yesterday night I saw the movie “The Butterfly Effect”. The so called butterfly effect is a short description of the theory by which small, apparently insignificant events, can lead to a path of much greater events.
The story was pretty good, although the scenario wasn’t really pushing the contrast between actions and reactions. It really didn’t force any awesome disparity of weight or prolonged levels of indirection between action and reaction. Still, it was pretty entertaining, although definitely dramatic and potentially disturbing.
Without giving up too much, let’s just say that the protagonist can somewhat picture himself in alternate presents depending of his past actions.
As usual, after the movie I went to check it on imdb.com. I looked up on the protagonist and read his short biography. Funny enough, the guy was studying biochemistry when he left the studies in order to become a model ! ..Talk about butterfly effect !
This brings up a recurring thought of mine. What if… I were a stud for example !!
I’m not the ugliest person around, nor the most introverted, but surely I’m no model material (who really is a model ? This will take a separate story (anticipation: lots of people 8)).
What if, I had the looks that made women heads turn as I walked in the streets ? How good would that be ? Where would my life be today ? Backward question: how many hot looking dudes and dudettes are in the software engineering business ?
Looks is one thing but there is more: money, character. What if I were born rich and I had no need to prove myself ? What if I were more extroverted and socialized with hundreds of people ?
..those are some problems too !
You are trying to study something and foxy girls keep calling your phone.
You buy the hottest sports car and floor it one time too many.
You end up in some party, but there are no beers there, only champagne and drugs.
So, I guess I’m generally happy with whatever brought me to be myself. And, as far as the looks goes, sometimes I see some people and I think “wow maybe I should look like him”, but it’s usually a very neutral face, without any strong emotions or any strong features, definitely not on the attractive side !
Clearly, the media pushes all those studs, and my animal instinct tells me that I should have a 30cm jaw to better attract mates… but in the end, my real happiness comes with the ability to use my brain, and for that, I need an neutral face.
Posted by Davide Pasca as Uncategorized at 1:47 AM EDT
7 Comments »
To enjoy programming one has to enjoy playing with logic. A very important logical concept is to see things in an hierarchical fashion. With C++ one can create classes of classes (or nested classes). Each class has a scope or domain of influence. A sub-class will clearly imply a sub-scope.
So, for an online game for example, one has a class Session, a subclass Lobby and another subclass Player (let’s assume that the design of the hierarchy is accurate for our purpose).
With nested classes it goes:
class Session{ class Lobby { class Player { }; };};
To declare a player type now, at the main level it goes:
Session::Lobby::Player myplayer;
Within a method in Session it’s:
Lobby::Player myplayer;
And a method in Lobby will just need to do:
Player myplayer;
This definitely makes sense. Each class has it’s own scope. In Lobby, a Player type is clearly a Session::Lobby::Player.
I tried working with this. I wanted this feature to be useful but, in the end it just generated a lot of confusion.
First of all, nested class declarations must be done all at once. One doesn’t have anymore the luxury to have a separate header file for every type. It all goes in one big file, and it surely gets pretty ugly to look at.. and to understand.
If this would have been possible:
class Session{};class Session::Lobby{};class Session::Lobby::Player{};
…one could at least try to take the feature more seriously.
But even then, the sub-scope declarations not only can create confusion at the global level (could end up with several Player types), but also create confusion at all sub-levels. In this case, one ends up dealing with three different declarations for the type Player: Session::Lobby::Player, Lobby::Player and Player ..phew !!
Eventually I went back to the:
class SessionLobbyPlayer{};class SessionLobby{};class Session{};
It’s not as stylish and one may end up doing slightly more typing but, generally, it avoids a lot of confusion.
In conclusion, with C++ one feels like if he doesn’t use all the features, he’s missing on something, somewhat at fault. I think however that one has to be pragmatic. C++ is a language in continuous design. There is a bunch of people that mostly really do just that. They spend a lot of time thinking around features. Those people are much like teachers. They are the gods of C++, their business is to keep C++ expanding, so that they can be on top of it, write books about it and generally keep their status of god-at-something.
Then, there are the poor programmers, those that spend most of their time developing real software. They clearly can’t grasp all the features and subtleties of C++ nor they should, as the time spent learning all the features and possible applications, is a job on itself, only really useful if one plans on writing C++ books, teaching C++ or writing C++ compilers.
Still, it’s not fair. It’s not fair that I’m an experienced programmer, it’s my job, my passion and still, I have to use a language that it’s poisonous to master (too many misleading features). Makes me feel as I’m somewhat less as a programmer….
…ummmmm…
…fuck… …you !!!
8)
Posted by Davide Pasca as Uncategorized at 1:48 AM EDT
7 Comments »