TOWARDS A GREEN PROGRAMMING PARADIGM FOR MOBILE SOFTWARE DEVELOPMENT Selvakumar Samuel Asia Pacific University of Technology and Innovation Technology Park Malaysia 57000 Bukit Jalil, Malaysia. Email: selvakumar@apu.edu.my Dr. Arangasamy Kovalan Periyar Maniammai University Vallam, Thanjavur 613403, Tamilnadu, India. Email: kovapmu@gmail.com ABSTRACT This paper evaluates the features of mainstream programming paradigms. Imperative, object oriented programming and functional programming concepts are considered here. This is an effort to identify the programming paradigms which consume less resource from mobile devices. In general any programming language is a collection of API s which is mainly based on a core programming paradigm and a few other programming paradigms, for example Java programming language is based on Object oriented programming paradigm as a core and also integrated other imperative programming paradigms concepts. Designers usually depend on the programming languages, language oriented programming design is in current practice. Choosing appropriate programming paradigms during the mobile application design is not in practice now; Failure to use best approaches for mobile computing from programming paradigms will cause mobile applications to consume more mobile resources. Imperative paradigm concepts such as inheritance, creating redundant objects, unnecessary constructors, recursion, strings concatenation, thread synchronization, using global variables and abstract methods results in redundancy, memory leaks, stack overflow, low execution speed and consumes more memory. These features are relatively not suitable for mobile software development. Functional paradigm concepts such as higher order functions, tail recursion, lazy evaluation, referential transparency, parametric polymorphism, and list comprehension principles are suitable for mobile software development as they consume less memory and or use less processing power. Many modern programming languages such as Java, Scala, C#, F#, etc., support the fusion of object oriented and functional programming paradigms. Using appropriate paradigms on those programming languages will optimize the resource utilisation of mobile applications. KEYWORDS Green computing, Mobile Computing, Mobile Application Development, Programming Paradigms and Functional paradigm. 1. INTRODUCTION Presently, mobile devices are able to do things that are normally done with computers including desktops and laptops except for software development. The advantage of mobile devices is that it consumes much less power compared to computers hence if used for common computing need; it will save energy and contribute towards keeping our planet green. In programming, there are many paradigms that have been suggested and this keeps evolving, in order to suit the requirements of software development of the respective times. A paradigm defines a fundamental approach to design a solution for a given problem. Choosing an optimal paradigm will enhance the accuracy and performance of the solution. Mostly procedural, structural and object oriented features are common when we use languages like Java, C#, C++, Objective C etc. for mobile software development. Inappropriate use of paradigms consuming more resources while running the mobile applications in mobile devices. Programming paradigm plays a major role in optimizing the program design. As we know optimization can occur at a number of levels. Design level optimization will allow the system to make best use of the available resources. The architectural design of a system overwhelmingly affects its performance. The choice of paradigms affects efficiency more than any other item of the design [4]. The following simple JavaMe code Example1 demonstrates the role and dominancy of programming paradigms in a program design. ISBN: 978-0-9853483-8-0 2013 SDIWC 74
Example 1 importjavax.microedition.lcdui.*; importjavax.microedition.midlet.*; public class Example1 extends MIDlet { private Form form; public Example1() { form = new Form("Example1"); form.append(new StringItem(null, "Hello Mobile World")); public void startapp() { Display.getDisplay(this).setCurrent(for m); public void pauseapp() { public void destroyapp(boolean unconditional) { notifydestroyed(); The output of the above 12- lines code is just to display a message Hello mobile world as shown in Figure 1 below. But the usage of programming paradigms is vast, it has most of the object oriented and other programming concepts such as class, objects, encapsulation, packages, inheritance, abstract methods, static, constructors, etc., while it executes it will take more mobile resources which is not worthwhile for the required output. This is the main flaw of the current approaches. Choosing the appropriate paradigms for program design will increase the efficiency, performance and accuracy of the system. This will use relatively less resources. This discussion is applicable for desktop development as well but most required for mobile software development. 2. IMPERATIVE AND OBJECT ORIENTED PROGRAMMING PARADIGMS IN MOBILE SOFTWARE DEVELOPMENT Programming paradigms such as procedural, structural, etc. can be considered as imperative programming paradigms and the programming languages based on those concepts are referred to called imperative programming languages. Imperative and Object Oriented Programming are common when we use any mainstream programming languages for desktop and mobile software development. As per the investigation object oriented programming concepts are dominating mobile software development. Concepts such as multiple inheritance, creating multiple objects are often used in the mobile application design. Other concepts such as abstract methods, recursive procedures, concatenating string, thread synchronization, declaring global variables, etc. are mostly used in mobile application designs. Basically they are not appropriate for mobile software development. Since most of the development tools are based on the imperative programming and object oriented programming paradigms, these concepts are being used by the software developers. 2.1 Evaluation Comments This section briefly evaluates major programming concepts of imperative programming paradigms in order to optimize the resource utilization of mobile devices. Figure 1 Mobile devices have limited resources compared to desktop machines; hence application of the desktop development approaches is not suitable for mobile application development. 2.1.1 Objects The object-oriented system is heavily constrained by the nature of the object sizes. Flushing of the objects by the virtual memory manager must defragment the primary storage before the new object can be stored. It is observed that the speed of the execution of the ISBN: 978-0-9853483-8-0 2013 SDIWC 75
benchmarks in case of procedural programming style is 8% more as compared to the object oriented programming. It is analyzed that this is mainly due to the increased instruction count, larger code size and increased number of accesses to the data memory for the objectoriented versions [7]. A primary way to reduce total memory requirements of your application is to avoid using object types. Instead, use scalar types, which use less memory than object types. Always reuse objects instead of creating new objects. This reduces both memory allocation and the need for processing power. Memory allocation is reduced because multiple references can use the same object at different times in the application s life cycle. Obviously, both objects that use the same memory cannot run simultaneously. The need for processing power is reduced because a portion of the processing is required to allocate new memory doesn t need to be invoked since memory has already been allocated when the object is instantiated. This and similar data management subtleties usually have little or no noticeable impact on a mobile software. However, this kind of attention to detail will have a dramatic impact on the performance of a mobile application. 2.1.2 Constructors, Destructors and Abstract Methods As we discussed in section 2.1.1, if we minimize the creation of more objects by reusing the objects and on the other hand will minimize the use of constructors. The following screen (figure 2) shows the volume of resources used by the Example 1 program just to display a message Hello Mobile World and also it is listed that how many object have been created, classes used and all. This result supports the arguments of this paper. Most of the object oriented languages having destructor or garbage collector mechanisms but you don t know when the garbage collector will collect your garbage. Therefore, it is critical that you clean up after the application finished using memory. Likewise most of the programmers use the approach like Example 1while designing their mobile applications in JavaME. Indirectly it is increasing the constructor usages and number of source code lines. This should be avoided. Same effect can be achieved by providing all the codes in the startapp() abstract method. Figure 2 In addition to this due to the language architecture, most of the built-in classes are abstract. Hence in most cases we have to define the abstract methods unnecessarily like pause() functions in Example 1. Alternative approaches are necessary to avoid this type of flaws. 2.1.3 Inheritance As you can see from Example 1 such a simple program needs to use more resources (see Figure 2) unnecessarily to execute the programs due to the inheritance mechanism. Inheritance is another processing drain that can be avoided by designing an application to eliminate Inheritance or at least reduce multiple inheritance to the minimum necessary to achieve the objective of the application since it increases the application s use of memory in addition to increasing the application s processing requirements. 2.1.4 Recursive Procedures In Imperative And Object Oriented Programming. If possible, recursive procedures should be avoided for mobile software design as it pushes stacks to large sizes. Imperative programming recursive functions uses dynamic memory management (stacks) while tail recursive ISBN: 978-0-9853483-8-0 2013 SDIWC 76
technique in functional programming uses static memory management (i.e. not using stacks); hence tail recursive is better for mobile software development. When applications use a function that calls itself, the function should not be deeply nested. Similarly, copying large objects or passing them by value should be avoided wherever possible in favour of using pointers or passing the objects by reference [9]. 2.1.5 Others Practises Concepts such as concatenating strings, thread synchronization and use of global variables should be formulated. In many applications, developers assign values to data members of a class rather than using a local variable. You can increase processing speed of your application if you eliminate the extra steps of accessing a data member of a class by assigning values to local variables. Concatenation also increases the application s use of memory in addition to increasing the application s processing requirements, which becomes apparent by comparing processing a string with processing a concatenated string. Another way to increase performance is to avoid using synchronization where possible. Synchronization requires additional processing steps that are not necessary when synchronization is deactivated. 3. FUNCTIONAL PROGRAMMING Upon reviewing various programming paradigms, author have identified that most of the functional programming concepts are suitable for mobile software development. In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed [10]. Functional programming avoids states and mutable data. Some of the important functional programming concepts are higher order functions, closures, lazy evaluation, tail recursions, referential transparency, functional composition, parametric polymorphism and pure functions. These concepts are relatively suitable for mobile computing requirements. The paradigm of functional programming is used in languages like Haskell, Lisp, F#, Ruby, Smalltalk, Python, scala etc. and now supported by Java Functional libraries such as FunctionalJ, Functional Java and LamdaJ, Javascript, c, c++, etc. Ericsson has been successfully using a functional language called Erlang for quite a long time for their mobile software projects. Apple starts using in Ojective C for iphone programming. 3.1 Evaluation Comments Functional paradigm generally eliminates the redundancy and uses only required functionalities to perform the task, these features are more suitable for mobile computing requirements. Certain mobile applications may involve computationally intensive algorithms. Functional programs are inherently parallelized, which can help us to implement computationally intensive algorithms in resource constrained mobile applications. Asynchronous and parallel programmings are essential components of a mobile application. Creation of HTTP channels, asynchronous calls, completion routines are common here. Microsoft F# handles this difficulty with a functional programming concept called workflows. 3.1.1 Higher Order Functions For a mobile application development that requires high degree of modularity and scalability, higher order functions would be a good option. A function that takes another function as one of its arguments or returns a function as result is called a higher-order function. Shown below is an example using map (higher order function) and list in Functional Java where a function increments each element of the list to produce a new list. This method has prevented repeated iteration over the data, hence optimizing the speed and size of code. Example 2 import fj.*; import static fj.data.*; import static fj.pre.*; public final class HofList{ public static void main(final String s[]){ final List<Integer>l1 = list(11,21,31); final List<Integer>l2= l1.map(add.f(10)); listshow(intshow).println(l2); ISBN: 978-0-9853483-8-0 2013 SDIWC 77
Using Map function we can write a code as simple as possible to search the entire internet contents with a basic string searcher as a parameter, which means map function can be used to tackle huge problems in an instant. Lots of Google applications use Map and Reduce (a family of higher order functions) and they all benefit whenever someone optimizes it or fixes bugs [2]. Functional programming took a tightly coupled section of code and removed the dependency by use of higher-order functions. 3.1.2 Referential Transparency Pure computations yield the same value each time they are invoked. This property is called referential transparency. A mobile application may require proof of correctness of an algorithm, a clear understanding of the code by the programmer and the compiler for better code optimization. Referential Transparency would be a clear solution to this. Referential transparency helps the compiler to optimize code by memorization, common sub-expression elimination. A program could have been written to invoke a function inside a loop construct. A referentially transparent function could be moved outside the loop since it can be evaluated at any time. Such decisions can be taken by an intelligent compiler easily when we use referential transparency. 3.1.3 Polymorphism Functional paradigms implements polymorphism in two ways namely, Ad-hoc polymorphism and Parametric polymorphism. Generally polymorphism enables a single interface to operate upon values of different data types. Adhoc polymorphism may provide different functionalities through a single interface when the number and type of data vary. Instead, when the same functionality needs to be provided, parametric polymorphism should be chosen. This choice is necessary to avoid redundancy of code. Hence a clear choice of the type of polymorphism should be made. In this case, functional approach is better than imperative programming polymorphism approach. Due to the effectiveness Java and C++ have already derived the parametric polymorphism under the names Generic class and Templates. This is yet another concept to save mobile resources. 3.1.4 Lazy Evaluation Vs Eager Evaluation In imperative programming language to find nth elements of an unsorted list is expensive since it will search the entire list, this is called Eager Evaluation. But in a functional programming language the lazy evaluation approach is in practice. Lazy evaluation ensures that the list will only be sorted enough to find the nth element and it s actually performs well although it would have to perform some moderately complex book-keeping. For mobile computing the lazy evaluation approach is more suitable than eager evaluation approach of imperative paradigms as it uses less processing power. 3.1.4 Recursion As discussed in section 2.1.4, recursive technique is inefficient in OOP and procedural programming, as it push stack to large sizes. Functional programming recursive techniques are fast, simple and not using stacks. Functional languages also tend to make function calls very cheap and allocation on the heap cheap. That is combined with a recursive type and recursive techniques all adds up to heavy use of recursion, while retaining performance [1,5]. Hence it is very suitable for mobile computing. Tail recursion is iteration in any pure functional language implementation. The following simple program Example 3 is to add a sequence of numbers using GHC Haskell. Example 3 loop: leaq 1(%rsi), %rax addq %rsi, %r14 cmpq $10000001, %rax jge.lbb1_5 addq $2, %rsi addq %rax, %r14 test: # %tailrecurse cmpq $10000001, %rsi jl loop (Source [11]) So we had a pipeline of recursive functions, which were compiled to a single tail- recursive function, which was compiled to a single imperative loop using no stack. This is why both ISBN: 978-0-9853483-8-0 2013 SDIWC 78
functional composition (functional concept) and recursion are extremely efficient in optimizing function languages. 4. FUSION OF FUNCTIONAL AND OBJECT ORIENTED PARADIGMS Microsoft F #, Scala Java-based functional programming libraries, such as LamdaJ, FunctionalJ, functional Java incorporates concepts from object-oriented languages and functional. It provides features such as higherorder functions, closures, currying, filtering and functional composition instead of multiple inheritance. All this is complemented by a powerful type system, which allows expressing programs in a concise and elegant, but type-safe [6]. We can use these types of programming languages to design green mobile programs by choosing appropriate paradigms. But intensive research needs to be done to design an appropriate language for only supporting suitable programming as per our discussion which can serve our green mobile computing needs. 5. CONCLUSION In conclusion, if we formulate the programming paradigm by identifying the suitable features from both functional and non-functional programming paradigm, it will help us to achieve optimal resources utilization of mobile devices. In order to avoid the memory leak and stack overflow, minimize the use of object oriented features such as inheritance, creating many objects and avoid features such as recursion, concatenating strings thread synchronization and declaring global variables. Due to these issues, generally Java like languages execution speed is low and it takes more memory. Functional programming features such as higher order functions, parametric polymorphism, referential transparency and list comprehension principles are more suitable for current mobile computing scenario. 6. ACKNOWLEDGEMENT Thanks to God for making this paper successful. Sincere thanks to our university for allowing me to use the resources during research and analysis process. I would like to thank library manager Ms. Haresh Kumari and my colleagues for extending their valuable support and encouragement.last but not least thanks to my family for their understanding, tolerance and patience while I was writing this paper. 7. REFERENCES 1. Don Stewart: Efficient recursion in functional languages vs inefficient recursion in different paradigms stack overflow (2013). 2. Joel Sposky : Can your programming language do this, Joel on Software (2006). 3. John Hughes: Why Functional Programming Matters, Research Topics in Functional Programming ed. D. Turner, Addison-Wesley, pp 17 42 (1990). 4. Jon Bentley : Writing Efficient Programs, ISBN 0-13- 970251-2 (1982). 5. Mark Jason Dominus: The wonderful "High Order perl (2005). 6. Martin Odersky, Philippe Altherr, (eds) : The Scala language specification. Technical report, EPFL (2004). 7. Sunil Dutt, Shubhnandan, S. Jamwal & Devanand : Object Oriented Vs Procedural Programming Embedded Systems, International Journal of Computer Science & Communication Vol. 1, No. 2, 47-50 (2010). 8. Xiaogeng Zhao and Peter Clayton: A Comparative Analysis of Java and.net Mobile Development Environments for Supporting Mobile Services, Rhodes University (2003). 9. Danail Dochev and Ivo Hristov: Mobile Learning Applications- Ubiquitous Characteristics and Technological Solutions, Institute of Information Technologies,1113 Sofia (2006). 10. www.haskell.org/haskellwiki/functional_programmin g#referential_transparency (2013). 11. http://donsbot.wordpress.com/2010/02/21/smokingfast-haskell-code-using-ghcs-new-llvmcodegen/(2010). ISBN: 978-0-9853483-8-0 2013 SDIWC 79