pages

Monday, 11 May 2020

Git - common commands

Below are some common git commands which help with almost anything you need to do with git day to day basis.


How to get the latest changes

  • git fetch: gets the latest changes from server, keep it in local without applying.
  • git pull: gets the latest changes from server, try to merge with your local to make sure your local is up-to date with the server. 
  • git rebase : rebase your working directory with the latest changes on the given branch 
  • git reset: reset all your local changes and moves you to latest head position available in local (if you have done git fetch before, this will be same position as server) Flags are listed below 
    • hard : lose all uncommited changes and all untracked files 
    • keep : It only resets the files which are different between the HEAD and local. It aborts the reset if one or more of these files has uncommited changes. It basically acts as a safer version of hard. 
    • soft 
    • mixed 
  • Head~1 : points you to one commit before head, used with git reset.


Save your changes 

  • git stash: saves your changes in local. 
    • m : used to pass a message to stash (git stash -m "stash message") 
  • git stash pop: gets the latest stash and merge with your code. 
  • git commit: commits your changes to working directory. 
    • a : all 
    • m : used to pass a message to commit (git commit -m "commit message") 
  • git push: sends all the commits from working directory to server. 
    • u : upstream, pushes the local branch to server 

Miscellaneous 

  • git status : tell you about your working tree/directory 
  • git add : add the files to stage, ready for commit
    •  . : adds all the files 
  • git branch : tells about all the branches available 
  • git branch : creates a branch with that name 
  • git checkout : checkout the branch specified

Thursday, 23 April 2020

Override ToString in Csharp

First question that comes to mind is why should I do that. My answer is “for fun”. ;)
The code is pretty simple to write. Here is a sample.

There are two return statements. One return name and salary, while the other returns json string.

It is mostly recommended to override ToString for debugging purposes.

I was able to find a good stackoverflow answer written in 2012, talks about why we should do that.

There is an important note on msdn article on how to override ToString which is as below.

"When you decide what information to provide through this method, consider whether your class or struct will ever be used by untrusted code. Be careful to ensure that you do not provide any information that could be exploited by malicious code."

You can find the complete program here.





Tuesday, 11 February 2020

Using yield return in CSharp


I was not very sure on the use of yield return in C#, until I read completely about it in very simple manner. I will try my best to share all I learned in as simple words I can.

I know it’s a very old thing and many people online use it, but I believe there, still, are many developers like me who are avoiding it because all the books and articles explain it in very few words at very high level view.

So, let us just start with a simple premise, we need a function to return list of numbers from a very large sequence. An example can be to return the list of first 100 even numbers, assume that later on in the program we are processing our list of 100 even numbers. In your case it can be to pull the record of customers from database on certain condition.

Well there are many efficient ways to write the same program, I intentionally chose this, as this post is about keeping it simple.

If you keep the input as 100, there is no major memory allocation, but if the input is changed to 1million or more, the memory allocated for execution of the program increases drastically. Same can be checked using diagnostic tools in visual studio.


We live in the world where memory cost is always ignored against CPU, but that should not be our reason to fill the stack when we can do better.

yield return is a solution. I wrote the same program as above, the only difference is the return type. Instead of returning the List we are here returning IEnumberable.

Don’t even think of counting the elements in the sequence. This is IEnumerable, not IQueryable. IEnumberable<T> exist in System.Collections.Generic.
IQueryable exists in System.Linq. This article talks about the difference in detail.
You can find the complete program on github.

I hope this was easy. Please share your thoughts in the comments below.