Click here to Skip to main content
14,333,447 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: let, var, val - Swift, Kotlin, JavaScript Pin
MSBassSinger13-Jun-19 5:23
professionalMSBassSinger13-Jun-19 5:23 
GeneralRe: let, var, val - Swift, Kotlin, JavaScript Pin
Maximilien17-Jun-19 8:10
memberMaximilien17-Jun-19 8:10 
GeneralRe: let, var, val - Swift, Kotlin, JavaScript Pin
raddevus18-Jun-19 2:00
mvaraddevus18-Jun-19 2:00 
GeneralMust-Have NuGet Constants Packages Pin
David A. Gray7-Jun-19 20:56
groupDavid A. Gray7-Jun-19 20:56 
GeneralKill it with fire #2 - or fml Pin
charlieg31-May-19 12:04
membercharlieg31-May-19 12:04 
GeneralRe: Kill it with fire #2 - or fml Pin
David O'Neil31-May-19 15:11
professionalDavid O'Neil31-May-19 15:11 
GeneralRe: Kill it with fire #2 - or fml Pin
Gary R. Wheeler31-May-19 16:38
memberGary R. Wheeler31-May-19 16:38 
GeneralRe: Kill it with fire #2 - or fml Pin
David O'Neil31-May-19 17:34
professionalDavid O'Neil31-May-19 17:34 
To me, this is cleaner than the alternative. I'm certain many will disagree, but this matches my thought process, and eliminating the gotos requires more indentation levels (which I despise) and thinking time. And if it doesn't require more indentation levels, it still requires more time! I agree with not using gotos often.
void Repeater::createRepeatedObjs() {
   if (!createdObjsC.empty()) throw dwl::Exception(_T("Delete created objs prior to "
               "creation"));
   app::ObjMap & holder = appC.objMap();
   holder.setPtrPos(s_cast<OBJECT_TIME>(0));
   object::BaseObj * obj;
   object::BaseObj * objToCopy;
   while ((obj = holder.objAtPtr()) && obj != nullptr && obj->time() < endTimeC) {
      if (obj->time() < timeC) goto breakout;
      for (size_t i=1, count=numRepeatsC; i<=count; ++i) {
         object::BaseObj::Type type = obj->type();
         if (type == object::BaseObj::Type::TypeOne) {
            object::TypeOne * typeOne = s_cast<object::TypeOne *>(obj);
            object::ObjParent * parent = s_cast<object::ObjParent*>(typeOne->parent());
            if (parent) objToCopy = parent;
            else objToCopy = typeOne;
            }
         else if (type == object::BaseObj::Type::TypeTwo) goto breakout;
         else if (type == object::BaseObj::Type::TypeThree) goto breakout;
         else objToCopy = obj;

         //Don't make copies of copies!
         if (objToCopy->copyOf()) goto breakout;

         std::unique_ptr<object::BaseObj> newObj = objToCopy->copyToTick(objToCopy->time() +
                     durationC * i);
         if (newObj->type() == object::BaseObj::Type::ObjParent) {
            s_cast<object::ObjParent*>(newObj.get())->typeOne().copyOf((object::BaseObj*)1);
            s_cast<object::ObjParent*>(newObj.get())->noteOff().copyOf((object::BaseObj*)1);
            }
         createdObjsC.push_back(newObj.get());
         newObj.release(); //It is in the vector
         }
breakout:
      holder.incObjPtr();
      }
   }

