Posts

Data Schema and Blobs

The schema gives the organization of data. It says what the data means. In the previous example, the 'contact' column had the contact of a person and not an ID. That's what is given by a schema, in addition to the table-structure itself. Often, changes in schema require expensive migration - not only in the database, but also all applications that are using the database. As we saw in the previous example, we needed to change the country to an economic zone, but all zones were not ready to move at once. How can we reduce the burden of migration? Let's examine a few ways: Build some wrappers over the database. So when the structure of the database changes, we can still convert back to the old data. Retain the country and relate it to the region in a separate table: In this way, whoever needs the country can refer to that, and whoever needs the region will access it. This is a clever way that relies on modeling like the real world. Change the organization of the ...

Applications ^ Data

Image
Today's applications are powered by data, just like machines are powered by energy. In this series of blogs, we will look at how applications use data, how data is organized and what effect it has on the evolution of functionality. A professionally managed product will typically have this context: The product itself: X-ray equipment, Surgery devices, Ultrasound machines, toothbrushes and so on. A sales person would be in the process of getting a customer interested A customer would need consumables - like a new brush Many of these equipment produce images A customer would own one or more equipment A service engineer would service a set of equipment A product will have parts that are replaceable Spares would be stocked In today's world, each of these aspects are supported through applications. Application experience is enhanced with data. How do we store and correlate the data, so that it's usable? One way is to structure the data by modeling using Entit...

Relationships in a Relational Database

Image
A Relational Database models data in the form of tables and relation between tables. Consider the following application: When a customer asks a question about an equipment, we want to connect to an expert who can answer. For example, an expert on Ultrasound is different from an expert on X-ray; and there are different experts in different countries. One way to model this data is to make a table with the equipment identifier and the contact of the expert. As you can see, there's a lot of redundancy; the expert's contact is replicated in multiple rows. Not only is this inefficient in terms of storage, but also multiple updates need to be done when there's a new expert. How could we solve this? Any identification we repeat will have the same redundancy. The answer is to remove redundancy - normalize the data. The best normalizations are the ones that reflect the real world. In this case, the equipment and the region are linked to the contact. So one way to organize th...

Transacting in a Relational Database

Image
SQL is a language used to transact with a Relational Database. If you are unfamiliar with SQL, you can quickly get a flavor for it over here:  https://www.w3schools.com/sql/ SQL statements can be used to create, delete and update databases. The concept is based on the principle of transactions - till you commit a set of modifications, none of them are done. When you commit, all of them are done together. This way, 'integrity' of the data is guaranteed. However, strict schema management may have its downfalls... What if we need to add columns or the type of a column changes? What happens to applications that were written with old assumptions in mind? In the example we saw before, what if the schema changes? Let's say we discover that the economic zone is more important than the region. So we'd like to replace the region field. Economic zones have abbreviations like ASEAN, EMEA, etc. This change may be driven by considerations of cost: Costs are less when the pa...

Power On!

Welcome to this series of blogs. Let's do a review of our favorite languages! Looking for the next piece on Applications ^ Data? See here! These blogs are designed to trigger questions and discussions. It's not about being right or wrong. The purpose is to discover, know and develop. Feel free to leave comments, ask and answer questions, voice opinions and enjoy! Guideline : Please be respectful and welcoming of everyone's views and questions. You are now a part of our team :) Click here  to attempt the  assessment of this series Notes: - You can login using your gmail id . If you have provided us the gmail id already, it will succeed. - Otherwise, please write a mail from your gmail id to sudeep@rapalearning.com with subject Bootcamp access - Each lesson has an assessment. Each assessment can be submitted once. Here's is a sequence we suggest: Building block of polymorphism : Start here for a fun exercise to program an artillery firing sequence... in C...

JavaScript functions

Functions are famously called first-class citizens of the JavaScript world. Functions can smoothly be treated as a variable,  For example, if we execute this in a browser: function myFunction(a, b) {   return a * b; } var x = myFunction; console.log(x(3,4)); It will log 12. The declaration of myFunction makes it a 'value', which is assigned to a variable 'x'. Notice that there are no parenthesis in that assignment. Next, we have a log that invokes myFunction by applying the parenthesis () operator on the variable. The log statement logs the return value of myFunction, which is the product of 3 and 4. Let's say we forget the parenthesis: console.log(x); This will log the function itself! Try it out in your browser by enabling the console display via the 'developer tools' option. Of course, this will just call the function normally: var x = myFunction(3,4); It's just the application of the () operator to call a...

JavaScript data-types

JavaScript is a versatile language that's used in programming everything from web pages to servers and desktop applications. We will use this interactive tutorial  as a reference in our journey. JavaScript is different from the other languages we've seen here (C++, C#, Java), in that it doesn't have a compiler. The code is interpreted while running it. This brings us to a distinction in data-types as well. JavaScript doesn't fix the type of its variables. For example, this is a valid sequence for the interpreter: var x;           // x is undefined to start with x = 15;          // After this assignment, x is a Number x = "Phil";      // Here x becomes a String Many other fun things can happen with JavaScript data, as you can see here . What could be the use of such flexibility? Tell us what you feel below. Hint: Think of real-life arrays - like array of vehicles, array of chat messages...

Java Characters

Characters are represented by codes. ASCII is a standard for alphabets and a few symbols. For example, the code 65 is used for the letter 'A' char c1 = 65; System.out.println(c1); Will print the character A. Modern Java compilers and Operating Systems are aware of Unicode characters. Unicode is a standard system of encoding characters in all known scripts. Here's code to print the recent 'Rupee' symbol: char c1 = 0x20b9; System.out.println(c1); Will print the symbol  ₹ If you don't have a local Java compiler, you can use this online compiler . Explore Unicode. Share some characters and the corresponding codes of your mother-tongue. Click here to power back

Java Strings

If you are unfamiliar with Java or interested in a refresher, use this link . In this blog we look at some quirky dealings with Java Strings. We'll be using concepts of Java variables , data-types and operators and especially Strings . The following code constructs two strings  s1  and  s2 . When they are compared, their contents are identical. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1 == s2) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does NOT equal " + s2); } Strangely, this code reports that the strings are not equal: 12345 does NOT equal 12345   Why is that? How do we fix this? Notice the change in bold below. String s1 = "12345"; String s2 = "123"; s2 += "45"; if(s1.equals(s2)) {     System.out.println(s1 + " equals " + s2); } else {     System.out.println(s1 + " does...

C# Collection and Iteration

C# has powerful structures for dealing with collections and other data structures. In this blog, we'll be using string formatting  and lists . Click on the links if you haven't done them in C# before. var names = new List<string>   { "nobita", "bheem", "oggy", "oggy cockroach", "ninja" }; foreach (var name in names) {     if(name.StartsWith("oggy")) {         Console.WriteLine($"Need to remove {name}!");         names.Remove(name);     } } This code tries to remove names starting with "oggy" from a list. Try it in the interactive editor offered by the above tutorial (click 'Enter focus mode' at the bottom of this site ) Does it work? No! Which is the line that's causing the crash? How would you resolve it? Click here to power back

C# loop optimization

See here for an interactive tutorial on control flows in C#. At the end of the tutorial, you see this piece of code, to sum all numbers between 1 and 20, which are divisible by 3 int sum = 0; for (int number = 1; number < 21; number++) {   if (number % 3 == 0)   {     sum = sum + number;   } }   Check the cyclomatic complexity  of this code. Is there a way you can simplify it by removing the nesting? Type your thoughts below. Going ahead, we'll look at using loops to iterate over a collection

C# arithmetic

See here for a short interactive tutorial about number-arithmetic in C# Of course, arithmetic in C# is similar to any other language.  Here's something to think about: Normally, if we divide & multiply by the same number, we get back the variable. For example: (c / a) * a  Would evaluate to  c  itself. However, there are some situations in which this is not true. It will not evaluate to  c What do you think? Write the situations that come to your mind below. Next, we look at control structures and a small optimization problem

C++ functions

See here for a discussion on the types of arguments that a C++ function can take. A discussion on passing arrays in particular is given here . More about this in the assessment :) Click here to power back

