Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Which function is a better approach?(solved)
Forum Updated to NodeBB v4.3 + New Features

Which function is a better approach?(solved)

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 676 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    houmingc
    wrote on last edited by
    #1

    Like to get comment which function is a better approach.

    --------------------Function 1---------------
    @
    QTcpSocket *socket;
    socket = ...... ;
    QDataStream in(socket);
    QString str;
    in >> str;

    switch(str)
    {
    case '1':
    emit checkfucntion1(str)
    break;

    case '2':
    emit checkfucntion2(str);
    break;

    }
    @
    --------------------Function 2---------------
    @
    QTcpSocket *socket;
    socket = ...... ;
    QDataStream in(socket);
    QString str;
    in >> str;
    emit checkfucntion1(str)
    @

    [Added @ around code. andreyc]

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Binary91
      wrote on last edited by
      #2

      I would really prefer the second method. What would you do if you have 1000 str possibilities? Would you write 1000 functions, each specific to your str ?

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andreyc
        wrote on last edited by
        #3

        It depends.
        If you need to parse str then somewhere you will have to use switch or other parsing methods. And you will have to create 1000 functions or 1000 classes.

        In general second approach is better because you split receiving and processing tasks. Also it will help if this function must process incoming string as fast as possible. Assuming that it runs in a separate thread.

        But if it is simple interactive application first approach maybe simpler and faster to implement.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved