Page 1 of 1

Using pseudo-code to design application logic What is pseudo-code and how to use it.

#1 PixelCard  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 139
  • Joined: 20-June 08

Posted 29 July 2008 - 10:01 AM

In this tutorial I will describe the methods of designing the application logic using the pseudo-code.

Introduction

Before developing an application it needs to be somehow designed - on a piece of paper, using UML or just by creating a prototype. Another way to design the application logic is using pseudo-code.

What is pseudo-code and how to use it?

Pseudo-code is a non-formal language, a way to create a logical structure, describing the actions, which will be executed by the application. Using pseudo-code, the developer describes the application logic using his native language, without applying the structural rules of a specific programming language. The big plus of the pseudo-code is that the application logic can be easily understood by any developer in the development team (in this case, it doesnít depend, which programming language knows each team member). Also, when the application algorithm is described in pseudo-code, it is very easy to transform the pseudo-code into real code (using any programming language). For a better understanding of what is pseudo-code, letís take a look at an example. Suppose that you have to develop an application, that gets the number of students in a high school and then it gets each studentís final grade (100, 90, 80, 60 and 50) and processes the average grade for the whole school. Instead of creating the program at a computer, letís describe the application logic using pseudo-code. First, I will describe the general purpose of the application:

Process the average grade for the whole school


Now, I need to specify the fundamental actions that are needed to process the final result:
Request the number of students;
Get the grade for each student;
Process the average grade for the whole school;


This is a generalized description, but it describes the basic application logic. Now, I have to specify some more details (how the above actions are performed):

Application start;
  Declare an integer variable numberOfStudents;
  Declare an integer variable counter;
  Declare an integer variable sum;
  Declare a decimal variable average;

  Set the numberOfStudents value to 0;
  Set the counter value to 0;
  Set the sum value to 0;

  Request the number of students (numberOfStudents);

  If the entered number of students is 0 then
		Exit the application
  Else
	While the counter is less or equal to the number of students
		Get the student grade
		Add the student grade to the sum variable
		Increment the counter by 1.
		Get the next student information

  When the student information is entered, set the average variable to the result of the division (sum divided by the number of students).
  Show the result.
Application end.


As you see, I used a human language to describe the application logic. Also, to better see the code fragments, I used a specific indentation style, so I can easily see which part of the algorithm is executing a specific action.

Some developers may think that using pseudo-code to design the application instead of creating and testing the application on the computer is a waste of time. This is true for small modules, that were created many times and which require just some small modifications to accomplish a specific task, but when it comes to large projects, itís pretty easy to be lost in the hundreds or even thousands of code lines. In this case, the pseudo-code clearly describes the application algorithms, so these can be easily implemented and it gives the developer an opportunity to think on the algorithm before implementing it.

Is This A Good Question/Topic? 1
  • +

Replies To: Using pseudo-code to design application logic

#2 williamgeorgegardner  Icon User is offline

  • CEO of GeekTelligence
  • member icon

Reputation: 19
  • View blog
  • Posts: 584
  • Joined: 27-December 09

Posted 14 March 2010 - 01:25 PM

This is a great tutorial!!!!
Was This Post Helpful? 0
  • +
  • -

#3 f1alan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-August 10

Posted 10 August 2010 - 12:55 AM

View PostPixelCard, on 29 July 2008 - 09:01 AM, said:

Some developers may think that using pseudo-code to design the application instead of creating and testing the application on the computer is a waste of time. This is true for small modules, that were created many times and which require just some small modifications to accomplish a specific task, but when it comes to large projects, it’s pretty easy to be lost in the hundreds or even thousands of code lines. In this case, the pseudo-code clearly describes the application algorithms, so these can be easily implemented and it gives the developer an opportunity to think on the algorithm before implementing it.


Hi,

This is a very good tutorial for those unfamiliar with pseudocode. I am also of the opinion that designing methods properly using pseudocode is extremely valuable. By jumping straight into the code, it is very easy to get distracted by implementation details. Having a solid design will result in better quality code.

We've seen particular benefits of Code Rocket which can produce code and flowchart from your pseudocode.

Well done again on a good post.

Alan

This post has been edited by f1alan: 10 August 2010 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 bennitto  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 64
  • Joined: 28-November 09

Posted 14 August 2010 - 05:55 PM

I have known of pseudo code and I often use it but I tend to use a more general form as I am still learning to program and working on programs reaching barely 500 lines.

I tend to start with the computer straight away(which sometimes is not a good idea) but using pseudo code afterwards helps me solve problems easier
Was This Post Helpful? 0
  • +
  • -

#5 pablo9891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 19-December 09

Posted 10 June 2012 - 02:15 AM

Great Tutorial!!. I'd like to add something and that is that the abstract model of what you are going to build in a computer (the algorithm)is a really important thing in computing in general, even if you are doing a really small program you always have to keep that model in mind. Pseudocode is a way of treating that abstraction in a direct way and it allows you to see alternatives and mistakes in what you are going to code then.
Was This Post Helpful? 0
  • +
  • -

#6 ladkins1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 15-June 14

Posted 15 June 2014 - 11:23 AM

I'm new to coding and in the midst of a career change. As I have been studying, I want to get all that I can from my lessons, however, I feel that it can be intimidating. However, your breakdown of pseudo-code is breath of fresh air. Thank you for your layman terms and making it plain and simple. I look forward to learning from you and this site. And yes, I'm doing my OWN homework. :-)
Was This Post Helpful? 0
  • +
  • -

#7 k.dombek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-August 14

Posted 06 August 2014 - 08:33 PM

View Postbennitto, on 14 August 2010 - 05:55 PM, said:

I have known of pseudo code and I often use it but I tend to use a more general form as I am still learning to program and working on programs reaching barely 500 lines.

I tend to start with the computer straight away(which sometimes is not a good idea) but using pseudo code afterwards helps me solve problems easier

Serious suggestion - do the pseudo code first - then you won't waste the time spent coding the wrong thing.
Was This Post Helpful? 0
  • +
  • -

#8 turboscrew  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 133
  • View blog
  • Posts: 915
  • Joined: 03-April 12

Posted 19 March 2016 - 07:32 AM

My pseudocode description of rpi_stub ( https://github.com/turboscrew/rpi_stub ):

This is a rough 'conceptual' pseudocode of the program. Here asynchronous/independent code sequences are called processes.

PROCESS loader
    handle command line parameters
    // set up exceptions and some other low level stuff (MMU, caches)
    CALL rpi2_init
    // set up serial (UART0)
    CALL serial_init
    FOREVER
        CALL gdb_init // initialize debugger
        CALL gdb_reset // initialize for debugging a program
        execute initial-entry bkpt
    ENDFOREVER
ENDPROCESS

// called from excepion initializations and reset_gdb_stub
PROCEDURE set_up_exception_vectors
    lower_vectors =
        ".word rpi2_reset_handler\n\t"
        ".word rpi2_undef_handler\n\t"
        ".word rpi2_svc_handler\n\t"
        ".word rpi2_dabt_handler\n\t"
        ".word rpi2_unhandled_dabt\n\t"
        ".word rpi2_aux_handler @ used in some cases\n\t "
        ".word rpi2_unhandled_irq\n\t"
        ".word rpi2_unhandled_fiq\n\t"

    upper_vectors =
        ".word rpi2_reroute_reset\n\t"
        ".word rpi2_reroute_exc_und\n\t"
        ".word rpi2_reroute_exc_svc\n\t"
        ".word rpi2_pabt_handler\n\t"
        ".word rpi2_reroute_exc_dabt\n\t"
        ".word rpi2_reroute_exc_aux @ in some cases\n\t "
        ".word rpi2_irq_handler\n\t"
        ".word rpi2_reroute_exc_fiq\n\t"

    IF rpi_stub_interrupt is 'fiq' THEN
        rpi2_upper_vector[IRQ] = &rpi2_reroute_exc_irq
        rpi2_upper_vector[FIQ] = &rpi2_fiq_handler
    ENDIF
ENDPROC

PROCESS rpi2_reroute_<exception>
    "mov PC, low_vector_address_of<exception>
ENDPROC

PROCESS rpi2_unhandled_<exception>
    store sp locally
    switch to rpi_stub stack
    fix return address
    store register cotext to this exception's register storage
    mark exception reason and type
    GOTO gdb_exception
ENDPROC

PROCESS pabt_exception_handler (upper vector)
    store sp locally
    switch to rpi_stub stack
    push registers
    IF debug event THEN
        IF rpi_stub breakpoint THEN
            mark exception reason and type
            pop registers
            load locally stored sp
            fix return address
            store register cotext to this exception's register storage
            GOTO gdb_exception            
        ELSE
            pop registers
            load locally stored sp
            "mov PC, low_vector_address_of_PABT
        ENDIF
    ELSE
        pop registers
        load locally stored sp
        "mov PC, low_vector_address_of_PABT
    ENDIF
ENDPROCESS

PROCESS irq_fiq_exception_handler (upper vector)
    store sp locally
    switch to rpi_stub stack
    push registers
    IF UART0 interrupt THEN
        CALL serial_interrupt_handler
        IF ctrl-C flag THEN
            clear ctrl-C flag
            set sigint flag (instead)
            mark exception reason and type
            pop registers
            load locally stored sp
            fix return address
            store register cotext to this exception's register storage
            GOTO gdb_exception
        ELSE
            IF irq THEN
                IF other irqs THEN
                    pop registers
                    load locally stored sp
                    "mov PC, low_vector_address_of<exception>
                ENDIF
            ENDIF
            pop registers
            load locally stored sp
            return from interrupt
        ENDIF
    ELSE
        pop registers
        load locally stored sp
        "mov PC, low_vector_address_of<exception>
    ENDIF
ENDPROCESS    

PROCEDURE gdb_exception
    switch mode to ABT
    START gdb_exception_handler
ENDPROC

PROCESS gdb_exception_handler
    check which exception type
    copy processor context from exception register storage to
        debugger register storage
    CALL gdb_monitor
    IF rpi_stub_keep_ctrlc THEN
        IF rpi_stub_interrupt is 'fiq' THEN
            try to force-enable UART0 FIQ
        ELSE
            try to force-enable UART0 IRQ
        ENDIF
    ENDIF
    load registers from debugger storage
    return from exception
ENDPROCESS

PROCEDURE serial_interrupt_handler
    IF data to send in queue THEN
        send data from queue
    ENDIF

    IF data to receive THEN
        receive data to queue
        IF ctrl-C encountered THEN
            IF ctrl-C handling enabled
                set ctrl_C flag
                discard ctrl-C character
            ENDIF
        ENDIF
    ENDIF
ENDPROC

PROCEDURE gdb_monitor
    IF rpi_stub_interrupt is not 'poll' THEN
        enable UART0 interrupt
    ENDIF
    handle debug exception (all except reset, serial tx and serial rx)
    set stub_running to TRUE
    handle pending status (like remove single-stepping breakpoints)
    send pending response to gdb-client (response for s,c,...)
    IF pending status expects continuation THEN
        set stub_running to FALSE
    ENDIF
    disable ctrl-C handling
    WHILE stub_running DO
        get command packet
        IF NACK received THEN
            resend last packet
            continue
        ENDIF
        IF ACK received THEN
            continue
        ENDIF
        IF command packet is good THEN
            send ACK
        ELSE
            send NACK
            continue
        ENDIF
        CALL gdb-stub command_interpreter
        // to resume, set stub_running to FALSE and return from call
    ENDWHILE
    enable ctrl-C handling
ENDPROC


This post has been edited by turboscrew: 19 March 2016 - 07:43 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1