stringObject.IsNullOrWhiteSpace() a simple c# extension method

Every time doing String.IsNullOrWhiteSpace(input) is irritating. Moreover we try to input. and look for this method in intelligence. But it won’t be there, as it is static, unless we do String. we don’t see this method.

Writing an extension method will save us this trouble.

public static class MustHaveCSharpExtensions
{
	public static bool IsNullOrWhiteSpace(this string input)
	{
		return String.IsNullOrWhiteSpace(input);
	}
}

c# new feature request : AutoInitListsOnFirstUse

Another sugar coating – a compiler should easily generate code for this (?) without any side effects. why force people to do new boiler plate code ?

List<string> myListOfStrings;

myListOfStrings.Add(“firstString”);

This throws NullReferenceException until we do new List<string>();

The probability of it happening is more when this is part of another data class, which we initialized but forgot to look into all hose other initializations we need to do.

In case somebody want to avoid this behavior we should have an attribute to support old behavior.

[SansAutoInitListOnFirstUse]

Creating dotnet solution, project and test project with cli

This is a good source. https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-mstest 

mkidr MyConsoleProjectWithUnitTest
cd MyConsoleProjectWithUnitTest/
dotnet new sln
mkdir HelloWorldConsole
cd HelloWorldConsole/
dotnet new console 
(or dotnet new classlib for creating Library instead of console project)
dotnet run
cd ..
mkdir HelloWorldConsole.Tests
cd HelloWorldConsole.Tests/
dotnet new mstest 
dotnet add reference ../HelloWorldConsole/HelloWorldConsole.csproj 
cd ..
dotnet sln add HelloWorldConsole/HelloWorldConsole.csproj 
dotnet sln add HelloWorldConsole.Tests/HelloWorldConsole.Tests.csproj
code . 
# Make modifications to your tests, program. 
Cd HelloWorldConsole.Tests
dotnet test 

c# using custom attributes to validate prerequisites for methods, controller APIs

If you have bunch of prerequisites to be validated for more than one method, we could make code more cute by using an attribute.

For normal methods, this new custom attribute’s constructor could throw exception in case of failure.

But throwing exception in Controller API is bad idea. We want to return more gracefully.

Instead of extending Attribute, extend AuthorizeAttribute and override IsAuthorized() [ or AuthorizeCore() ] method to return true or false. This is more graceful way of doing prerequisite check.

I know this AuthroizeAttribute is to check authorization, but we could mis-use it to validate any prerequisites until a better way is available.

/*****************************************************************************************/

using System;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
internal class MustBeAuthorizedAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{

if (preRequsitesFail)

{
return false;

}

return true;
}
}
}

/*****************************************************************************************/

[MustBeAuthorized]
public ActionResult Index()
{
return View();
}

/*****************************************************************************************/

Thanks to @philip instead of AuthroizeAttribute  ActionFilterAttribute is better. We could return any return value (e.g.. MethodNotAllowed, InternalServerError); see https://stackoverflow.com/questions/16822365/web-api-how-to-stop-the-web-pipeline-directly-from-an-onactionexecuting-filter 

notes, unit testing c# mock, stub, shim!

What to unit test?

Some times it feels why the heck are we adding this unit test? Some times did we really added at least minimum required unit tests or not?

I spent good amount of time researching this topic.

My thumb rule is :

Assume you are adding a document with samples directly taken from Unit tests (except Mocking part though) ; We want our Unit tests to test what customer typically do.

Unit Test is a shield:

Unit test works like a shield. It protects from the minute it is born. This unit test shield will protect the code from your future self. This unit test shield will also protect from others working on this piece of code. Mistakes which will break some existing user can be avoided with this unit testing strategy.

Mock Vs Fake(Stub/Shim)

Use whatever works! External DLLs are better faked. But there are some beasts which will have give tough fight to be faked. In that case do not directly call this external DLL, rather call via a thin layer, a layer with zero business logic but with an interface+class design. Then mock this layer instead of trying to fake your external DLL. Unit tests are saved!

An idea to keep Microsoft Windows relevant for another decade

Microsoft Windows, a must have for every middle class home is no longer true. Everybody is buying phones. Either Android or iPhone and occasionally WinPhone. Windows Desktop is no longer relevant. All the developers moved to Browser or Mobile. All the users happily moved to web or apps. Except office, Adobe no major apps continue to innovate for Windows desktop. Is there any miracle in pipeline to reverse the tide?

