ASP.NET MVC and passing model as string to view

Another day, another “hit me hard” encounter with weird coding practices. The code below has been extracted from an ASP.NET MVC 5 project. It implements an action for a controller and the names have been modified. Only the main issue remains: some developers (I can no longer call them young or rookies or otherwise) create a model as a string. Of course when they check the string they do it case sensitive!

public ActionResult Action173( string email)
{
   if( String.IsNullOrEmpty( email ) )
   {
       return View();
   }
   
   var succeeded = BackendService.ExecuteAction173( email );
   return View( "{view name here}", succeeded ? "success":"failed" );
}

My first question when seeing the code was: how is the string used in the page? Is it actually displayed in the page?
No, the code in the view is something like:

var pageLayout = (String.IsNullOrEmpty(@Model) )? "invoice": @Model.Equals("success")? "success":"failed";
...
@if( pageLayout == "invoice")
{
...
}

@if( pageLayout == "success" )
{
...
}

@if( pageLayout == "failed" )
{
...
}

So why not define an enumeration in the controller sources (where R# quickly helps you move the definition to another file) and then switch on the passed enumeration? This way the compiler catches immediately a broken parameter.

Bad commenting practice

Another nice code sequence I see often:

 var seconds = 30;
/* rotate every 60 seconds */
function rotate() { ...

Easy to fix and harmless.

The good thing is there’s a bright side: such a code sequence makes it easy to detect a developer that pays attention to the details (I have two developers in the team that have never made this mistake in two years!) and “the other kind of developer”.

Rookie developer code

From time to time I see code that’s been written by rookie developers. Many of them are well-intended, but their code puzzles experienced developers, because the experienced developers are used to ask themselves “is there a situation where this line of code is actually needed, like a bug in the framework?”.

The last experience I had was with the following sequence:

Random rand = new Random();
int iFirst = rand.Next( 1, 9 );
int iSecond = rand.Next( 1, 5 );
lbl1.Text = iFirst.ToString().Trim();
lbl2.Text = iSecond.ToString().Trim();

Why would anyone assume an Int32.ToString() call would insert whitespaces in the result?

Pagini web de doi bani

Ultimele cerinte ale clientului m-au obligat sa lucrez cu Google Analytics. Tabelul de pagini utilizate e cel mai amuzant: in coloana “Valoarea paginii” sunt citeva pagini cu valoare USD 0.03, restul au valoare sub USD 0.01. Cu alte cuvinte, pagini de doi bani :-)

My dog is now Android … :-)

The legislation has changed in Romania and now it seems every dog owner, when innoculating the dog against rabies, he must also implant the dog with a microchip and record himself in the national dog’s owners registrar.

It’s a good thing, let’s hope it will help a little with our national astray dogs problems.

I had to do the implant last Saturday. I’ve told my coworker about this and his reaction was “ok, now you have an android dog” :-D