• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Life is Good.

            Enhance Tech and English
            隨筆 - 65, 文章 - 20, 評(píng)論 - 21, 引用 - 0
            數(shù)據(jù)加載中……

            Sorting out behavior of "IF / ELSE"

            Sorting out behavior of “IF / ELSE “

             

            The “ELSE” part of an IF statement is assumed in Lisp. So, if not careful, you may get unexpected behavior.

             

            Here is a recent change (in blue/gray highlight) someone pushed up to Perforce for one of the  lisp files:

               (setq x nil)

              (setq cnt 0)

              (while (< cnt (length ss))

                (if (nth cnt ss)

                  (setq en (handent (nth cnt ss)))

                  (if en

                    (progn

                      (setq x value1)

                      (if (not x)

                        (setq x value2)

                      )

                    )

                  )

                )

             

            This looks pretty benign… but it will likely cause a regression.

             

            To better see this problem, here is the same code with an “ELSE” comment inserted where end of the “True” part of the IF statement ends and the assumed “else” part of the if statement begins:

             

               (setq x nil)

              (setq cnt 0)

              (while (< cnt (length ss))

                (if (nth cnt ss)

                  (setq en (handent (nth cnt ss)))

                             ; ELSE

                  (if en

                    (progn

                      (setq x value1)

                      (if (not x)

                        (setq x value2)

                      )

                    )

                  )

                )

             

            So, here’s what will happen. The IF statement checks (nth cnt ss) to see if it is non-nil. If it is non-nil, then it fills variable “en” with the converted handle value in (nth cnt ss). Then it exits the IF statement. But, if (nth cnt ss) is nil, then a second IF statement checks undefined / uninitialized variable “en” and, if it happens to be non-nil, tries to do something with it. Likely it will do something not expected or desired.

             

            Grouping lines of code in an IF statement

             

            A better way to set up this IF/ELSE statement is probably something like this:

             

              (setq x nil)

              (setq cnt 0)

              (while (< cnt (length ss))

                (if (nth cnt ss)

                  (progn

                    (setq en (handent (nth cnt ss)))

                    (if en

                      (progn

                        (setq x value1)

                        (if (not x)

                          (setq x value2)

                        )

                      )

                    )

                  )

                )

             

            The beginning “(progn” and ending “)” to balance it groups all of the in-between lines of code to be executed for the “True” part of the IF statement. This is probably what needs to happen here.

             

            So, two things to watch for which may help avoid creating new regressions:

            1.       Go ahead and show the “; ELSE” comment in an IF statement when both conditions are being coded up.

            2.       Use the “(progn “ flag to group two or more lines of code together that need to execute for a single condition of the IF statement

            posted on 2008-08-08 13:46 Mike Song 閱讀(108) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            色欲综合久久中文字幕网| 欧美777精品久久久久网| 一本一本久久a久久精品综合麻豆| 久久有码中文字幕| 国产精品久久久久久久久免费| 精品精品国产自在久久高清| 狠狠狠色丁香婷婷综合久久五月| 亚洲欧美另类日本久久国产真实乱对白 | 久久久久国产精品三级网| 理论片午午伦夜理片久久| 人妻精品久久无码区| 久久久久99精品成人片牛牛影视| 亚洲AV无码久久| 久久久久亚洲av成人无码电影| 亚洲中文精品久久久久久不卡| 国产精品激情综合久久| 亚洲AV无码久久| 久久青青草原精品国产软件 | 中文字幕精品久久久久人妻| 国产精品视频久久| 精品久久人人爽天天玩人人妻| 久久国产午夜精品一区二区三区| 久久免费视频观看| 久久久久亚洲AV片无码下载蜜桃 | 中文精品99久久国产 | 色妞色综合久久夜夜| 久久国产精品无码网站| 天天综合久久久网| 国产精品99久久精品| 久久亚洲春色中文字幕久久久| 久久99九九国产免费看小说| 欧美麻豆久久久久久中文| 久久久久亚洲AV成人网人人网站| 国内精品久久久久影院网站 | 午夜精品久久久久久影视777| 欧美激情精品久久久久久久| 久久性生大片免费观看性| 久久青青草原精品国产软件| 亚洲国产成人精品女人久久久 | 中文字幕精品无码久久久久久3D日动漫 | 久久人与动人物a级毛片|