Is this bad practice?

Started by
65 comments, last by rsegal 20 years, 4 months ago
quote:Original post by Estor
exeptions... exeptions... ok show me please, haw will be lookin this code with exeprions ???
Boring task. That code is C, not C++ so fixing it to use exceptions and the idioms that go with it (RAII) would mean rewriting it. With C++ stuff (e.g. streams, strings, exceptions) it''d look pretty different and a lot shorter, that I can assure.

Buy a C++ book.
Advertisement
quote:Original post by Estor
exceptions... exceptions... ok show me please, haw will be lookin this code with exceptions???
#include <stdio.h>#include <stdlib.h>int      err = 0;// Err values// 0 - no error and return value is buffer adres// if return value = NULL then// 1 - name is a NULL ponter// 2 - file is empty// 3 - out of memory// 4 - file errorchar*LoadFile (char *name){   FILE     *fhnd;   char     *buf;   size_t   len;   if (name)   {      fhnd = fopen (name, "rb");      if (fhnd)      {         fseek (fhnd, 0, SEEK_SET);         fseek (fhnd, 0, SEEK_END);         len = ftell (fhnd);         fseek (fhnd, 0, SEEK_SET);         if (len)         {            buf = (char*) malloc (len + 1);            if (buf)            {               if (fread (buf, 1, len, fhnd) == len)               {                  fclose (fhnd);                  return (buf);               }               err++;            }            err++;            free (buf);         }         err++;      }      err++;      fclose (fhnd);   }   err++;   return (NULL);}voidSYS_Error (char* e){   if (e)      printf ("%s", e);   exit(1);}intmain (){   char *ptr = LoadFile ("blebleble.txt");   if (!ptr)   {      switch (err)      {         case 1: SYS_Error ("Invalid file name"); break;         case 2: SYS_Error ("file is empty"); break;         case 3: SYS_Error ("out of memory"); break;         case 4: SYS_Error ("file error"); break;         default: SYS_Error ("???? what a hell ????"); break;      }   }   free (ptr);   return (0);}
#include <fstream>#include <iostream>#include <sstream>#include <stdexcept>#include <string>std::string LoadFile(const std::string& name){	std::ifstream foo(name.c_str());	std::stringstream bar;	if (foo.fail()) throw std::runtime_error("Couldn''t open file.");	bar << foo.rdbuf();	if (foo.fail()) throw std::runtime_error("Error reading file.");		std::string result = bar.str();	if (result.size() == 0) throw std::runtime_error("File is empty.");	return result;}int main(){	try	{		std::string text = LoadFile("\\foo.txt");	}	catch (std::exception& e)	{		std::cout << e.what() << std::endl;	}}
I think I win.
OK i giveup, Beer Hunter You win.


"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We Would
When Losers Say Its Over With You Know That It’s A Lie The Gods Made Heavy Metal And It’s Never Gonna Die"

THE GODS MADE HEAVY METAL/by ManOwaR
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
Ok so here's a new for you guys since I got such an awesome response from my first post. Should void functions be using return.
void MyFunc(int *aNumber){  if (aNumber == NULL)  {    return;  }  else     {    *aNumber++;  }} 

The visual c++ compiler is cool with it and execution of such a call seems to have no problems. I have no problem with it either. I really think this is an aestethic thing, something that a teacher might take marks off for.

[edited by - rsegal on December 17, 2003 3:09:51 PM]
Rob Segal - Software Developerrob@sarcasticcoder.com
That seems fine to me.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by rsegal
Ok so here''s a new for you guys since I got such an awesome response from my first post. Should void functions be using return.
void MyFunc(int *aNumber){  if (aNumber == NULL)  {    return;  }  else     {    *aNumber++;  }}  

...


void MyFunc(int *aNumber){  if (aNumber == NULL)    return;  *aNumber++;}  

I think there is no need for else


"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We Would
When Losers Say Its Over With You Know That It’s A Lie The Gods Made Heavy Metal And It’s Never Gonna Die"

THE GODS MADE HEAVY METAL/by ManOwaR
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
quote:Original post by rsegal
Ok so here''s a new for you guys since I got such an awesome response from my first post. Should void functions be using return.
void MyFunc(int *aNumber){  if (aNumber == NULL)  {    return;  }  else     {    *aNumber++;  }}  

The visual c++ compiler is cool with it and execution of such a call seems to have no problems. I have no problem with it either. I really think this is an aestethic thing, something that a teacher might take marks off for.

[edited by - rsegal on December 17, 2003 3:09:51 PM]



I like this better:

void MyFunc(int *aNumber){  if (aNumber)    *aNumber++;}


There''s nothing wrong with putting a return statement in a void function, but there''s usually no need to because you can probably rewrite the function differently so as to not need it.

This topic is closed to new replies.

Advertisement