return statements inside a function - function

I was wondering how people handle return statements in a function. I have a method that allocates some memory but has a return false; statement when a function goes wrong. this statement is located about half way through the function so my memory is leaked if that function fails. This isn't the only return ...; statement I have in this function. What would stackoverflow recommend doing to clean up the code in a function with a few return statements in it?
if( true == OpenFtpConnection() )
{
AfxMessageBox( _T( "Connection to internet and ftp server found" ) );
// set the directory to where the xml file lives
if( false == FtpSetCurrentDirectory( m_ftpHandle, _T(_FTP_XML_LOCATION) ) )
return false;
HINTERNET xmlHandle = NULL;
WIN32_FIND_DATA fileData;
SYSTEMTIME fileWriteTime;
xmlHandle = FtpFindFirstFile( m_ftpHandle, _T("TPCFeed.xml"), &fileData, INTERNET_FLAG_RELOAD, 0 );
if( NULL == xmlHandle )
return false;
else
{
// get the write time of the ftp file
FileTimeToSystemTime( &fileData.ftLastWriteTime, &fileWriteTime );
// get the write time of the local file
HANDLE localFileHandle = NULL;
localFileHandle = CreateFile( _T(_XML_FILENAME_PATH), FILE_READ_ATTRIBUTES,
FILE_SHARE_READ, NULL, OPEN_EXISTING,
NULL, NULL );
if( INVALID_HANDLE_VALUE == localFileHandle )
{
AfxMessageBox( _T( "opening file failed, file not found" ) );
return false;
}
else
{
CloseHandle( localFileHandle );
}
// close the FtpFindFirstFile() handle
InternetCloseHandle( xmlHandle );
}
// download xml file to disk
//if( false == FtpGetFile( m_ftpHandle, _T("TPCFeed.xml"), _T(_XML_FILENAME_PATH), FALSE,
// FILE_ATTRIBUTE_NORMAL, FTP_TRANSFER_TYPE_BINARY, 0 ) )
// return false;
}
else
{
AfxMessageBox( _T( "No Connection to internet or ftp server found" ) );
return false;
}
if( true == CloseFtpConnection() )
AfxMessageBox( _T( "Connection to internet closed" ) );
else
AfxMessageBox( _T( "Connection to internet not closed" ) );

Clarity is the most important thing. Many times, having multiple return points can make the program flow less obvious and thus more prone to bugs; but on the other hand, sometimes early returns are quite obvious; their meaning and purpose is clear. So I tend to avoid any hard rules about that.
You might get more mileage if you actually post the code you want to clean up though.

We've recently switched from the "one return per method" style to "return where it makes sense". Part of that switch was that we limit the number of lines in our methods to something reasonable (say 50 lines). By limiting the function size, the code becomes much more readable, and the multi-returns are natural, readable, and performant.

You haven't specified your programming language. Assuming it is C++: Use Boost's Smart Pointer. This not only handles multiple returns, but also exceptions thrown during method execution. If using Boost is not an option, it should be easy to create your own smart pointer class :-)

Related

OpenH264 concealing errors

I can't work out how to get error concealment working in OpenH264. My initialisation code looks like this:
SDecodingParam sDecParam = {0};
sDecParam.sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_SVC;
sDecParam.bParseOnly = false;
sDecParam.eEcActiveIdc = ERROR_CON_SLICE_MV_COPY_CROSS_IDR_FREEZE_RES_CHANGE;
if ( 0 == WelsCreateDecoder ( &decoder ) && decoder != nullptr && 0 == decoder->Initialize(&sDecParam) )
Obviously I've tried every possible option for concealment type, without much success
Finally worked this out by stepping through the source of OpenH264:
in decoder setup set eEcActiveIdc to one of the concealment types (e.g. ERROR_CON_SLICE_MV_COPY_CROSS_IDR)
decode with DecodeFrame2 and NOT DecodeFrameNoDelay
Ignore the result code and only look at iBufferStatus in the info block, to see if a buffer is available.
Downside is that this introduces extra latency, so definitely not useful for all applications

How to convert large UTF-8 encoded char* string to CStringW (UTF-16)?

