SEG 3550 Fundamentals of Information System - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

SEG 3550 Fundamentals of Information System

Description:

SEG 3550 Tutorial 9. 1. SEG 3550 - Fundamentals of Information System ... Handle IOException and SQLException. But not Exception ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: henry96
Category:

less

Transcript and Presenter's Notes

Title: SEG 3550 Fundamentals of Information System


1
SEG 3550 - Fundamentals of Information System
  • Tutorial 9 Programming Style and Demo
  • By Henry P.S. Hui

2
Outline
  • Programming Style
  • Demo
  • Java application with JDBC
  • ProC application

3
Programming Style
  • Writing program is far more easy than writing
    good program
  • Definition of Good
  • Easy to follow by other programmers
  • Efficient use of memory
  • Effective code reuse
  • (many others)

4
Both C/C and Java
  • Some techniques that share between C/C, Java
    and also some other languages

5
Boolean and Integer
  • A boolean variable takes the same byte size as
    integer
  • However, boolean only give two output
  • Integer can give many different
  • Conclusion
  • Integer is more informative without any
    additional lost
  • Use int to replace boolean especially as a return
    type
  • Can use to specify the condition of the output
  • Normal / if error, what type of error

6
Understanding different errors
  • Segmentation Fault
  • Illegal access of memory
  • Usually is the problems of pointers or
  • Reference of variables that have no assigned
    value
  • Bus Error
  • Access of physically not exist memory address
  • Pointer contain being alter by other operation

7
Reading the reference type
  • Read in reverse direction
  • char nameList 0
  • An array of pointers which only points to char
  • Array reference is the same as pointers

8
DEBUG Label
  • Define a variable DEBUG
  • Put all the debug statement into the condition
    score under DEBUG
  • if(DEBUG)
  • Quite and easy way to identify the debug message
  • When the development is completed, simple set it
    to false or 0, then everything gone

9
Writing debug message
  • Common debug messages
  • Die!!!!
  • Help
  • Completely not meaningful
  • Better way
  • ERRORClassName.FunctionName Description
  • Descrption should describe what have been done
    therefore the code get into this error message

10
Static or not static?
  • Static member
  • Share among class
  • Can be access by all objects of the class
  • Non-static member
  • Share within the instance of object created
  • If the member representing the same thing for all
    object of the class ? Static
  • Caution! Can be changed by different objects

11
Function Call
  • Each function call will need to create a new
    stack for the execution
  • Type of stacks
  • Control stack
  • Execution stack
  • Function stack
  • Run-time stack
  • Recursive function is not efficient
  • Recursively creating Activation Records

12
Activation Records
  • Contain stack pointers, frame pointers
  • Problem is it will hold up a bigger and bigger
    memory size in the system

AR1
AR 2
AR 3
AR 4
13
Structure of the program
  • Some of the action in the program is critical
  • E.g. Action that changes the database
  • Use a single entry scheme
  • All the function that do such action will call a
    single function
  • This function need to be very generic
  • Can be used to log the action items

14
Java
  • Starting from import
  • Dont import the entire package
  • Unless you have used more than three classes
  • This can reduce the preloaded memory

15
StringBuffer
  • String sql select from table namelist where
    id id and name name
  • 5 strings are actually created in the memory for
    this line of code
  • Have to wait the garbage collector to collect the
    unused memory
  • Using StringBuffer can reduce this problem
  • More trouble but more efficient in memory
    utilization

16
E.g. of using StringBuffer
  • StringBuffer sql_sb new StringBuffer()
  • sql_sb.append(select from table namelist where
    id )
  • sql_sb.append(id)
  • sql_sb.append( and name )
  • sql_sb.append(name)
  • sql_sb.append()
  • String sql sql_sb.toString()

17
Exception Handling
  • Handle specify exception
  • Handle IOException and SQLException
  • But not Exception
  • Catching specify exception give more information
    on the error
  • Can use a catch all at the outer scope to prevent
    the system to exit without attention or log

18
Try-Catch-Finally
  • Try-catch block is commonly used for handle
    exception, but the finally block
  • Finally block will be executed whenever the code
    of the try block is executed
  • Useful for clear up the instances created in the
    try block
  • E.g. Stream opened in try block and error in the
    middle, use finally block to close it

19
Static initialization Block
  • Any block with the label static is a static
    initialization block
  • A java program without main(String arg)
  • public class DemoClass
  • static
  • System.out.println("hi")
  • System.exit(0)

20
IO in C
  • Use Exclusively the C library instead of C
    C
  • printf or iostreamObjectcout ltlt
  • The application will be more efficient

21
Variable definition
  • Set a value to a variable is declared
  • Java will assign a null to it by default but
    not in C/C
  • If we do not assign value to the variable when we
    declare it, problem will exist, e.g.
  • char testString
  • printf(s\n, testString)

Random outputMay have segmentation fault
22
Demo
  • The code is just to show you what have been
    taught in the previous tutorials works
  • The style of the code is not considered in the
    example demo code
Write a Comment
User Comments (0)
About PowerShow.com