I use DMD 1.056 with Tango 0.99.9 to build a GPX document using API. I am a beginner in D language.Usage DMD 1.056 with Tango 0.99.9 is compulsory as a requirementThe GPX data are here hardcoded but my intent is to write a more high level GPX builder code using appropriate API.The piece of code to refactor:module SwathGen;import   tango.io.Stdout,  tango.text.xml.Document,  tango.text.xml.DocPrinter;void main(char[][] args){       auto gpxdoc = new Document!(char);    gpxdoc.header;    gpxdoc.tree         .element(null,gpx)      .attribute (null,xmlns,http://www.topografix.com/GPX/1/1)      .attribute (null,version,1.1)            .attribute (null,creator,SwathGen)      .attribute (null,xmlns:xsi,http://www.w3.org/2001/XMLSchema-instance)      .attribute (null,xsi:schemaLocation,http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.topografix.com/GPX/gpx_style/0/2 http://www.topografix.com/GPX/gpx_style/0/2/gpx_style.xsd http://www.topografix.com/GPX/gpx_overlay/0/3 http://www.topografix.com/GPX/gpx_overlay/0/3/gpx_overlay.xsd)  ;  gpxdoc.elements        .element (null,metadata)      .element(null,name,JobDef.gpx)      .parent      .element(null,desc,Spray Job)      .parent      .element(null,author)        .element (null,name,izylay)        .parent        .element (null,email)          .attribute (null,id,izylay)          .attribute (null,domain,ary.com)        .parent      .parent      .element(null,copyright)        .attribute (null,author,izylay)        .element (null,year,2011)        .parent      .parent      .element(null,time,2011-10-10T08:19:50Z)      .parent      .element(null,keywords,ULM, J300, Aerial Spraying, Locust)      .parent            .element (null,bounds)        .attribute (null,minlat,-18.85522622)                .attribute (null,minlon,47.37275913)                .attribute (null,maxlat,-18.82044444)                .attribute (null,maxlon,47.39838002)    ;    gpxdoc.elements             .element(null,wpt)      .attribute (null,lat,-18.85522622)      .attribute (null,lon,47.39173757)      .element (null,name,A000)      .parent      .element (null,sym,Waypoint)  ;    gpxdoc.elements             .element(null,rte)      .element (null,name,Spray Job)      //      .parent      .element(null,rtept)        .attribute (null,lat,-18.85522610)        .attribute (null,lon,47.39838002)        .element (null,name,Entry point)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85522525)        .attribute (null,lon,47.37275913)        .element (null,name,B000)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85387012)        .attribute (null,lon,47.37275913)        .element (null,name,B001)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85387109)        .attribute (null,lon,47.39173757)        .element (null,name,A001)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85251596)        .attribute (null,lon,47.39173757)        .element (null,name,A002)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85251499)        .attribute (null,lon,47.37275913)        .element (null,name,B002)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85115986)        .attribute (null,lon,47.37275913)        .element (null,name,B003)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.85116082)        .attribute (null,lon,47.39173757)        .element (null,name,A003)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84980569)        .attribute (null,lon,47.39173757)        .element (null,name,A004)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84980472)        .attribute (null,lon,47.37275913)        .element (null,name,B004)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84844959)        .attribute (null,lon,47.37275913)        .element (null,name,B005)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84845056)        .attribute (null,lon,47.39173757)        .element (null,name,A005)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84709543)        .attribute (null,lon,47.39173757)        .element (null,name,A006)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84709446)        .attribute (null,lon,47.37275913)        .element (null,name,B006)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84573933)        .attribute (null,lon,47.37275913)        .element (null,name,B007)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84574029)        .attribute (null,lon,47.39173757)        .element (null,name,A007)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84438516)        .attribute (null,lon,47.39173757)        .element (null,name,A008)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84438419)        .attribute (null,lon,47.37275913)        .element (null,name,B008)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84302906)        .attribute (null,lon,47.37275913)        .element (null,name,B009)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84303003)        .attribute (null,lon,47.39173757)        .element (null,name,A009)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84167489)        .attribute (null,lon,47.39173757)        .element (null,name,A010)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84167393)        .attribute (null,lon,47.37275913)        .element (null,name,B010)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84031879)        .attribute (null,lon,47.37275913)        .element (null,name,B011)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.84031976)        .attribute (null,lon,47.39173757)        .element (null,name,A011)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83896463)        .attribute (null,lon,47.39173757)        .element (null,name,A012)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83896366)        .attribute (null,lon,47.37275913)        .element (null,name,B012)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83760852)        .attribute (null,lon,47.37275913)        .element (null,name,B013)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83760949)        .attribute (null,lon,47.39173757)        .element (null,name,A013)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83625436)        .attribute (null,lon,47.39173757)        .element (null,name,A014)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83625339)        .attribute (null,lon,47.37275913)        .element (null,name,B014)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83489825)        .attribute (null,lon,47.37275913)        .element (null,name,B015)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83489922)        .attribute (null,lon,47.39173757)        .element (null,name,A015)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83354409)        .attribute (null,lon,47.39173757)        .element (null,name,A016)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83354312)        .attribute (null,lon,47.37275913)        .element (null,name,B016)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83218798)        .attribute (null,lon,47.37275913)        .element (null,name,B017)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83218895)        .attribute (null,lon,47.39173757)        .element (null,name,A017)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83083382)        .attribute (null,lon,47.39173757)        .element (null,name,A018)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.83083285)        .attribute (null,lon,47.37275913)        .element (null,name,B018)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82947771)        .attribute (null,lon,47.37275913)        .element (null,name,B019)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82947868)        .attribute (null,lon,47.39173757)        .element (null,name,A019)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82812355)        .attribute (null,lon,47.39173757)        .element (null,name,A020)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82812258)        .attribute (null,lon,47.37275913)        .element (null,name,B020)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82676744)        .attribute (null,lon,47.37275913)        .element (null,name,B021)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82676841)        .attribute (null,lon,47.39173757)        .element (null,name,A021)        .parent        .element (null,sym,Dot)        .parent      .parent      .element(null,rtept)        .attribute (null,lat,-18.82044444)        .attribute (null,lon,47.39173757)        .element (null,name,Exit point)        .parent        .element (null,sym,Dot)        .parent    ;    gpxdoc.elements             .element(null,extensions)       .element(null,polyline)        .attribute(null,xmlns,http://www.topografix.com/GPX/gpx_overlay/0/3)        .element(null,points)          //          .element(null,pt)               .attribute (null,lat,-18.85522622)            .attribute (null,lon,47.37275913)          .parent          .element(null,pt)               .attribute (null,lat,-18.85522622)            .attribute (null,lon,47.39838002)          .parent          .element(null,pt)               .attribute (null,lat,-18.82044444)            .attribute (null,lon,47.39838002)          .parent          .element(null,pt)               .attribute (null,lat,-18.82044444)            .attribute (null,lon,47.37275913)          .parent          .element(null,pt)               .attribute (null,lat,-18.85522622)            .attribute (null,lon,47.37275913)          //    ;    auto print = new DocPrinter!(char);    Stdout(print(gpxdoc)).newline;  }Question:How could it be improved ?
How code be this piece of code be refactored?
d
Hm, there is a lot of data in your program. As Cygal points out, you should put this where it belongs - in a data file.Other than that, there are lots of this instructions in your code:  .element(null,rtept)    .attribute (null,lat,-18.82812258)    .attribute (null,lon,47.37275913)    .element (null,name,B020)    .parent    .element (null,sym,Dot)    .parentI would put that in a single function..append(createDot(gpxdoc, -18.82812258, 47.37275913, B020)).parent.append(createDot(gpxdoc, -18.82676744, 47.37275913, B021)).parentSince I've never used this language I leave the implementation of the createDot function to you.Edit: Now I understand your intent a little better. You want to do something like this:auto gpxdoc = new GPXDocument;gpxdoc    .setDesc(Spray Job)    .setAuthor(izylay, izylay@ary.com)    .setCopyright(izylay, 2011)    .setKeywords(ULM, Aerial Spraying, Locust)    .setBounds(-18.85522622, 47.37275913, -18.82044444, 47.39838002)    .addWaypoint(A000, -18.85522622, 47.39173757)    .addEntrypoint(-18.85522610, 47.39838002)    .addPoint(B000, -18.85522525, 47.37275913)    .addPoint(B001, -18.85387012, 47.37275913)You can inherit the Document object and add all these methods just to be wrappers around normal node manipulation.The thing with fluent interfaces is that they may maintain state (that's why you need a parent method, for example.) I'm not familiar with the gpx format, but I wouldn't be surprised if you can create groups of points. For that case, you just need to store a state reference. For the top-level attributes or elements (description, etc.) you can store references to the appropriate node.Hope this points you to the right direction.