{} Matter!

        [SuppressMessage( "Style", "IDE0011:Add braces" )]
        public int BadlyWrittenCode( ref int x ) {
            if ( foo( ref x ) )
                bar( ref x );
            if ( foo( ref x ) )
                bar( ref x );
                baz( ref x );
            if ( foo( ref x ) )
                bar( ref x );
            else
                baz( ref x );
            if ( ++x < 127 )
                bar( ref x );
            x++;

            //TODO (1 year later..) Was baz always supposed to be called, or is this a bug?
            //TODO (1 year later..) Was x intended to always be incremented, or is this a bug?
        }

Code is for humans to read. By adding these brackets, you make your code’s intention (the LOGIC) clear.

(Note: For those of you in the U.S., “{}” may be called braces. Other parts of the world may call them brackets. They’re the same set! Lol. “<>, (), [], {}”. Open your mind.. Not everyone codes in the U.S.)

We now have the ability to use the new form of using, i.e. “using var outer = new Outer();”. And please use the new format when it makes sense.

But for “if”, “for”, and “while” (and others) you still should use the curly brackets “{” and “}“.

If your braceless code is ever used/refactored again, even by you, the lack of proper brackets will bite with a nasty bug and (more than likely) that will not show up at compile time.

If your argument against using curly brackets is that it’s not worth your time to take the extra milliseconds to type them.. then you’re charging too much. Way too much.

Focus on writing bug free and future-proof code instead of saving keystrokes or minimizing line counts. Personally, I am sick and tired of finding & fixing bugs caused by other people’s laziness (even though I get paid quite well to do so).

Do not write code to the lowest common denominator!

Adding the brackets is so simple, most code formatters can even add them for you.
It’s simply laziness to not add them.

Okay, an example you need? Nah, if you haven’t learned from this example, experience, or an experienced programmer, then learn from Google.. I’m done wasting my time trying to teach you.

You’re still reading? Good.
Do you contest my assertion that adding brackets/braces is always the correct way? Yes? Then show me an example that causes the code to BREAK by adding the proper braces.

I triple double dog dare you.

Ordering of static fields in C# matters

via Ordering of static fields in C# matters

The ordering of static fields does matter.
But per your examples, here is the pattern I always use:

public class Person
{
    private static EmptyPerson _emptyPerson;
    public static Person Empty => _emptyPerson ?? ( _emptyPerson = new EmptyPerson() );
}

The allows the Empty value to be initialized once and read-only, yet still used many times.
It also hides away the requirements of the initialization (the value pulled from a database for example) from the public getter. We could even put the value into a Lazy<Person>().

And in C# 8.0 we now have the compound assignment!

public class Person
{
    private static EmptyPerson _emptyPerson;
    public static Person Empty => _emptyPerson ??= new EmptyPerson();
}

 

Posted in C#

Samsung NVMe 3.1 Driver

Download Here: Samsung NVMe 3.1 Driver
Password: “Samsung“.

I created this because sometimes the windows executable from Samsung won’t install if a temp folder is not set correctly.

This archive is the 3 files from the official Samsung NVMe 3.1 driver download page with no modifications, just zipped up.

Remember: Always create a Restore Point before you install, and keep your antivirus ENABLED.. just to be on the safe side.

Here are my results before installing this driver.
nvme standard 1GB

And the results after installing this driver.
nvme samsung 1GB

Conclusion: only slightly better than Microsoft’s NVMe driver on this computer. Both results are within the margin of error for a Windows Pro workstation computer.

Sending a text message from SQL Server

You must have SQL Server database mail enabled for this stored procedure to work.

CREATE proc [dbo].[SendTextMessage](
@number nvarchar(255)
,@message nvarchar(255)
,@subject nvarchar(255) = null
,@provider nvarchar(20) = 'Verizon'
)
as
begin;
set nocount on;

set @number = ltrim(rtrim(@number));
if @number is null or len(@number)<7 begin;
set @message = '@number is too short';
throw 51000, @message, 1;
end;

set @message = ltrim(rtrim(@message));
if @message is null or len(@message)<2 begin;
set @message = '@message is too short';
throw 51000, @message, 1;
end;

set @subject = ltrim(rtrim(@subject));
if @subject is null set @subject = @@SERVERNAME + N' Notification';

declare @to nvarchar(255) = @number + N'@'+
case @provider
when 'Verizon' then N'vtext.com'
when 'VZ' then N'vtext.com'
when 'ATT' then N'txt.att.net'
when 'SPRINT' then N'messaging.sprintpcs.com'
when 'TMOBILE'  then N'tmomail.net'
end;
if nullif(@provider,N'@') is null begin;
throw 51000, 'Unknown provider. Message not sent.', 1;
end;

begin try;
declare @mailitem_id INT;
exec msdb.dbo.sp_send_dbmail @recipients = @to, @subject = @subject, @body = @message, @mailitem_id=@mailitem_id output;
end try
begin catch;
throw 51000, 'Unknown error calling msdb.dbo.sp_send_dbmail.', 1;
end catch;
end;