I have a problem with converting a UTF-8 encoded string to a UTF-16 encoded CStringW.
Here is my source code:
CStringW ConvertUTF8ToUTF16( __in const CHAR * pszTextUTF8 )
{
_wsetlocale( LC_ALL, L"Korean" );
if ( (pszTextUTF8 == NULL) || (*pszTextUTF8 == '\0') )
{
return L"";
}
const size_t cchUTF8Max = INT_MAX - 1;
size_t cchUTF8;
HRESULT hr = ::StringCbLengthA( pszTextUTF8, cchUTF8Max, &cchUTF8 );
if ( FAILED( hr ) )
{
AtlThrow( hr );
}
++cchUTF8;
int cbUTF8 = static_cast<int>( cchUTF8 );
int cchUTF16 = ::MultiByteToWideChar(
CP_UTF8,
MB_ERR_INVALID_CHARS,
pszTextUTF8,
-1,
NULL,
0
);
CString strUTF16;
strUTF16.GetBufferSetLength(cbUTF8);
WCHAR * pszUTF16 = new WCHAR[cchUTF16];
int result = ::MultiByteToWideChar(
CP_UTF8,
0,
pszTextUTF8,
cbUTF8,
pszUTF16,
cchUTF16
);
ATLASSERT( result != 0 );
if ( result == 0 )
{
AtlThrowLastWin32();
}
strUTF16.Format(_T("%s"), pszUTF16);
return strUTF16;
}
pszTextUTF8 is htm file's content in UTF-8.
When htm file's volume is less than 500kb, this code works well.
but, when converting over 500kb htm file, (ex 648KB htm file that I have.)
pszUTF16 has all content of file, but strUTF16 is not. (about half)
I guess File open is not wrong.
In strUTF16 m_pszData has all content how to I get that?
strUTF16.Getbuffer(); dosen't work.
The code in the question is stock full of bugs, somewhere in the order of 1 bug per 1-2 lines of code.
Here is a short summary:
_wsetlocale( LC_ALL, L"Korean" );
Changing a global setting in a conversion function is unexpected, and will break code calling that. It's not even necessary either; you aren't using the locale for the encoding conversion.
HRESULT hr = ::StringCbLengthA( pszTextUTF8, cchUTF8Max, &cchUTF8 );
This is passing the wrong cchUTF8Max value (according to the documentation), and counts the number of bytes (vs. the number of characters, i.e. code units). Besides all that, you do not even need to know the number of code units, as you never use it (well, you are, but that is just another bug).
int cbUTF8 = static_cast<int>( cchUTF8 );
While that fixes the prefix (count of bytes as opposed to count of characters), it won't save you from using it later on for something that has an unrelated value.
strUTF16.GetBufferSetLength(cbUTF8);
This resizes the string object that should eventually hold the UTF-16 encoded characters. But it doesn't use the correct number of characters (the previous call to MultiByteToWideChar would have provided that value), but rather chooses a completely unrelated value: The number of bytes in the UTF-8 encoded source string.
But it doesn't just stop there, that line of code also throws away the pointer to the internal buffer, that was ready to be written to. Failure to call ReleaseBuffer is only a natural consequence, since you decided against reading the documentation.
WCHAR * pszUTF16 = new WCHAR[cchUTF16];
While not a bug in itself, it needlessly allocates another buffer (this time passing the correct size). You already allocated a buffer of the required size (albeit wrong) in the previous call to GetBufferSetLength. Just use that, that's what the member function is for.
strUTF16.Format(_T("%s"), pszUTF16);
That is probably the anti-pattern associated with the printf family of functions. It is the convoluted way to write CopyChars (or Append).
Now that that's cleared up, here is the correct way to write that function (or at least one way to do it):
CStringW ConvertUTF8ToUTF16( __in const CHAR * pszTextUTF8 ) {
// Allocate return value immediately, so that (N)RVO can be applied
CStringW strUTF16;
if ( (pszTextUTF8 == NULL) || (*pszTextUTF8 == '\0') ) {
return strUTF16;
}
// Calculate the required destination buffer size
int cchUTF16 = ::MultiByteToWideChar( CP_UTF8,
MB_ERR_INVALID_CHARS,
pszTextUTF8,
-1,
nullptr,
0 );
// Perform error checking
if ( cchUTF16 == 0 ) {
throw std::runtime_error( "MultiByteToWideChar failed." );
}
// Resize the output string size and use the pointer to the internal buffer
wchar_t* const pszUTF16 = strUTF16.GetBufferSetLength( cchUTF16 );
// Perform conversion (return value ignored, since we just checked for success)
::MultiByteToWideChar( CP_UTF8,
MB_ERR_INVALID_CHARS, // Use identical flags
pszTextUTF8,
-1,
pszUTF16,
cchUTF16 );
// Perform required cleanup
strUTF16.ReleaseBuffer();
// Return converted string
return strUTF16;
}

