[R] Code formatting question - too ugly?

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Jul 22 17:23:43 CEST 2014


On Tue, 22 Jul 2014, John McKown wrote:

> I like to keep the individual lines in my source files relatively
> short. Mainly so that I can print them, email them, or display them on
> a narrow screen without needing to shift left & right. So, for a
> really long character string, such as an SQL query, I do something
> like:
>
> query=paste0("select CONVERT(smalldatetime,Int_Start_Date,11) as
> Int_Start_Date,",
>             " CONVERT(smalldatetime,CASE WHEN Int_Start_Time is NULL
> then '00:00' ",
>             "else
> LEFT(Int_Start_Time,2)+':'+SUBSTRING(Int_Start_Time,3,2) end +",
>             "':00', 14) as Int_Start_Time",
>             ", Int_duration, RTRIM(INTTYPE) AS INTTYPE,"
>             " RTRIM(Int_descr) AS Int_descr",
>             ", RTRIM(INTSUBT) as INTSUBT, "
>             "INDEXX, RTRIM(Label) AS Label",
>             ", RTRIM(CHANGED) AS CHANGED, "
>             "RTRIM(ALERT) AS ALERT, "
>             "RTRIM(RELEASE) AS RELEASE",
>             " FROM CPINTVL where Int_Start_Date BETWEEN '",
>             startDateChar,"' and '",endDateChar,"'"
>             );
>
> So, just as an opinion, is the above "ugly looking" code compared to
> just having a really long line? Also, if you/re curious, when I
> started most programs were literally punched onto 80 column cards or
> displayed on 80 column screens. I still have some damage from this
> time (1970s).

This is out there on the frontier of flame war material, but since your
code is missing a couple of commas that are hard to spot I would say it is 
rather poorly formatted. My formatting style is not exactly compatible 
with most pretty-print styles, but simply recognizing that strings can 
span multiple lines can fix a lot of problems for this particular use 
case:

query <- paste0( "select CONVERT( smalldatetime
                                 , Int_Start_Date
                                 , 11
                                 ) as Int_Start_Dates
                        , CONVERT( smalldatetime
                                 , CASE WHEN Int_Start_Time is NULL
                                        then '00:00'
                                        else LEFT( Int_Start_Time, 2 )
                                           + ':'
                                           + SUBSTRING(Int_Start_Time,3,2)
                                        end
                                   + ':00'
                                 , 14
                                 ) as Int_Start_Time
                        , Int_duration
                        , RTRIM( INTTYPE ) AS INTTYPE
                        , RTRIM( Int_descr ) AS Int_descr
                        , RTRIM( INTSUBT ) as INTSUBT
                        , INDEXX
                        , RTRIM( Label ) AS Label
                        , RTRIM( CHANGED ) AS CHANGED
                        , RTRIM( ALERT ) AS ALERT
                        , RTRIM( RELEASE ) AS RELEASE
                   FROM CPINTVL
                   where Int_Start_Date BETWEEN '"
                , startDateChar
                , "' and '"
                , endDateChar
                ,"'"
                );

By the way... parameterized queries are safer (no SQL injection) and 
faster-executing (well, for repeated use) than pasting input text into 
query text.

Another approach is to store really long strings in data files.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list