Investigating Reason As a Substitute for Javascript
Total Page:16
File Type:pdf, Size:1020Kb
DEGREE PROJECT IN COMPUTER ENGINEERING, FIRST CYCLE, 15 CREDITS STOCKHOLM, SWEDEN 2020 Investigating Reason as a substitute for JavaScript AXEL PETTERSSON KTH ROYAL INSTITUTE OF TECHNOLOGY SCHOOL OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE Investigation of Reason as a substitute for JavaScript AXEL PETTERSSON Degree Programme in Information and Communication Technology Date: June 4, 2020 Supervisor: Thomas Sjöland Examiner: Johan Montelius School of Electrical Engineering and Computer Science Host company: Slagkryssaren AB Swedish title: Undersökning av Reason som ett substitut till JavaScript Investigation of Reason as a substitute for JavaScript / Undersökning av Reason som ett substitut till JavaScript c 2020 Axel Pettersson Abstract | i Abstract JavaScript has in recent years become one of the most utilized programming languages for developing different kinds of applications. However, even though it has received a lot of praise for its simplicity, versatility and highly active community, it lacks some functionalities and features that a lot of programmers highly value, like static and strict typing, compile-time debugging, and to not be required to make use of third-party libraries to integrate crucial functionality. However, several new languages built on top of JavaScript have been developed to address and resolve these issues developers find with JavaScript without losing the benefits that come with it. One of these super- set languages is Reason, the new syntax and toolchain powered by the OCaml compiler. This thesis aims to address whether there are scenarios where Reason could act as a reasonable substitute of JavaScript by investigating how the languages compare in regards to different criteria. The criteria examined are writability, data structures and typing, reliability and testing, community support, market demand, portability, and performance. The findings show that using Reason over JavaScript could result in higher reliability and robustness due to static type checking, compile-time debugging, and other usable feature like pattern matching and explicitly defined custom data structures, which is convenient when dealing with advanced data. On the other hand, Reason’s interoperability with JavaScript is something that is not very straight-forward and makes it difficult to integrate Reason into an existing JavaScript codebase or include one of the thousand of JavaScript- written dependencies available on npm. Furthermore, with Reason being a rather young language yet to be used by a larger audience, both the community support and market demand are a lot smaller than that of JavaScript and have yet to see a significant growth, which leads to questions about the overall survival of the language. Both of the languages have a significant role and contribute with different kind of functionality. However, with the non-straightforward interoperability, Reason loses a lot of potential benefits to be gained from JavaScript, which could be problematic in the long run and could impact the future of Reason. Keywords Programming languages, Computer Science, JavaScript, Comparative Study, Reason ii | Abstract Sammanfattning | iii Sammanfattning JavaScript har under senare år blivit ett av de mest använda programmeringsspråken för att utveckla olika typer av applikationer. Men även om det har fått mycket beröm för sin enkelhet, mångsidighet och mycket aktivt nätgemenskap, saknar det vissa funktioner och egenskaper som många programmerare uppskattar mycket, som statisk och strikt typning, felsökning under kompilering och att inte vara I behov av tredjepartsbibliotek för att integrera grundläggande funktionalitet. Flera nya språk som är byggda ovanpå JavaScript har utvecklats för att lösa de problem som utvecklare har med JavaScript, utan att förlora fördelarna med det. Ett av dessa supersetsspråk är Reason, den nya syntaxen och verktygskedjan som drivs av OCaml-kompilatorn. Denna avhandling syfte är att utvärdera om det finns scenarier där Reason kan fungera som en rimlig ersättare för JavaScript, detta kommer genomföras genom att undersöka hur språken jämförs med varandra med avseende på olika kriterier. Kriterierna som undersöks är skrivbarhet, datastrukturer och typning, tillförlitlighet och testning, nätgemenskap, marknadsförfrågan, portabilitet, och prestanda. Resultaten visar att användning av Reason över JavaScript kan leda till högre tillförlitlighet på grund av statisk typkontroll, felsökning under kompilering och andra användbara funktioner som mönstermatchning och anpassade datastrukturer, båda underlättar vid hantering avancerade och komplexa datastrukturer. Däremot är Reasons kompabilitet med JavaScript komplicerad, vilket gör det svårt att integrera Reason i en befintlig JavaScript- kodbas eller inkludera ett av de tusentals JavaScript-skrivna biblioteken som finns tillgängliga på npm. Eftersom Reason är ett ganska ungt språk som ännu inte används på en större skala, så är både nätgemenskapen och marknadens efterfrågan mycket mindre än JavaScript och har ännu inte sett en betydande tillväxt, vilket leder till frågor om den långsiktiga överlevnaden av språket. Båda språken har en betydande roll och bidrar med olika slags funktioner. Men med den komplicerade kompatibiliteten med JavaScript så förlorar Reason en hel del potentiella fördelar som kan uppnås genom JavaScript, vilket kan vara problematiskt på lång sikt och kan påverka Reasons framtid. Nyckelord Programmeringsspråk, datavetenskap, JavaScript, jämförande studie, Reaasn iv | Sammanfattning Acknowledgments | v Acknowledgments I would like to thank Simone Stefani for introducing me to Slagkryssaren and given me the opportunity to do this thesis. You are a great inspiration, thank you! I would also want to thank David Broman for helping me understand the Reason and always being there for helping me out. Lastly, I would like to thank Thomas Sjöland and Johan Montelius for the great support they have provided throughout this project. Stockholm, June 2020 Axel Pettersson vi | Acknowledgments CONTENTS | vii Contents 1 Introduction1 1.1 Background...........................1 1.2 Problem.............................2 1.3 Purpose.............................3 1.4 Goals..............................3 1.5 Research Methodology.....................3 1.6 Delimiters............................4 1.7 Outline.............................4 2 Background5 2.1 JavaScript............................5 2.2 Reason..............................8 3 Methodology 11 3.1 Research process........................ 12 3.2 Test environment........................ 14 3.3 Validity and reliability..................... 15 4 Results and implementation 17 4.1 Writability............................ 17 4.2 Data structures and typing................... 23 4.3 Reliability and testing...................... 24 4.4 Community support....................... 28 4.5 Market demand......................... 32 4.6 Portability............................ 33 4.7 Performance........................... 35 5 Discussion 39 5.1 Flexibility and paradigms.................... 39 5.2 Data structures and typing................... 40 viii | Contents 5.3 Reliability and testing...................... 40 5.4 Community support and engagement.............. 41 5.5 Job openings and market demand................ 42 5.6 Portability and interoperability................. 43 5.7 Performance........................... 43 6 Conclusion 45 6.1 Why should someone use Reason?............... 45 6.2 Why should not someone use Reason?............. 46 6.3 When should or should not someone use Reason instead of JavaScript?........................... 47 6.4 Final thoughts.......................... 48 6.5 Limitations and future research................. 48 References 51 A Faulty code and output 55 A.I Faulty JavaScript Code..................... 55 A.II Faulty Reason Code....................... 57 B Code for fetching community support data 61 C Code for performance testing 63 C.I JavaScript performance code.................. 63 C.II Reason performance code.................... 64 LIST OF FIGURES | ix List of Figures 4.1 Created GitHub repositories with a minimum of 10 stars... 29 4.2 Created Stack Overflow questions............... 30 4.3 Average Stack Overflow views per questions as of today.... 30 4.4 Average Stack Overflow answers per questions as of today.. 31 4.5 Reason packages........................ 32 4.6 npm packages registry growth................. 33 4.7 Mean time to render user list of 10 runs............ 36 4.8 Mean time to remove user of 10 runs.............. 37 4.9 Mean time to add user in a sorted user list of 10 runs..... 38 x | LIST OF FIGURES LISTINGS | xi Listings 2.1 Usage example of the typeof function.............7 2.2 Type example in Reason....................9 4.1 Example of inheritance in JavaScript.............. 18 4.2 Example of Lambda syntax in JavaScript............ 18 4.3 Example on anonymous and non-anonymous functions.... 19 4.4 Example of class declaration.................. 19 4.5 Example of whitespace handling in JavaScript......... 20 4.6 Example of operators in JavaScript............... 20 4.7 Variable declaration in Reason................. 21 4.8 Example of recursive function declaration in Reason..... 21 4.9 Example of broken function declaration in Reason....... 22 4.10 Example of operators in Reason................ 22 4.11 Example of pattern matching on arrays in Reason....... 22 4.12 Example of pattern matching with variants in Reason..... 24 4.13 Dumping raw JavaScript in Reason............... 34 4.14 JavaScript function