Click here to Skip to main content
15,889,335 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Slacker00726-Jun-20 13:48
professionalSlacker00726-Jun-20 13:48 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
bandarpoker27-Jun-20 1:17
bandarpoker27-Jun-20 1:17 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Slacker00727-Jun-20 3:28
professionalSlacker00727-Jun-20 3:28 
QuestionRe: do you like globals? I do not, Sam-I-Am Pin
enhzflep28-Jun-20 22:17
enhzflep28-Jun-20 22:17 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
TheGreatAndPowerfulOz27-Jun-20 20:23
TheGreatAndPowerfulOz27-Jun-20 20:23 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
David O'Neil26-Jun-20 17:21
professionalDavid O'Neil26-Jun-20 17:21 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Nelek27-Jun-20 0:15
protectorNelek27-Jun-20 0:15 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
David O'Neil27-Jun-20 11:07
professionalDavid O'Neil27-Jun-20 11:07 
Viewing it from your perspective, it makes sense. Personally, I'd just create one instance of the logger in a global and use that everywhere. I know there is only one global, and everything in that global only has one instantiation, so I never worry about it beyond that point. I'd just make certain to place the logger at the top of the declarations in the global, so it can be used by any of the following items.

The reason I settled on the global approach is linked to my Pimple article. It just makes things easier for me. It might not make your workflows easier. I don't know, and I'm not going to be all evangelical about my methods. If it works, it works. The goal is software the user can use. Not to avoid singletons or gotos or globals.

For instance, here's the current global header for my main MIDI project at this point:
C++
#pragma once

//some includes...

//...some forward declarations

class Globals {
   //General declarations
      private:
         uchar lastNoteVelC;
         MUSIC_TIME lastNoteLenC;
         MUSIC_TIME snapValueC;
      public:
         Globals();
         ~Globals();

         void saveAppDefaults();

         enum NoteDurations { wholeNote=3072, halfNote=1536, quarterNote=768, eigthNote=384,
                     sixteenthNote=192, thirtySecondNote=96, sixtyFourthNote=48, tick=1 }
                     noteButtonDurationC;
         int noteButtonDuration() { return noteButtonDurationC; }
         uchar lastNoteVel() { return lastNoteVelC; }
         void  lastNoteVel(uchar vel) { lastNoteVelC = vel; }
         MUSIC_TIME lastNoteLen() { return lastNoteLenC; }
         void lastNoteLen(MUSIC_TIME newLen) { lastNoteLenC = newLen; }
         MUSIC_TIME snapValue() { return snapValueC; }
         void snapValue(MUSIC_TIME val) { snapValueC = val; }

   //For dealing with the ControllerNames class
      private:                                      
         std::unique_ptr<app::ControllerNames> controllersC;
      public:
         app::ControllerNames & controllerNames() { return *controllersC.get(); }

   //For dealing with the NoteValMap
      private:
         std::unique_ptr<app::NoteValMap> noteValMapC;
      public:
         app::NoteValMap & noteValMap() { return *noteValMapC.get(); }

   //For dealing with the key signatures
      private:
         std::unique_ptr<midi::KeySignatures> keySignaturesC;
      public:
         midi::KeySignatures & keySignatures() { return *keySignaturesC.get(); }

   //For dealing with the synching of multiple ports
      private:
         bool useSyncLogicC;
      public:
         void useSyncLogic(bool state) { useSyncLogicC = state; }
         inline bool useSyncLogic() { return useSyncLogicC; }

   //For dealing with the playing of any events prior to the cursor position which
   //affect the music at that point when play is pressed
      private:
         bool playPriorEventsC;
      public:
         bool playPriorEvents() { return playPriorEventsC; }
         void playPriorEvents(bool state) { playPriorEventsC = state; }

   //For dealing with the screen ticks per pixel
      private:
         double ticksPerPixelC;
         int    keyHeightC;
      public:
         inline double ticksPerPixel()           { return ticksPerPixelC; }
         inline void   ticksPerPixel(double val) { ticksPerPixelC = val; }
         int   keyHeight()              { return keyHeightC; }
         void  keyHeight(int height)    { keyHeightC = height; }

   //For dealing with wether the user wants to display ticks or time
      public:
         enum DisplayVal { DisplayTicks, DisplayTimes };
      private:
         DisplayVal displayValC;
      public:
         DisplayVal displayVal() { return displayValC; }
         void       displayVal(DisplayVal val) { displayValC = val; }

   //For dealing with the sleep time when midi ports stop playing
      private:
         int stopSleepTimeC;
      public:
         int stopSleepTime() { return stopSleepTimeC; }  

   //For showing the help file at startup
      private:
         bool showHelpOnStartupC;
      public:
         bool showHelpOnStartup() { return showHelpOnStartupC; }
         void showHelpOnStartup(bool state) { showHelpOnStartupC = state; }

   //For dealing with the stop method
      public:
         enum StopMethod { PlayNotesOff, PlayOmniModeOff, PlayOmniModeOn, PlayMonoModeOn,
                     PlayPolyModeOn, PlayNothing };
      private:                                   
         StopMethod stopMethodC;
      public:
         StopMethod stopMethod() { return stopMethodC; }
         void stopMethod(StopMethod newMethod) { stopMethodC = newMethod; }

   //For dealing with the last piano view bottom note:
      private:
         int pianoViewBottomNoteC;
      public:
         int pianoViewBottomNote() { return pianoViewBottomNoteC; }
         void pianoViewBottomNote(int x) { pianoViewBottomNoteC = x; }

