What’s wrong with this pseudo-code?

Asked

Viewed 270 times

3

Who is following the Sof-br saw that I asked something about pseudo-code a few hours ago (here if you want to follow the context)

Well, I need to make a pseudo-code in English that serves as a basis to implement this code in Java, C# and C++, Javascript and other languages (some already implemented).

So I ventured to make this pseudo-code, but I don’t know if it’s correct, can you tell me how to make it more righteous and befitting reality? Is that right? What to change?

STRUCTURE TYPECONNECTION
Client 
Server
END TYPECONNECTION

VARIABLES
TypeConnection,
NetworkStream,
Writer,
Reader,
Server,
Client,
Ip,
Port
IsStarted
ENDVARIABLES

STRUCTURE SOKETSUPPORT

PROCEDURE CONFIGURE
    SWICH TypeConnection
        CASE TypeConnection is Server
            initialize server with ip and port
            start Server
        END CASE
        CASE TypeConnection is Client
            initialize client
        END CASE
    END SWICH
END CONFIGURE

RETURNS true
END CONFIGURE 

PROCEDURE SENDDATA(data)

    TRY
        IF
            SWITCH TypeConnection
                CASE TypeConnection is Server
                    write data in Writer
                    flushes writer 
                    closes soket
                END CASE    
                CASE TypeConnection is Client
                    initialize client with ip and port
                    initialize NetworkStream
                    initialize Writer
                    initialize Reader
                    IF Client is connected
                        write data in Writer
                        flushes writer
                    END IF
                END CASE
            END SWITCH
        END IF
        RETURNS IsStarted
    END TRY
    CATCH
        RETURNS false
    END CATCH

END SENDDATA

PROCEDURE RECEIVEDATA
    TRY
        IF IsStarted
        SWITCH TypeConnection
            CASE TypeConnection is server
                Accept socket
                initialize NetworkStrem with socket
                initialize Reader with NetworkStrem
                RETURNS Reader's reading
            END CASE
            CASE TypeConnection is client
                RETURNS Reader's reading
                close client
            END CASE
        END SWITCH
        END IF
    END TRY
    CATCH
        RETURNS Exception message
    END CATCH
END RECEIVEDATA

END SOKETSUPPORT
  • 2

    If this is pseudo-code, the only thing I see wrong is that socket in English is written SOCKET with 'c'. And the program will always return true. I see no use in always returning the same constant value, especially when it is a primitive value as the value for true, i.e.: I can quietly ignore the return of the program because it will always end up in the same.

  • 1

    But the structure of a pseudo-code is that right then? There is no right or wrong way to write?

  • 2

    There’s no such thing as "official" pseudocode, so you can’t discuss syntax or semantics. Other than that, you demonstrate how you want to assemble a data structure with clarity and precision.

  • 1

    hmm, thank you so much, I thought it had some pattern because I researched it and I didn’t find it, so answer that there is no pattern defined that I mark as certain.

1 answer

2


There is no "official" pseudo-code language. There are several different ways of expressing itself textually about how a structure should be, or how it should behave; but since pseudocode is not interpreted or compiled by a machine, any attempt to point out an error in the style tends to pedantism. The most important thing is that the shape of the structures and the logic of the operations are clear.

However, several programming courses try to introduce a grammar to pseudocode, which I find somewhat forced. In these cases, I recommend following the course pattern, but without losing sleep nights because of this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.