C++ control structures

See here for a quick refresher of looping in C++ Usage of while statements are quite common. Why do you think a do-while loop is required to be present in the language at all? Have you used a do-while loop to solve any problem? If you haven't, do you think it's useless? Let's know what you think. Next, we look at functions and arguments in C++ .

C++ scope

Variables store the state of a program. We know that it's the way a program 'remembers' something. This 'state' is also notoriously prone to side-effects. A variable altered in one place can have un-intended consequence in another. The 'scope' of a variable determines where it can be accessed. A variable with lower scope can be changed with higher confidence (= lower side-effects). See here for an introduction to scope in C++ C++ also scopes variables in classes. See here for details. Is the scope of a variable checked at compile-time or run-time? In other words, can we access a private data member if the class returns a pointer to it? class PrivacyViolator {   private:     char name[256];   public:     char* getName() {       return name;     } }; Can I call  getName  from outside this class and change its contents?       PrivacyViolator p;     ...

C++ data types

See here for an introduction to the data types provided by C++. Let's explore some data types. Consider this code - it initializes an 'unsigned short' to 0 and decrements it. Will it become negative, though it is unsigned? unsigned short int i = 0; i--; Try it out and check the value of the variable after decrementing. Consider that the variable i is used to index an array: char name[] = "Helloo"; char c = name[i]; What would be the outcome of accessing the array in the second line? Let us know your thoughts below. Next, we look at variables in C++  and their scope.

Polymorphism problem statement

In the previous post, we saw a possible way to fire a series of artillery guns in quick succession:   void synchronized_fire() {   howitzer_v1_fire(GUN_ID1);   dhanush_fire( GUN_ID2 );   bofors_v2_fire( GUN_ID3 );   howitzer_v2_fire( GUN_ID4 );   dhanush_fire( GUN_ID5 ); } And we asked ourselves, what's wrong in writing the function this way? Well, to start with, adding a new gun will need us to change this function and re-compile.  Next, suppose we need a new routine of firing - like triggering alternate guns to save ammunition. Then we need two functions to fire odd and even guns: void synchronized_fire_odd() {   howitzer_v1_fire(GUN_ID1);   bofors_v2_fire( GUN_ID3 );   dhanush_fire( GUN_ID5 ); } void synchronized_fire_even() {   dhanush_fire( GUN_ID2 );   howitzer_v2_fire( GUN_ID4 ); } Gradually, such functions proliferate. After this, adding a new type of gun (or repl...

Polymorphism via Function pointers

Function pointers in C provide a method to achieve polymorphism. In fact, they are one of the ingredients in the C++ object model as well. See here for a quick refresher on C function pointers. What we need is 'a function to fire the guns one by one'. Here's an outline of such a function: void synchronized_fire(/*accept array of guns here*/) {    //iterate through the array    //and fire each element in there } What we've done is to remove the logic of 'which gun' and 'how to fire' from this function. Where do we put that logic? Let's try a struct:   struct Gun {   int gun_id;   void (*fire)(int); } Observe that this struct has two members. One is a noun (the gun identification) and another is a verb (the activity of firing). Then the function to fire them all would look like this. Remember that an array in C always needs to be accompanied by its bounds: void synchronized_fire(struct Gun[] guns, unsigned int n_g...

Polymorphism

Image
While programming in C, we get close to the system.  In this course, we'll use C to examine how building-blocks are built and gain a deeper understanding of polymorphism. Let's explore its internals. Polymorphism It means 'occurring in different forms'. It is an indispensable aspect of modern software production. For example, doctors want different forms of imaging for different parts of the body; developers want different forms of environments for testing and production -  Code often needs to run in different contexts. And these contexts keep growing over time. There are many ways to realize these 'different forms' . Let's look at a trivial way, which doesn't use polymorphism. Suppose we had a row of different heavy artillery machines. They look alike, but are made by different companies. Each company has provided its own function to fire the projectile. For example, the Howitzer equipment has a function called ' howitzer_v1_fire ', wh...