coding new

Upload: neha-thakur

Post on 01-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Coding New

    1/26

    CodingCoding

  • 8/9/2019 Coding New

    2/26

    C O D I N GC O D I N G The coding activity starts when some form of designThe coding activity starts when some form of design

    has been done and the specifications of the modules to behas been done and the specifications of the modules to bedeveloped are available.developed are available.

     Implementation starts either Top-Down or Bottom-upImplementation starts either Top-Down or Bottom-up

     There are certain process that developer does whileThere are certain process that developer does while

    coding i.e.coding i.e.

    An Incremental Coding ProcessAn Incremental Coding Processrite code for assigned module and perform unit testingrite code for assigned module and perform unit testing

    Develop code incrementally to facilitate debuggingDevelop code incrementally to facilitate debugging

    Test driven DevelopmentTest driven Development!ere programmer first writes the test scripts and then writes the code!ere programmer first writes the test scripts and then writes the code

    to pass the test.to pass the test.

  • 8/9/2019 Coding New

    3/26

    Programming Principles andProgramming Principles and

    GuidelinesGuidelinesCommon Coding ErrorsCommon Coding ErrorsMemory LeaksMemory Leaks

    Freeing an already freed resourceFreeing an already freed resource

    NULL DereferencingNULL Dereferencing

    Lack of unique addressLack of unique address

    ync!roni"a#ion Errorsync!roni"a#ion Errors

    $rray inde% ou# of &ound$rray inde% ou# of &ound

    $ri#!me#ic e%cep#ions$ri#!me#ic e%cep#ions

    'u(er o)er*o+'u(er o)er*o+

  • 8/9/2019 Coding New

    4/26

    #ruc#ured Programming#ruc#ured Programming

    elec#ion using if con#rolelec#ion using if con#rolI#era#ion using +!ileI#era#ion using +!ile

    equencingequencing

    Informa#ion ,idingInforma#ion ,idingome programming Prac#icesome programming Prac#icesCon#rol Cons#ruc#sCon#rol Cons#ruc#s

    Go#o-sGo#o-sDa#a #ype .user de/ned0 in &uil#1Da#a #ype .user de/ned0 in &uil#1

    Nes#ingNes#ing

    Module si"e0 in#erfaceModule si"e0 in#erface

    Programming PrinciplesProgramming Principles

    2Guidelines2Guidelines

  • 8/9/2019 Coding New

    5/26

    Coding ProcessCoding Process$n incremen#al coding process$n incremen#al coding process

     3es# dri)en de)elopmen# 3es# dri)en de)elopmen#

    Pair programmingPair programming

    ource code con#rol and &uild &yource code con#rol and &uild &y

    reposi#oryreposi#ory

    4efac#oring .C!ange in in#ernal s#ruc#ure14efac#oring .C!ange in in#ernal s#ruc#ure14educed coupling4educed coupling

    Increased co!esionIncreased co!esion

    'e##er Open Close principle'e##er Open Close principle

  • 8/9/2019 Coding New

    6/26

    C!arac#eris#ics of CodingC!arac#eris#ics of Codingimplici#yimplici#y

    easy #o unders#andeasy #o unders#and4eada&ili#y4eada&ili#y

    simple codesimple code

    Good Documen#a#ionGood Documen#a#ion

    unders#anda&ili#y 2 main#aina&ili#yunders#anda&ili#y 2 main#aina&ili#y 3ranspor#a&ili#y 3ranspor#a&ili#y

    #o di(eren# en)ironmen##o di(eren# en)ironmen#

    Usa&ili#yUsa&ili#y

    good in#erfacegood in#erface

  • 8/9/2019 Coding New

    7/26

    C " D I # $C " D I # $  Programming Practices:Programming Practices:  Concept of %tructured ProgrammingConcept of %tructured Programming  Information !idingInformation !iding  Programming %tyles&Programming %tyles&

     #ames' #ames'

    Control Constructs'Control Constructs'

    $o to'$o to'Info. !iding'Info. !iding'

    (ser Defined Data Types '(ser Defined Data Types '

     #esting' #esting'

      )odule %i*e')odule %i*e')odule Interface')odule Interface'

    Program +ayout'Program +ayout'

    ,obust ness.,obust ness.  Internal DocumentationInternal Documentation

  • 8/9/2019 Coding New

    8/26

    C O D I N GC O D I N G Internal DocumentationInternal Documentation

     Internal documentation of the Program is done by use of comments.Internal documentation of the Program is done by use of comments. Comments are the tetual statements that are meant for the program reader not forComments are the tetual statements that are meant for the program reader not for

    the eecution of the Program.the eecution of the Program.

     Comments if properly written and ept consistent with the code' can be invaluableComments if properly written and ept consistent with the code' can be invaluable

    during maintenance.during maintenance.

     Comment is not needed for every +"C /sComment is not needed for every +"C /s

     Comments should be provided for the bloc of code especially for the bloc whichComments should be provided for the bloc of code especially for the bloc which

    is hard to follow.is hard to follow.

     C"))0#T% 1", A )"D(+0 A,0 "1T0# CA++0D A%C"))0#T% 1", A )"D(+0 A,0 "1T0# CA++0D A% PROLOGUE.PROLOGUE.

     Prologue contains following desirable properties&Prologue contains following desirable properties&

    )odule 1unctionality)odule 1unctionality

    Parameter and their purposesParameter and their purposes

    Assumptions about the input/sAssumptions about the input/s

    $lobal variable accessed and2or modified module.$lobal variable accessed and2or modified module.

     Prologues are useful only if they are ept consistent with the logic of the module.Prologues are useful only if they are ept consistent with the logic of the module.

    If module is modified then P,"+"$(0 should also be modified.If module is modified then P,"+"$(0 should also be modified.

  • 8/9/2019 Coding New

    9/26

    C O D I N GC O D I N G 3erification3erification

      3erification of the output of Coding Phase is primarily3erification of the output of Coding Phase is primarilyintended for detecting the errors.intended for detecting the errors.

     $oal of verification to see the consistency of the code.$oal of verification to see the consistency of the code.

     Two methods for the verification of the Program&Two methods for the verification of the Program&  %tatic )ethod%tatic )ethod

     Dynamic )ethodDynamic )ethod

     Dynamic methods are applicable on some test data and findDynamic methods are applicable on some test data and findout some errors lie traditional testing mechanism.out some errors lie traditional testing mechanism.

      %tatic methods are conceptual eecution' here program are%tatic methods are conceptual eecution' here program are

    not tested lie dynamic.not tested lie dynamic.  Types of error detected through static method can not beTypes of error detected through static method can not beidentified by dynamic methods.identified by dynamic methods.

     Testing and static methods are complementary of each other.Testing and static methods are complementary of each other.

  • 8/9/2019 Coding New

    10/26

    C O D I N GC O D I N G 3erification3erification

     After writing the code the code is to be verified before it isAfter writing the code the code is to be verified before it is

    used by others.used by others.

     3erification does not mean proving correctness of program.3erification does not mean proving correctness of program.

      3arious techni4ues lie inspection' (nit testing 5 Program3arious techni4ues lie inspection' (nit testing 5 Program

    checing for verification.checing for verification.

     Code InspectionCode Inspection $eneral Approach that can be applied for detecting defects.$eneral Approach that can be applied for detecting defects.

     It is done after successful compilation of code.It is done after successful compilation of code.

     Costly affair Costly affair 

     Basically used to reduce human effort and timeBasically used to reduce human effort and time

     other techni4ue is Code ,eadingother techni4ue is Code ,eading

  • 8/9/2019 Coding New

    11/26

  • 8/9/2019 Coding New

    12/26

    C O D I N GC O D I N G %tatic Analysis%tatic Analysis

      It is basically a Program Checing and can be doneIt is basically a Program Checing and can be donein 6 waysin 6 ways

      )odel Checing)odel Checing  !ere abstract model of Program will be verified!ere abstract model of Program will be verified

     Dynamic AnalysisDynamic Analysis  !ere Program is Instrumented and checed with some data!ere Program is Instrumented and checed with some data

      %tatic Analysis%tatic Analysis  Analysis of the Program by methodically analy*ing the Program tet isAnalysis of the Program by methodically analy*ing the Program tet is

    called %Acalled %A

      It is performed mechanically by using tools.It is performed mechanically by using tools.

     During %tatic Analysis the Program itself is not eecuted but the tet isDuring %tatic Analysis the Program itself is not eecuted but the tet is

    input to toolinput to tool

      Aim is to generate the potential errors in the code.Aim is to generate the potential errors in the code.

     1or this purpose we use %tatic Analy*er 1or this purpose we use %tatic Analy*er 

  • 8/9/2019 Coding New

    13/26

    Code 5alk#!roug!sCode 5alk#!roug!s

    I# is informal code analysis #ec!niqueI# is informal code analysis #ec!niqueE)alua#ed &y #eam of re)ie+ersE)alua#ed &y #eam of re)ie+ers

    I# is no# pro6ec# re)ie+ &u# is dep#!I# is no# pro6ec# re)ie+ &u# is dep#!

    e%amina#ion7e%amina#ion7

     3eam for i# includes Pro6ec# Leader0 3eam 3eam for i# includes Pro6ec# Leader0 3eam

    mem&ers and represen#a#i)e form 8$mem&ers and represen#a#i)e form 8$

    group7group7

    Discussion on disco)ery of errors no# on !o+Discussion on disco)ery of errors no# on !o+#o /% #!e errors7#o /% #!e errors7

    5alk#!roug! impro)es 3eam communica#ion5alk#!roug! impro)es 3eam communica#ion

  • 8/9/2019 Coding New

    14/26

    Pro)ing Correc#nessPro)ing Correc#ness

    $ny proof #ec!nique mus# &egin +i#! a formal$ny proof #ec!nique mus# &egin +i#! a formalspeci/ca#ion of #!e program7 No formal proof canspeci/ca#ion of #!e program7 No formal proof can

    &e pro)ided if +!a# +e !a)e #o pro)e is no# s#a#ed&e pro)ided if +!a# +e !a)e #o pro)e is no# s#a#ed

    informally in an imprecise manner7informally in an imprecise manner7

    $ program usually does no# opera#e on ar&i#rary se#$ program usually does no# opera#e on ar&i#rary se#

    of inpu#7 ,ence i# is no# su9cien# #o s#a#e #!e goalof inpu#7 ,ence i# is no# su9cien# #o s#a#e #!e goal

    of program7of program7

     3!e asser#ion a&ou# #!e e%pec#ed /nal s#a#e of a 3!e asser#ion a&ou# #!e e%pec#ed /nal s#a#e of a

    program is called #!e pos# condi#ion of #!e program0program is called #!e pos# condi#ion of #!e program0

    and #!e asser#ion a&ou# #!e inpu# condi#ion is calledand #!e asser#ion a&ou# #!e inpu# condi#ion is called#!e pre:condi#ion of #!e program7#!e pre:condi#ion of #!e program7

    Of#en de/ning #!e pre condi#ion for +!ic! #!e pos#Of#en de/ning #!e pre condi#ion for +!ic! #!e pos#

    condi#ion +ill &e sa#is/ed is #!e goal of proof7condi#ion +ill &e sa#is/ed is #!e goal of proof7

  • 8/9/2019 Coding New

    15/26

     3!e a%ioma#ic approac! 3!e a%ioma#ic approac!

    $ll #!e proper#ies of a program can &e de#ermined$ll #!e proper#ies of a program can &e de#ermineds#a#ically from #!e #e%# of #!e program0 +i#!ou#s#a#ically from #!e #e%# of #!e program0 +i#!ou#

    ac#ually e%ecu#ing i#7ac#ually e%ecu#ing i#7

    In a%ioma#ic model of ,oar ;0 #!e goal is #oIn a%ioma#ic model of ,oar ;0 #!e goal is #o

    #ake #!e program and cons#ruc# a sequence of#ake #!e program and cons#ruc# a sequence ofasser#ions and #!e rules and #!e a%ioms a&ou#asser#ions and #!e rules and #!e a%ioms a&ou#

    #!e s#a#emen# and opera#ions in a program7#!e s#a#emen# and opera#ions in a program7

    Using ,oar no#a#ion0 #!e &asic asser#ion a&ou# aUsing ,oar no#a#ion0 #!e &asic asser#ion a&ou# a

    program is of #!e formprogram is of #!e form

    P{S}QP{S}Q

  • 8/9/2019 Coding New

    16/26

     3!e a%ioma#ic approac! 3!e a%ioma#ic approac!

    $%iom of $ssignmen#$%iom of $ssignmen#

    4ule of composi#ion4ule of composi#ion

    4ule for $l#erna#e s#a#emen#4ule for $l#erna#e s#a#emen#4ule of consequence4ule of consequence

    4ule of i#era#ion4ule of i#era#ion

  • 8/9/2019 Coding New

    17/26

    ,oare,oare’’s me#!od for pro)ings me#!od for pro)ing

    correc#ness of programscorrec#ness of programs,oare sugges#ed in ?@=@ an inference,oare sugges#ed in ?@=@ an inference

    sys#em +i#! +!ic! +e can pro)e proper#iessys#em +i#! +!ic! +e can pro)e proper#ies

    of programs7of programs7

    $ unique form of proof rules0 called ,oare$ unique form of proof rules0 called ,oare#riplesA#riplesA

    P B8 4P B8 4

    +!ere+!ereP is a precondi#ionP is a precondi#ion

    8 is a program or a program s#a#emen#8 is a program or a program s#a#emen#

    4 pos# condi#ion74 pos# condi#ion7

  • 8/9/2019 Coding New

    18/26

    $ssignmen#s$ssignmen#s

    D $%iom of assignmen#sD $%iom of assignmen#s

    PP {x = f}{x = f} 44

    +!ere+!ere

    P is deri)ed from 4 &y replacing allP is deri)ed from 4 &y replacing all

    occurrences of % +i#! f7occurrences of % +i#! f7

    E%ampleAE%ampleA

    y ? ?y ? ? {y = y+1}{y = y+1} y ?y ?

  • 8/9/2019 Coding New

    19/26

  • 8/9/2019 Coding New

    20/26

    Composi#ionComposi#ion

    DJDJ Inference rule for composi#ionInference rule for composi#ion

    PP {Q1}{Q1} 4?4?   4?4? {Q2}{Q2} 44

    PP {Q1; Q2}{Q1; Q2} 44

    E%ampleAE%ampleA

    %?%? {y = x}{y = x} y?y? y?y? {z=y}{z=y} "?"?

    %?%?{y=x; z=y}{y=x; z=y} "?"?

  • 8/9/2019 Coding New

    21/26

    I#era#ionI#era#ionConsider #!e s#a#emen#A +!ile ' do Consider #!e s#a#emen#A +!ile ' do

    DK Inference rule for i#era#ionDK Inference rule for i#era#ion  PP '' {S}{S} PP

      PP {while B do S}{while B do S} AA''  PP

    E%ampleE%ample

    % ?% ? % % {x = x+1}{x = x+1} % ?% ?

    %?%? {while (x < 5) x = x + 1}{while (x < 5) x = x + 1} %%    % ?% ?

  • 8/9/2019 Coding New

    22/26

    if:#!en:elseif:#!en:else

    D if:#!en:else inference ruleD if:#!en:else inference rule

    PP  ee {Q1}{Q1} 44  PP  ee {Q2}{Q2} 44

      PP {if e then Q1 ele Q2 !}{if e then Q1 ele Q2 !} 44

    E%ampleE%ample

    " "  % % {y "= #x + z}{y "= #x + z} yy   " "  %%   {y "= x + z}{y "= x + z} yy  

     " " {if (x

  • 8/9/2019 Coding New

    23/26

    No#es on ,oare:s#yle rulesNo#es on ,oare:s#yle rules

    Pro)esPro)es par#ial correc#nesspar#ial correc#nessi7e70 meaningless +!en #!e programi7e70 meaningless +!en #!e program

    does no# #ermina#e7does no# #ermina#e7

    If in addi#ion +e pro)e #ermina#ion0If in addi#ion +e pro)e #ermina#ion0

    #!en +e !a)e#!en +e !a)e #o#al correc#ness#o#al correc#ness77

    8A suppose #!a# y in our8A suppose #!a# y in our

    program7 Does i# c!ange #!e proof Qprogram7 Does i# c!ange #!e proof Q

  • 8/9/2019 Coding New

    24/26

    No#es on ,oare:s#yle rulesNo#es on ,oare:s#yle rules

    ,oare logic is,oare logic is soundsound in #!e sense #!a#in #!e sense #!a#

    e)ery#!ing #!a# can &e pro)ed ise)ery#!ing #!a# can &e pro)ed is

    correc#correc#

     3!ere e%is#s e%#ensions #o all popular 3!ere e%is#s e%#ensions #o all popular

    cons#ruc#sAcons#ruc#sA

    recursionrecursionarrays0 poin#ers0777arrays0 poin#ers0777

    concurrency777concurrency777

  • 8/9/2019 Coding New

    25/26

    JJ

    Formal $nalysisFormal $nalysis

    4efers #o #ool:&ased me#!ods used #o4efers #o #ool:&ased me#!ods used #o

    e%plore0 de&ug0 and )erify formale%plore0 de&ug0 and )erify formal

    speci/ca#ionsspeci/ca#ions

    Me#!odsMe#!ods 3!eorem pro)ing 3!eorem pro)ing

    Proof c!eckingProof c!ecking

    Model c!eckingModel c!ecking

    $nima#ion and simula#ion$nima#ion and simula#ion

  • 8/9/2019 Coding New

    26/26

     3!ank Rou 3!ank Rou