Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Do me a favor, would you? Have a quick look at this code and tell me what you think it does...

MysteryProcedureUsingCodeBlock  (long StudentID)!,long ! Declare Procedure


FilesOpened     BYTE(0)
Result                                              long
StepNumber                                          long
MostAvoidedTeacher                                  cstring(100)
CourseTakenQ                                        Queue
CourseID                                                long
TeacherID                                               long
                                                    end
CourseNotTakenQ                                     Queue
CourseID                                                long
TeacherID                                               long
                                                    end
CourseQ                                             Queue
CourseID                                                long
Taken                                                   BOOL
                                                    end
TeacherQ                                            Queue
TeacherID                                               Long
ClassesTaught                                           Long
                                                    end
x                                                   long
WasTaken                                            bool

    CODE
    do OpenFiles
    STU:StudentID = StudentID
    get(Student,STU:pkStudentID)
    if errorcode()
        do CloseFiles
        return FALSE
    end
    set(Course)
    loop
        next(Course)
        if errorcode() then break.
        clear(CRSI:Record)
        CRSI:CourseID = CRS:CourseID
        set(CRSI:kCourseIDTeacherID,CRSI:kCourseIDTeacherID)
        WasTaken = false
        loop 
            next(CourseInstance)
            if errorcode() or CRSI:CourseID <> CRS:CourseID then break.
            ENR:StudentID = StudentID
            ENR:CourseInstanceID = crsi:CourseInstanceID
            get(Enrollment,ENR:kCourseInstanceUDStudentID)
            if not errorcode()
                WasTaken = TRUE
                break
            end
        end
        if WasTaken
            clear(CourseTakenQ)
            CourseTakenQ.CourseID = CRS:CourseID
            CourseTakenQ.TeacherID = CRSI:TeacherID
            add(CourseTakenQ)
        else
            clear(CourseNotTakenQ)
            CourseNotTakenQ.CourseID = CRS:CourseID
            CourseNotTakenQ.TeacherID = CRSI:TeacherID
            add(CourseNotTakenQ)           
        end
    end
    set(Teacher)
    loop
        next(Teacher)
        if errorcode() then break.
        TeacherQ.TeacherID = TEA:TeacherID
        add(TeacherQ)
    end
    loop x = 1 to records(CourseTakenQ)
        get(CourseTakenQ,x)
        TeacherQ.TeacherID = CourseTakenQ.TeacherID
        get(TeacherQ,TeacherQ.TeacherID)
        if not errorcode() ! should always find a record
            TeacherQ.ClassesTaught += 1
            put(TeacherQ)
        end
    end
    sort(TeacherQ,-TeacherQ.ClassesTaught)
    loop while not errorcode()
        get(TeacherQ,6)
        delete(TeacherQ)
    end
    x = 1
    loop 
        get(CourseNotTakenQ,x)
        if errorcode() then break.
        TeacherQ.TeacherID = CourseNotTakenQ.TeacherID
        get(TeacherQ,TeacherQ.TeacherID)
        if not errorcode()
            x += 1
        else
            delete(CourseNotTakenQ)
        end
    end
    TeacherQ.TeacherID = CourseNotTakenQ.TeacherID
    get(TeacherQ,TeacherQ.TeacherID)
    get(CourseNotTakenQ,random(1,records(CourseNotTakenQ)))
    do CloseFiles
    return false  ! because this code is not yet complete

How are you making out? Do you have a clear idea of what this code does?

If you study the code long enough you should be able to figure out what it does, but you probably still won't know why it's doing what it does.

