Showing posts with label client-side programming. Show all posts
Showing posts with label client-side programming. Show all posts

Friday, August 29, 2014

Don't Make Convoluted Code Part 2

Client-Server Processes
Client-Server Processes
Now we have enough understanding of the differences between client-side and server-side scripting and the scenario of the entire process with the help of previous part.


Let's explore the real life scenario through an example code where PHP code has messed up with the JavaScript code in a website programming document.

<script type="text/javascript">
    var foo = 'bar';
    <?php
        file_put_contents('foo.txt', ' + foo + ');
    ?>

    var baz = <?php echo 42; ?>;
    alert(baz);
</script>

our hire dedicated programmer team has created and run this test code and unfortunately browser has responded in wired way. It displayed '42' placed into the PHP code, instead of 'bar' put into the JavaScript code.


Let's dissect the code in to two fragments. One is JavaScript code and another is PHP code placed as nested code in JavaScript code. As we know that JavaScript code is falling in to the client-side code category. Whereas PHP code is purely server-side code. I am using word 'purely' as PHP never runs on the client devices at all while JavaScript code may created as the ensue of server-side scripting and server many times send JavaScript code embedding into the HTML documents to get desired results. Therefore, we can't say that JavaScript code is not found on the server. However, server don't need to run JS in most cases and simply generate it or packed with HTML docs.


With this understanding let's see what happened with our example code closely. When we run this code to test, it reaches to the server and run the PHP code first in the server. Once the PHP code execution finishing or PHP scripts end up, server stops the running any code and return the results obtained through PHP code compilations. There are no chances of running JavaScript on the server at all. Moreover, the returning results also missing the original JavaScript code so you only getting the results of PHP code executions in the browsers of the clients.


Therefore, in our example code we don't get any results for the 'foo' variable written in JavaScript. Whereas server run the part of script located in between the <?php...?> tags only and send alert 42 in return. Therefore, only alert call works and foo variable missing at all. However, this was a simple example code, but when you need to pass the variable or any sort of data written in between the JavaScript there is a programming trick.


You have to rely on the Ajax code means you have to call server via Ajax requests for any kind of dynamic data or you have to put data into the source code of the page, if they are static data so it can be processed with JavaScript. The later technique of embedding in to JavaScript is bit complicated and demands adequate knowledge of converting data into the server-side scripting for further processing.


Conclusively, I can say that if you have any ambiguity regarding to the server-side and client-side scripting and their rules of using in practice, you will end up with convoluted code and prove your code a headache for QA team or for the maintenance team in future. Therefore, our hire dedicated developers department take care of such code understanding and best coding practices prevailing in the industry in order to provide quality work to our patrons.


Thursday, August 28, 2014

Don't Make Convoluted Code Part 1

client - server interactions
Client-Server Interactions

Today we are living in fiercely competitive environment on outsourcing horizon and we need to create products with high quality with least investment of time and resources. All quality software products need standard coding with the best programming practices. If anything goes wrong in code, it may lead to frequent debugging. In other words, it proves waste of time and resources behind non-productive work.

However, best programming skills come with prolonged experiences and thorough understanding of technologies, tools, and techniques. At Lujayn, the team leaders of our hire dedicated programmers departments frequently do complains regarding to the lack of proper understanding of programming in junior web developers. Therefore, they always avoid their participation at critical levels and let them to learn things correctly and in righteous manners.

Among such complains I have sort out one that is surfacing frequently in discussions. Let me translate that in words. It is lack of understanding of differences between client side programming and server side programming. Thus, they tend to mix up the server side code in to client side code and vice verso. This convoluted code trigger unexpected results and some tough to detect bugs in the products at the end of the project. Let me explain this in details.

Understanding of Server-side and Client-side Programming

Understanding Client-Server Terms

When we use the website or web application or a web app on mobiles, we have two parties working behind the display of web interface on your devices. The first one is your device itself and another is web server that communicate with your devices using HTTP protocol or other modern ways/protocols we can say cloud-computing technologies. Let’s stick to the HTTP protocol for ease in understanding.

Technically, server is a single entity, which is located at remote and central place and communicating with the innumerable devices across the world using Internet. Its prime role is to server the web pages or web interfaces of web applications. These web pages or web interfaces are displaying on the various devices with browsers.

These browser enabled devices term as clients, the clients of the server, which are using the services of the server. No doubt, in a broad concept, not all the client devices need browsers to use services and HTTP to communicate, modern web services in particular. For sake of simplicity, we avoid all exceptions and stick with HTTP and browser concepts.

Now, we know about client and server terms with their respective roles. Let’s check what happens at the code level during the client-server communication. At first place, client wants to see the web page in browser to meet its needs on the web. In due course, client sending request for a particular website by typing URL in browser using HTTP protocol.

Processes on the Server

This request is reaching to the server and server begins processing on it. It find out the source code of the requested website in its databases/storage/memory. Server Runs the source code and create an output in form of HTML or other standard forms, which can render on the browsers of the client devices across the world.

In next step, server send the website code to the client device and now client device/its browser, turns it into the human visible web pages on the screen of device. When device render the code sent by server it processes on it

Server-side Scripts

In above scenario, we have seen that server runs the source code on its machines using its own mechanisms/compilers. During this process on server, some code turns into the different forms in output files or omitted from the inclusion in to the output. Therefore, they term as server-side code or programming and PHP, ASP.NET, C++, C#, Java, ROR, Perl, etc. are claiming as server-side scripts/languages in web development industries.

This server-side scripts do following actions on server and triggers outputs accordingly.
  • Its first and foremost function is to process the user inputs
  • Create pages for clients to display
  • Structure or frame the web application
  • interact with databases

Processes on the Client

Once user start using client devices, she put some input to process and create a request to send to the respective server. When server in return send the outputs to the client, its process on its and interpret the code and render it in browsers or interfaces, which are human readable. The performance of client depends on that what the code is coming back to the client from the server and what the capacities of the client devices to process it. Due to known constraints of mobile devices, responsive designers and programmers prefer to have more processes on the server, while in case of big sister devices web programmers go leeway.

Client-side Scripts

As we know that server is mainly sending markups and styles in return of the requests made by clients. Therefore, HTML and CSS are main forms of code need to be rendered on the client or browsers. However, server also send JavaScript in some responses along with HTML and CSS. Today other scripts mainly based on JS libraries like jQuery, Media Query, etc. are in use for dynamic and interactive website or web applications for multiple screens.

No doubt, the king is still JavaScript and it always renders on the client devices therefore, it creates great confusions for the newbie developers when they embed the JS code in HTML along with other PHP code. In next part, we will see some practical problems and try to understand the differences between server-side and client-side programming paradigm.



Total Pageviews

Pages