1. Dashboard
  2. Forum
    1. Unerledigte Themen
  3. Mitglieder
    1. Letzte Aktivitäten
    2. Benutzer online
    3. Team-Mitglieder
    4. Trophäen
    5. Mitgliedersuche
  4. Tutorial Bereich
  • Anmelden
  • Registrieren
  • Suche
Dieses Thema
  • Alles
  • Dieses Thema
  • Dieses Forum
  • Seiten
  • Forum
  • Lexikon
  • Erweiterte Suche
  1. Informatik Forum
  2. Webmaster & Internet
  3. Entwicklung

Programmier Strukturen (C++/Universell)

    • Frage
  • kim366
  • 24. November 2016 um 14:02
  • Unerledigt
  • kim366
    1
    kim366
    Mitglied
    Punkte
    10
    Beiträge
    1
    • 24. November 2016 um 14:02
    • #1

    Ich verfasse diesen Post auf Englisch, weil mir das leichter fällt (ja, meine Muttersprache ist Deutsch). Falls Ihr lieber auf Deutsch antwortet, tut dies. I have no idea what the right word for this is. Is it structure? Paradigm? Strategy?

    I have the following struct:

    Code
    namespace o
    {
        struct Mouse
        {
            // Button Pressed This Frame
            bool bptf;
    
    
    
    
            // Button Released This Frame
            bool brtf;
    
    
    
    
            // Button Pressed
            bool bp = false;
    
            // Mouse Properties
            sf::Mouse::Button button;
            sf::Vector2i position;
        };
    
    
    
    
    } // Namespace o
    Alles anzeigen

    All the properties are being updated every frame. The variable name

    Code
    buttonPressedThisFrame

    is extremely long, so I have shortened it to

    Code
    bptf

    since it is commented at the declaration anyways. Is this the correct thing to do?

    I also have a function, which is called every frame, called

    Code
    void handleInput(o::Mouse m)

    which surprisingly enough handles all the input. The project I am currently working on has to do with nodes and mouse controls are there for the selection and moving of said nodes. So in that function I have a few if-statements. Is it better to have completely dry code and only have every if-statement once, or is it better to logically separate them?

    Examle:

    Code
    void handleInput(o::Mouse m)
    {
        if (m.bp)
        {
            // Selection of Nodes
    
    
    
    
            // Moving of Nodes
        }
    
    
    
    
        else if (m.brtf)
        {
            // Selection of Nodes
    
    
    
    
            // Moving of Nodes
        }
    }
    Alles anzeigen

    vs

    Code
    void handleInput(o::Mouse m)
    {
        // Selection of Nodes
        if (m.bp)
        {
    
    
    
    
        }
    
    
    
    
        else if (m.brtf)
        {
    
    
    
    
        }
    
    
    
    
        // Moving of Nodes
        if (m.bp)
        {
    
    
    
    
        }
    
    
    
    
        else if (m.brtf)
        {
    
    
    
    
        }
    }
    Alles anzeigen

    What is to be said about my project, is that those if-statements become quite nested (up to 6 tabstops with else-statements to each of them) and also cover keyboard input combined with mouse input (e.g. Ctrl + LMB).

    Which of these strategies is the correct/best one to use?

  • stackoverflow
    5
    stackoverflow
    Mitglied
    Reaktionen
    8
    Punkte
    258
    Beiträge
    49
    • 6. Dezember 2016 um 20:53
    • #2
    Zitat

    Is this the correct thing to do?

    Würde unbedingt von solchen Abkürzungen abraten. Bei buttonPressedThisFrame weiß ich sofort was die Bedeutung der Variable ist (sofern sie korrekt benannt ist).
    Vor allem in großen Projekten ist gute Benennung extrem wichtig.


    Zitat

    So in that function I have a few if-statements. Is it better to have completely dry code and only have every if-statement once, or is it better to logically separate them?

    Das Beispiel ist etwas abstrakt, d.h. kann ich dir da konkret nicht viel dazu sagen.
    Aber wann immer du Codeduplizierung vermeiden kannst, mach es.
    Vielleicht nennst du noch ein paar mehr Details, dann kann man zum konkreten Fall mehr sagen.

  • Maximilian Rupp 27. Dezember 2024 um 00:26

    Hat das Thema aus dem Forum Programmieren nach Entwicklung verschoben.

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!

Benutzerkonto erstellen Anmelden

Rechtliches

Impressum

Datenschutzerklärung