Random programming things I'd want to remember

Wednesday, January 17, 2018

Visual Studio snippet for a TestMethod with a proper naming convention (naming convention was proposed by Roy Osherove)

Here it is, see this post for details on how to import it into your Visual Studio. Enjoy!

<?xml version="1.0" encoding="utf-8"?>
  <CodeSnippet Format="1.0.0">
      <Title>Test method with a naming convention</Title>
      <Author>Roy Osherove, yevgeller</Author>
      <Description>Test method with the following naming convention: method under testing, condition, and expected results. The naming convention was taken from Roy Osherove's book "The Art of Unit Testing"</Description>
          <ToolTip>Replace with the method that you want to test</ToolTip>
          <ToolTip>Condition that you want to test</ToolTip>
          <ToolTip>The result that you expect</ToolTip>
        <Code Language="csharp">
          public void $NameOfMethodUnderTest$_$ConditionYouAreTesting$_$ExpectedResult$()


Thursday, January 11, 2018

Timer to count up in JavaScript

Here is my implementation, nothing original or extraordinary. Putting it here for future reference.
        var totalSeconds = 0;
        var timer;

        function startTimer() {
            timer = setInterval(function () {
                var t = document.getElementById("time2"); //or other place where to display data
                t.innerHTML = displayTimer(totalSeconds);
            }, 1000); 

        function stopTimer() {

        function displayTimer(numberOfSeconds) {
            var result = "";

            var hours = Math.floor(numberOfSeconds / 3600);
            if (hours > 0) { result += hours + " h "; }

            var minutes = Math.floor((numberOfSeconds - (hours * 3600)) / 60);
            if (minutes > 0) { result += minutes + " m "; }

            result += (numberOfSeconds % 60 + " s");

            return result;

Tuesday, January 9, 2018

To remember: declaring an inline array for splitting strings

This is how to declare an inline char array (C#) to split the string into multiple parts:

List result = inputLine.Split(new[] {'%'}, StringSplitOptions.RemoveEmptyEntries).ToList();