megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Error loading V8 startup snapshot file in Visual Studio Code

Unusually, one fine afternoon, I was facing issues with opening my VSCode editor and I faced the followinge error when I tried code . on my project directory:

[0830/101630.031:FATAL:v8_initializer.cc(527)] Error loading V8 startup snapshot file

I didn't really understand what might have caused it but after reading this issue raised on GitHub, I realized that occurred due to a corrupted update while I was shutting down my PC (Good job, Windows! :facepalm:)

If you're are lucky enough, here's how you can get it back to work:

  1. Go C:\Users\XXX\AppData\Local\Programs\Microsoft VS Code directory
  2. If you spot a folder titled _, copy the contents and paste it in the VS Code directly
  3. Try starting your editor again

The following steps worked out for me and if it worked out for you, that's great! Else, you'll have to re-install your editor all over again, which means you might have to re-install your extensions and re-configure it again (if you haven't taken a backup of it).

Hope this helps you out!

Cicada 3301

A short docu-series about one of the most mysterious puzzles on the internet.

Ever heard of Cicada 3301? It's one of those mysterious puzzles that made every crypto enthusiast around the globe turn into super sleuths hunting for clues and chasing the next puzzle. I was quite interested in it after watching this series of documentaries titled Cracking the Code of Cicada 3301 that shot by Great Big Story, which made my interest towards my cryptography grow larger.

Although, I never tried this series of puzzles myself, the actual host of this puzzle is unknown as some claimed that it could be a recruitment programme by the FBI/CIA/NSA/GCHQ to hire super smart individuals or maybe some crypto/security group that cares about data privacy and security.

I remember watching this series back in 2020 and it was quite hard for me to find these videos and hence, I'm sharing it here for you and my reference as well (in case, I wanted to re-watch the documentary again):

What I liked about this documentary series is how it connects people of various disciplines such as Mathematics, Programming, Linguistics and Steganography to deduce and decipher a really complex puzzle. I just find that inspiring because knowledge is power and when aligned with people of similar interests and ambitions, you build new bridges of friendship that spans around the globe.

Hope you liked reading this article.

Generating custom snippets using Visual Studio Code

I've always wanted to write about this feature and I believe this is one of the most productivity hacks you can do to speed your work and worry-less about writing boilerplate code.

For example, if I wanted to write a piece of code that fetches a list of books asynchronously using JavaScript, I would have to type it or maybe copy-paste that stub from another file or project and edit it accordingly. That's fine but when working on a large project or tight deadlines, it can be quite time-consuming if all you want is just a boilerplate code that's ready to begin with.

Here's how you can create your own snippet:

  1. Press Ctrl+Shift+P
  2. Type Configure User Snippets and hit Enter
  3. Select the language for which it should appear and hit Enter

Once done, a new tab will open up with simple instructions for you to get started with. The instructions are quite straight-forward and you can write something like this right below the note:

{
    // Place your snippets for javascript here. Each snippet is defined under a snippet name and has a prefix, body and 
    // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
    // same ids are connected.
    // Example:
    // "Print to console": {
    //  "prefix": "log",
    //  "body": [
    //      "console.log('$1');",
    //      "$2"
    //  ],
    //  "description": "Log output to console"
    // }
    "AJAX Get Method": {
        "prefix": "ajax-get",
        "body": [
            "$.ajax({",
            "\turl: '/',",
            "\tmethod: 'GET',",
            "\tdatatype: 'json',",
            "\tsuccess: (data) => {",
            "\t\tconsole.log(data);",
            "\t},",
            "\terror: (err) => {",
            "\t\tconsole.error(err);",
            "\t}",
            "});",
        ],
        "description": "A simple AJAX GET Method"
    }
}

Save the file and test if the snippet works fine by opening any file, in this case, open an empty .js file and type ajax-get and hit Tab at the end of the prefix. The generated snippet would look like this:

$.ajax({
    url: '/',
    method: 'GET',
    datatype: 'json',
    success: (data) => {
        console.log(data);
    },
    error: (err) => {
        console.error(err);
    }
});

Learn more about generating snippets by reading the documentation about it.

Hope this tip helps you out!

Using the null-coaleascing operator in C#

Null checks are the most underrated thing and despite years of experience, we still forget to write a null check in our code.

When it comes to writing null checks, this is what one would most write:

string foo = null;
string bar;
if(foo != null)
{
    bar = foo;
}
else
{
    bar = "Hello, friend!";
}
Console.WriteLine(bar) // Output: Hello, friend!

Yes, it does the job but it's a bit lengthy and not so readable. Let's see how the ?? operator i.e. the null-coalescing operator can help simplify your logic:

string foo = null;
string bar = foo ?? "Hello, friend!";
Console.WriteLine(bar) // Output: Hello, friend!

Now, you can bid goodbyes to writing verbose null checks and make your code more cleaner and readable.

Hope this tip helps you out!

Check if a string is a UNIQUEIDENTIFIER in SQL Server

Recently, when I was performing a data migration that contained previous payment history, I came across a table that had a column named PaymentId which is supposed to have a UUID styled format but funnily enough, it was kind of confusing because some of them had numbers in it.

Luckily, the ones that weren't in UUID format are not supposed to be migrated, so I decided to write a SELECT query and filter the records using the TRY_CONVERT function:

SELECT 
    * 
FROM 
    dbo.YourTable 
WHERE 
    TRY_CONVERT(UNIQUEIDENTIFIER, YourColumn) IS NOT NULL;

Yes, it's that simple. You don't need to write any complex regular expressions for such operations.

Hope you found this tip useful!

Filtering results by weekdays in SQL Server

Ever wanted to know how many people are actively posting something on a Monday? Or maybe during the weekends?

You can do that by using the DATEPART and DATENAME functions. Let me show you how they work.

Using DATEPART

The DATEPART function represents weekdays as integers from 1 (Sunday) to 7 (Saturday).

Let's say that you wanted to filter between Monday and Friday, you can achieve something like this:

SELECT * FROM dbo.YourTable WHERE DATEPART(WEEKDAY, YourDateColumn) BETWEEN 2 and 6;

One thing to note, in SQL Server, Sunday is the first day of the week. So, if you wanted to set the first day of the week, you can write the following statement before executing your query:

SET DATEFIRST 1;  -- Set Monday as the first day of the week
SELECT * FROM dbo.YourTable WHERE DATEPART(WEEKDAY, YourDateColumn) BETWEEN 1 and 5;

In the example above, you'll achieve the same results as the previous one.

Using DATENAME

The DATENAME function represents weekdays as a string from Monday to Sunday.

Let's replicate the previous example using this function:

SELECT * FROM dbo.YourTable WHERE DATENAME(WEEKDAY, YourDateColumn) IN ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday');

If you want to know more about filtering your records effectively using these methods, you can the read the documentation:

Hope you found this tip useful!

Don't forget what sparked your passion

A short letter to myself and other fellow programmers.

Do you ever remember falling in love with programming when learning to code during school days or while working on a client's project? Probably not.

When I started learning to code, I never wrote code because I wanted to but because I desired to. No one to poke me around with tight deadlines, no need to race against time. Just the feeling of power and creativity in your hands to build something out of nothing but just pure code. It could have been anything as simple as building a small widget for my blog post, writing a game, solving a problem in LeetCode or Project Euler or maybe writing a bash script to automate my tasks.

You could probably relate to some but for me, these are some of the examples that resonate with me.

I'm writing this as a reminder to myself and other fellow programmers: don't ever forget that moment when your passion for programming had sparked. You might be older now and you're probably filled with a lot of responsibilities (especially if you're parenting).