Forcing it into your way, I believe it becomes:
void Repeater::createRepeatedObjs() {
   if (!createdObjsC.empty()) throw dwl::Exception(_T("Delete created objs prior to "
               "creation"));
   app::ObjMap & holder = appC.objMap();
   holder.setPtrPos(s_cast<OBJECT_TIME>(0));
   object::BaseObj * obj;
   object::BaseObj * objToCopy;
   while ((obj = holder.objAtPtr()) && obj != nullptr && obj->time() < endTimeC) {
      if (obj->time() < timeC) goto breakout;
      for (size_t i=1, count=numRepeatsC; i<=count; ++i) {
         object::BaseObj::Type type = obj->type();

         if (!(type == object::BaseObj::Type::TypeTwo ||
               type == object::BaseObj::Type::TypeThree)) {
            if (type == object::BaseObj::Type::TypeOne) {
               object::TypeOne * typeOne = s_cast<object::TypeOne *>(obj);
               object::ObjParent * parent = s_cast<object::ObjParent*>(typeOne->parent());
               if (parent) objToCopy = parent;
               else objToCopy = typeOne;
               }
            else objToCopy = obj;
            }

         //Don't make copies of copies!
         if (!(objToCopy->copyOf())) {
            std::unique_ptr<object::BaseObj> newObj = objToCopy->copyToTick(objToCopy->time() +
                        durationC * i);
            if (newObj->type() == object::BaseObj::Type::ObjParent) {
               s_cast<object::ObjParent*>(newObj.get())->typeOne().copyOf((object::BaseObj*)1);
               s_cast<object::ObjParent*>(newObj.get())->noteOff().copyOf((object::BaseObj*)1);
               }
            createdObjsC.push_back(newObj.get());
            newObj.release(); //It is in the vector
            }
         }
      holder.incObjPtr();
      }
   }
That is much uglier to me, especially with the !.


modified 31-May-19 23:50pm.

GeneralRe: Kill it with fire #2 - or fml Pin
charlieg1-Jun-19 3:22
membercharlieg1-Jun-19 3:22 
GeneralRe: Kill it with fire #2 - or fml Pin
David O'Neil1-Jun-19 11:54
professionalDavid O'Neil1-Jun-19 11:54 
GeneralRe: Kill it with fire #2 - or fml Pin
charlieg1-Jun-19 17:43
membercharlieg1-Jun-19 17:43 
GeneralRe: Kill it with fire #2 - or fml Pin
Sander Rossel8-Jun-19 0:47
professionalSander Rossel8-Jun-19 0:47 
GeneralRe: Kill it with fire #2 - or fml Pin
David O'Neil8-Jun-19 5:49
professionalDavid O'Neil8-Jun-19 5:49 
GeneralRe: Kill it with fire #2 - or fml Pin
Rick York12-Jun-19 13:22
mveRick York12-Jun-19 13:22 
GeneralRe: Kill it with fire #2 - or fml Pin
Member 1388243212-Jun-19 20:31
memberMember 1388243212-Jun-19 20:31 
GeneralRe: Kill it with fire #2 - or fml Pin
Richard Deeming12-Jun-19 23:19
mveRichard Deeming12-Jun-19 23:19 
GeneralRe: Kill it with fire #2 - or fml Pin
charlieg1-Jun-19 3:15
membercharlieg1-Jun-19 3:15 
GeneralRe: Kill it with fire #2 - or fml Pin
Matias Lopez5-Jun-19 6:06
memberMatias Lopez5-Jun-19 6:06 
GeneralRe: Kill it with fire #2 - or fml Pin
AFell24-Jun-19 11:39
memberAFell24-Jun-19 11:39 
GeneralRe: Kill it with fire #2 - or fml Pin
charlieg4-Jun-19 18:01
membercharlieg4-Jun-19 18:01 
GeneralRe: Kill it with fire #2 - or fml Pin
Dr.Walt Fair, PE8-Jun-19 5:34
professionalDr.Walt Fair, PE8-Jun-19 5:34 
GeneralRe: Kill it with fire #2 - or fml Pin
Jörgen Andersson12-Jun-19 10:02
protectorJörgen Andersson12-Jun-19 10:02 
GeneralRe: Kill it with fire #2 - or fml Pin
Dr.Walt Fair, PE8-Jun-19 5:32
professionalDr.Walt Fair, PE8-Jun-19 5:32 
GeneralToday's entry for the "kill it with fire" list Pin
Gary Wheeler14-May-19 7:06
memberGary Wheeler14-May-19 7:06 
GeneralRe: Today's entry for the "kill it with fire" list Pin
phil.o14-May-19 7:30
mvephil.o14-May-19 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.