   //For dealing with the UIManager:
      private:
         std::unique_ptr<ui::UIManager> uiManagerC;
      public:
         ui::UIManager & uiMan() { return *uiManagerC.get(); }

   //For dealing with the medit::Strings:
      private:
         std::unique_ptr<medit::Strings> stringsC;
      public:
         medit::Strings & strings() { return *stringsC.get(); }

   //For dealing with the initialization file path:
      public:
         wString appWorkDir();

   //For user interface definitions:
      private:
         int buttonHeightC = 25;

      public:
         int buttonHeight() { return buttonHeightC; }

   //A global date versioner:
      private:
         std::unique_ptr<utils::DateVersioner> dateVersionerC;
      public:
         utils::DateVersioner * dateVersioner() { return dateVersionerC.get(); }

   //A global for triggering setpoints:
      public:
         bool breakPointTrigger;

   //For the minimum # of ticks between curve events:
      private:
         int minTicksBetweenCurveEventsC;
      public:
         int minTicksBetweenCurveEvents() { return minTicksBetweenCurveEventsC; }

   };

It is simple, and I control the instantiation order of everything in it. Some of it is rarely used in the rest of the codebase, but the UIManager it extensively accessed from almost everywhere for all the color choices and everything else related to UI. Knowing that things are there, my life as a programmer is easier.

As per gotos, I only have a few in my work. Here's one location that seems to be a good choice, from my perspective. It is the way my brain thinks of the solution. I know they could be eliminated, but I believe it would take several more lines of code to do so, and I have better things to worry about than whether the code is 'goto'-free:
C++
void Repeat::createRepeatedMsgs() {
   if (!createdMsgsC.empty()) throw dwl::Exception(_T("Delete created msgs prior to "
               "creation"));
   app::MsgPlayMap & holder = trackC.playMap();
   holder.setPtrPos(s_cast<MUSIC_TIME>(0));
   midi::BaseMsg * msg;
   midi::BaseMsg * msgToCopy;
   while ((msg = holder.msgAtPtr()) && msg != nullptr && msg->tick() < endTickC) {
      if (msg->tick() < tickC) goto breakout;
      for (size_t i=1, count=numRepeatsC; i<=count; ++i) {
         midi::BaseMsg::Type type = msg->type();
         if (type == midi::BaseMsg::Type::NoteOn) {
            midi::NoteOn * noteOn = s_cast<midi::NoteOn *>(msg);
            midi::Note * note = s_cast<midi::Note*>(noteOn->parent());
            if (note) msgToCopy = note;
            else msgToCopy = noteOn;
            }
         else if (type == midi::BaseMsg::Type::NoteOff) goto breakout;
         else if (type == midi::BaseMsg::Type::KeyAftertouch) goto breakout;
         else msgToCopy = msg;

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

         std::unique_ptr<midi::BaseMsg> newMsg = msgToCopy->copyToTick(msgToCopy->tick() +
                     durationC * i);
         if (newMsg->type() == midi::BaseMsg::Type::Note) {
            s_cast<midi::Note*>(newMsg.get())->noteOn().copyOf((midi::BaseMsg*)1);
            s_cast<midi::Note*>(newMsg.get())->noteOff().copyOf((midi::BaseMsg*)1);
            }
         createdMsgsC.push_back(newMsg.get());
         newMsg.release(); //It is in the vector
         }
breakout:
      holder.incMsgPtr();
      }

   //Make the created messages visible OUTSIDE of the loop, to keep the iterator valid:
   for (auto msg: createdMsgsC) {
      msg->addToVisible();
      }
   }

GeneralRe: do you like globals? I do not, Sam-I-Am Pin
#realJSOP27-Jun-20 3:24
mve#realJSOP27-Jun-20 3:24 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus27-Jun-20 6:02
mvaraddevus27-Jun-20 6:02 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
David O'Neil27-Jun-20 11:10
professionalDavid O'Neil27-Jun-20 11:10 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Greg Utas27-Jun-20 4:10
professionalGreg Utas27-Jun-20 4:10 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus27-Jun-20 6:11
mvaraddevus27-Jun-20 6:11 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus27-Jun-20 5:58
mvaraddevus27-Jun-20 5:58 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
David O'Neil27-Jun-20 11:11
professionalDavid O'Neil27-Jun-20 11:11 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus28-Jun-20 12:32
mvaraddevus28-Jun-20 12:32 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
David O'Neil28-Jun-20 12:42
professionalDavid O'Neil28-Jun-20 12:42 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus28-Jun-20 13:14
mvaraddevus28-Jun-20 13:14 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Super Lloyd26-Jun-20 17:39
Super Lloyd26-Jun-20 17:39 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus27-Jun-20 5:59
mvaraddevus27-Jun-20 5:59 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Jörgen Andersson26-Jun-20 21:51
professionalJörgen Andersson26-Jun-20 21:51 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
Richard MacCutchan26-Jun-20 23:24
mveRichard MacCutchan26-Jun-20 23:24 
GeneralRe: do you like globals? I do not, Sam-I-Am Pin
raddevus27-Jun-20 5:53
mvaraddevus27-Jun-20 5:53 
RantPlease don't microwave library books! Pin
Cp-Coder26-Jun-20 8:03
Cp-Coder26-Jun-20 8:03 
GeneralRe: Please don't microwave library books! Pin
ZurdoDev26-Jun-20 10:20
professionalZurdoDev26-Jun-20 10:20 

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.