Corona Lua call external function

I have block_basic.lua that I want to call another function in touch_input.lua
block_basic.lua does:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
local function touched( event )
-- 'self' parameter exists via the ':' in function definition
print(onScreenTouch, screen_touch, event)
end
From what I am seeing event seems to be correct (a table), screen_touch is also correct. But the function (screen_touch.onScreenTouch) is always nil and I don't know why
In touch_input.lua I simply have:
local function onScreenTouch( event )
-- no 'self' parameter
etc. etc.
Why is it nil? Why can't I call it?
You don't show what you return in touch_input.lua, but if you expect the first two lines of your script to work, it needs to be something like this:
local function onScreenTouch( event )
...
return {
onScreenTouch = onScreenTouch
}
Since you don't get a run-time error on line 2, you may be returning a table already, but you need to make sure that onScreenTouch field of that table points to onScreenTouch function.
Here is how your files should be:
touch_input.lua:
local M = {}
M.onScreenTouch = function( event )
--some code here
end
return M
block_basic.lua:
local screen_touch = require( "touch_input")
local onScreenTouch = screen_touch.onScreenTouch
print(onScreenTouch, screen_touch, event)
I tested it. It works 100%.
More info:
http://www.coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/
http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/
http://developer.coronalabs.com/content/modules
http://www.coronalabs.com/blog/2011/07/06/using-external-modules-in-corona/

mysql_affected_rows() always returns 1 even though no row was updated

What I am trying to do is: (programmatically)
Update status where id is something, if no rows where updated, give error: we cannot find the record with id something, otherwise give message success.
Here I am using mysql_affected_rows() to know if a row was updated or not, but it always return 1, so the user gets a success message, even though there was no row updated.
Can anyone tell me what could it be?
Here's the code:
function update_sql($sql) {
$this->last_query = $sql;
$r = mysql_query($sql);
if (!$r) {
$this->last_error = mysql_error();
return false;
}
$rows = mysql_affected_rows();
if ($rows == 0) return true; // no rows were updated
else return $rows; }
This code returns 1.
That is because true will print out as "1" if you use echo. For debugging try using var_dump(), or let your function return 0 (which seems to me, in this case, the better option).
One little note; I think you should try to make your code a bit more readable (if the code in your question has the same layout as the code in your file). Try to indent code blocks, use separate lines for closing curly brackets, etc...
This is just a guess...
Maybe your function works as excepted? Maybe this piece of code if ($rows == 0) return true; works fine, and returns true but you treat that value as integer (boolean true can be displayed as 1)? Do: var_dump(uddated_sql('YOUR QUERY')) and check whether it returns boolean true or integer 1 value.

PHP + MySQL profiler

You know how vBulletin has a sql profiler when in debug mode? How would I go about building one for my own web application? It's built in procedural PHP.
Thanks.
http://dev.mysql.com/tech-resources/articles/using-new-query-profiler.html
The above link links how you can get al the sql profile information after any query.
Best way to implement it is to create a database class and have it have a "profile" flag to turn on logging of queries and the appropriate information as shown int he link above.
Example:
Class dbthing{
var $profile = false;
function __construct($profile = false){
if($profile){
$this->query('set profiling=1');
$this->profile = true;
}
...
}
function query($sql, $profile_this == false){
...
if($this->profile && !$profile_this)
$this->query("select sum(duration) as qtime from information_schema.profiling where query_id=1", true);
... // store the timing here
}
}
I use a database connection wrapper that I can place a profiling wrapper arround. This way I can discard the wrapper, or change it, without changing my base connector class.
class dbcon {
function query( $q ) {}
}
class profiled_dbcon()
{
private $dbcon;
private $thresh;
function __construct( dbcon $d, $thresh=false )
{
$this->dbcon = $d;
$this->thresh = $thresh;
}
function queury( $q )
{
$begin = microtime( true );
$result = this->dbcon->query();
$end = microtime( true );
if( $this->thresh && ($end - $begin) >= $this->thresh ) error_log( ... );
return $result;
}
}
For profiling with a 10 second threshold:
$dbc = new profiled_dbcon( new dbcon(), 10 );
I have it use error_log() what the times were. I would not log query performance back to the database server, that affects the database server performance. You'd rather have your web-heads absorb that impact.
Though late, Open PHP MyProfiler would help you achieve this, and you can extract functional sections from the code for your usage.