Doesn’t seem to be so. Microsoft is trying to move towards single OS for all screens hoping it will somehow solve its developer problem. But no positive signs in that direction.

At the same time Microsoft is concentrating on enterprise market and cloud market. Both of them turning out to be its bread and butter for future. At least stock market thinks so.

What else it can do?

Idea: Introduce Home-Server concept!

No more home-PC, it will be home-server. Or PS – Personal Server! Unlike the past – now middle class homes are generating too much of digital content. Everything is either going to specific clouds or to Google servers! Introduce a concept of MyData, MyHardware, MyControl. Every home will have servers. (no more PCs); Developers can take advantage of APIs and can write data, analyze the data, improve customers life greatly. Data is the most valuable asset. Complete ownership, privacy, security is divine gift one can wish for!

This will nurture a new developer echo system very soon.

For example based on the data in PS’s we could have apps around

  1. What is my ideal sleep time?
  2. How is my health?
  3. Where is my daughter now? Where is my family in past three days?
  4. Most liked photo in my album.
  5. My video dairy
  6. How much time the light is on in past week?
  7. How many times garage door opened?
  8. When is water filter change time?
  9. Auto prepare my shopping cart for the week.
  10. What am I doing 10 years back.
  11. Auto write my auto biography so far!
  12. How to be happy?
  13. what makes me happy?
  14. what makes me sad?
  15. What are my strengths? weaknesses ?
  16. etc..

Taking IOT concept, edge computing into consideration. Allowing sharing of data with friends and family servers. We could have much better app scenarios.

  1. What is the fad in neighbors now?
  2. Who are potential friends I haven’t yet met in my neighbors?
  3. Who had three D printer in my neighborhood?
  4. Display garage sales happening
  5. What is my father doing now in India?
  6. Suggest new hobbies for me
  7. Who can give me training on xyzzy?
  8. who needs training on abcd?
  9. etc..

May be all is not lost yet.

Opportunity: Every damn innovation now a days requires gathering data. But all the data belonging to a home is widely scattered. There is simply no way to cross reference data, unless both solutions are coming from same company. Once PSs comes live any solution can input data to PS in predefined format. Same can be queried by other solutions after proper permissions sharing handling. Of course no direct access to data is allowed only via predefined APIs yadha yadha!

This will also be biggest blow to its competitors. Who’s bread and butter is selling users data in one form or other form. Now Users own their data, it is not straightforward to sell it!

What more? There are thousands of patents as the arena is very new!

Idea2: Phone is PC. (Phone + Docking station + Screen)

Just like after laptops the desktops are optional. after phones we could make laptops/Desktops optional. We need to make docking stations innovative. Everything else we need will be in phone. Phone is new PC. Microsoft should start selling these docking stations which makes any phone (Android, iPhone, WinPhone) as PC! The Phone + docking station + screen (either smart TV or a separate monitor or existing laptop/desktop screen). All the Windows apps will run on docking stations but uses data from phone, writes data to phone. User folder will be on phone. (that is MyDocuments, MyPictures, etc.. will be on phone instead of PC).

 

iOT – 1 Internet of things what it means for our future?

Internet of Things. The latest of latest jargons. Simply put disconnect machines from their human masters. Let machine communicate themselves. Let machines take predefined decisions based upon the data they receive, while the human masters are immersed heavily in their shopping to heart or workaholicing ;

As tech guy + science fiction fan + a bit of creativity I am trying to guess how this is going to change our simplest of simple lives.

We visit a restaurant ; The glasses there are really bottom less. You drink the fluid and keep the glass over table. Glass communicates to table it opens its bottom – table pushes more fluid to auto-fill the glass.

The buffet wastage is reduced. Buffet is started, the dishes knows how much is consumed how fast it is consumed, the door knows how many foot falls are coming in and their weight, even their previous history. The plates knows who is eating how much and what. The smart monitor in kitchen displays  which dish is required in next five minutes, sixty minutes.

TBC

Are you missing the power of vim on Eclipse?

Are you missing power of VIM in Eclipse?

Here are quick steps to make eclipse work like vim at the same time use eclipse’s power features. Best of both worlds!!

1. Install Vrapper. 

2. Use split editor feature of eclipse. (Window -> Editor -> Toggle Split Editor ) See this help page 

3. Map Ctrl + W from Closing windows to switch Windows!! (Window -> preferences -> General -> Keys)

 

Happy Vimming,

Happy Eclipsing

🙂