Now consider this version of the same code (I've omitted the OpenFiles/CloseFiles routines in both cases):

MysteryProcedureUsingLocalProcedures  (long StudentID)!,long ! Declare Procedure

                                        map
                                            GetStudentRecord(),long
                                            BuildListsOfCoursesTakenAndNotTaken(),long
                                            CourseWasTaken(),long
                                            BuildListOfTeachers(),long
                                            CountCoursesTaughtToStudent(),long
                                            SortTeacherListByCountOfCoursesTaughtToStudent(),long
                                            NarrowTeacherListToFiveMostAvoided(),long
                                            NarrowCourseNotTakenListToThoseTaughtByFiveMostAvoidedTeachers(),long
                                            CourseIsTaughtByOneOfFiveMostAvoidedTeachers(),long
                                            RegisterStudentForRandomlyChosenCourse(),long
                                        end
FilesOpened                                         BYTE(0)
Result                                              long
StepNumber                                          long
MostAvoidedTeacher                                  cstring(100)
CourseTakenQ                                        Queue
CourseID                                                long
TeacherID                                               long
                                                    end
CourseNotTakenQ                                     Queue
CourseID                                                long
TeacherID                                               long
                                                    end
CourseQ                                             Queue
CourseID                                                long
Taken                                                   BOOL
                                                    end
TeacherQ                                            Queue
TeacherID                                               Long
ClassesTaught                                           Long
                                                    end

    CODE
    do OpenFiles
    StepNumber = 0
    Result = Level:Benign
    Loop while Result = Level:Benign
        StepNumber += 1
        execute StepNumber
            Result = GetStudentRecord()
            Result = BuildListsOfCoursesTakenAndNotTaken()
            Result = BuildListOfTeachers()
            Result = CountCoursesTaughtToStudent()
            Result = SortTeacherListByCountOfCoursesTaughtToStudent()
            Result = NarrowTeacherListToFiveMostAvoided()
            Result = NarrowCourseNotTakenListToThoseTaughtByFiveMostAvoidedTeachers()
            Result = RegisterStudentForRandomlyChosenCourse()
            break
        end
    end
    do CloseFiles
    Return result
    
GetStudentRecord                        procedure()!,long
    code
    STU:StudentID = StudentID
    get(Student,STU:pkStudentID)
    if not errorcode() then return level:benign.
    return level:notify
    
BuildListsOfCoursesTakenAndNotTaken     procedure()!,long
    code
    set(Course)
    loop
        next(Course)
        if errorcode() then break.
        if CourseWasTaken() = Level:Benign
            clear(CourseTakenQ)
            CourseTakenQ.CourseID = CRS:CourseID
            CourseTakenQ.TeacherID = CRSI:TeacherID
            add(CourseTakenQ)
        else
            clear(CourseNotTakenQ)
            CourseNotTakenQ.CourseID = CRS:CourseID
            CourseNotTakenQ.TeacherID = CRSI:TeacherID
            add(CourseNotTakenQ)           
        end
    end
    return Level:Benign
    
CourseWasTaken                          procedure()!,long
WasTaken                                    bool
    code
    clear(CRSI:Record)
    CRSI:CourseID = CRS:CourseID
    set(CRSI:kCourseIDTeacherID,CRSI:kCourseIDTeacherID)
    loop 
        next(CourseInstance)
        if errorcode() or CRSI:CourseID <> CRS:CourseID then break.
        ENR:StudentID = StudentID
        ENR:CourseInstanceID = crsi:CourseInstanceID
        get(Enrollment,ENR:kCourseInstanceUDStudentID)
        if not errorcode()
            WasTaken = TRUE
            break
        end
    end
    if WasTaken then return level:benign.
    return level:notify
    
BuildListOfTeachers                     procedure()!,long
    code
    set(Teacher)
    loop
        next(Teacher)
        if errorcode() then break.
        TeacherQ.TeacherID = TEA:TeacherID
        add(TeacherQ)
    end
    return Level:Benign
            
CountCoursesTaughtToStudent             procedure()!,long
x                                           long
    code
    loop x = 1 to records(CourseTakenQ)
        get(CourseTakenQ,x)
        TeacherQ.TeacherID = CourseTakenQ.TeacherID
        get(TeacherQ,TeacherQ.TeacherID)
        if not errorcode() ! should always find a record
            TeacherQ.ClassesTaught += 1
            put(TeacherQ)
        end
    end
    return Level:Benign
SortTeacherListByCountOfCoursesTaughtToStudent  procedure()!,long
    code
    sort(TeacherQ,-TeacherQ.ClassesTaught)
    return Level:Benign
    
NarrowTeacherListToFiveMostAvoided      procedure()!,long
x                                           Long
    code
    loop while not errorcode()
        get(TeacherQ,6)
        delete(TeacherQ)
    end
    return Level:Benign
NarrowCourseNotTakenListToThoseTaughtByFiveMostAvoidedTeachers  procedure()!,long
x                                                                   long
    code
    x = 1
    loop 
        get(CourseNotTakenQ,x)
        if errorcode() then break.
        if CourseIsTaughtByOneOfFiveMostAvoidedTeachers() = Level:Benign
            x += 1
        else
            delete(CourseNotTakenQ)
        end
    end
    return Level:Benign
    
CourseIsTaughtByOneOfFiveMostAvoidedTeachers    procedure()!,long
    code
    TeacherQ.TeacherID = CourseNotTakenQ.TeacherID
    get(TeacherQ,TeacherQ.TeacherID)
    if not errorcode() then return Level:Benign.
    return Level:Notify
    
    
RegisterStudentForRandomlyChosenCourse  procedure()!,long
    code
    get(CourseNotTakenQ,random(1,records(CourseNotTakenQ)))
    return Level:Notify  ! because this code is not yet complete
        

 

 

 

    do OpenFiles
    StepNumber = 0
    Result = Level:Benign
    Loop while Result = Level:Benign
        StepNumber += 1
        execute StepNumber
            Result = GetStudentRecord()
            Result = BuildListsOfCoursesTakenAndNotTaken()
            Result = BuildListOfTeachers()
            Result = CountCoursesTaughtToStudent()
            Result = SortTeacherListByCountOfCoursesTaughtToStudent()
            Result = NarrowTeacherListToFiveMostAvoided()
            Result = NarrowCourseNotTakenListToThoseTaughtByFiveMostAvoidedTeachers()
            Result = RegisterStudentForRandomlyChosenCourse()
            break
        end
    end
    do CloseFiles
    Return result

 

 

  • No labels