Perhaps, you might never even experience that same amount of adrenaline and rush that you had when you were behind the computer in your room while your siblings and friends were trying to distract you.

There's never an end to learning. You can still find some passion projects that'll help you keep that spark. Have an open mind and be open to newer ideas. Solve a puzzle, read a book, write a game or maybe a blog post.

Don't do this because you need to impress someone or for the sake of it rather because you can and it brings you joy.

Oh and wishing you a Happy New Year! ๐Ÿ˜€๐ŸŽ„

Configure XDebug in Visual Studio Code

For the past 6 months, I've been writing code in C# 11 and .NET 7 Core and of course, using the amazing Visual Studio 2022 IDE was quite an amazing experience especially when it comes to debugging your code.

Recently, I had to switch back to classic ol' PHP and now, I wanted a similar debugging experience in Visual Studio Code.

Using XDebug and if configured correctly, it'll be quite useful in your debugging journey instead of using var_dump() or dd() your variables everytime.

Prerequisites

The only thing you need here is Visual Studio Code and ensure that it's the latest version. I'm writing this from a Windows Machine using the latest version of WAMP Server.

1. Download PHP Debug

The official XDebug team has released an extension that could be installed in your editor. So, here you go, first download it.

2. Install XDebug

In the previous step, we just installed the extension for the editor but that's just an adapter used between the editor and XDebug. Whereas, XDebug is a PHP extension that needs to be installed on your local system.

I'm not going to show you the installation process as this documentation explains that well enough and you can follow it based on your operating system of choice.

3. PHP Configuration

After you're done with installing the extension, open your php.ini file and add the following line in this file:

zend_extension=path/to/xdebug

Now, it's time to enable remote debugging. Depending on your XDebug version:

For XDebug v3.x.x:

xdebug.mode = debug
xdebug.start_with_request = yes

For XDebug v2.x.x:

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_port = 9000

If you are looking for more specific options, please see read the documentation on remote debugging. Please note that the default port in XDebug v3.x.x has been changed from 9000 to 9003.

Once done, restart your PHP service and check if the configuration has taken effect. You can do this by creating a simple test.php with a phpinfo(); statement in there and look for your XDebug extension under the Loaded Configuration File section.

4. Visual Studio Code Configuration

In your editor, hold CTRL + SHIFT + P and type "Debug: Add Configuration" and it'll prompt you to select your language of preference and this case, of course, you should "PHP".

Upon selecting, it'll automatically generate a launch.json file in your .vscode folder in your project folder.

There'll be three different configurations in your file:

  • Listen for XDebug: This setting will simply start listening on the specified port (by default 9003) for XDebug.
  • Launch currently open script: It will launch the currently opened script as a CLI, show all stdout/stderr output in the debug console and end the debug session once the script exits.
  • Launch Built-in web server: This configuration starts the PHP built-in web server on a random port and opens the browser with the serverReadyAction directive.

Now, to test if it works, hit F5 and add a breakpoint in your PHP code and if it lands on your breakpoint, that means it works fine.

Hope you found this article useful!

References