Which function is a better approach?(solved)



  • 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]



  • 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 ?



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.