Pages

Friday, April 28, 2023

When adding technical debt can be relevant?

 

Technical debt refers to the accumulation of work that needs to be done in a software project due to taking shortcuts or implementing quick fixes that prioritize short-term gains over long-term maintainability.

 Technical debt can be relevant in certain situations where it is necessary to prioritize short-term goals over long-term goals. Here are a few examples:

  • Meeting a deadline: Sometimes a project has a hard deadline that must be met, and there isn't enough time to do everything perfectly. In this case, the team may choose to take on some technical debt in order to deliver the project on time.
  • Proof of concept: In the early stages of a project, it may be necessary to build a proof of concept or prototype quickly to demonstrate feasibility. Technical debt may be incurred in this stage, with the expectation that it will be addressed later if the project moves forward.
  • Emergencies: In emergency situations where a system failure or security breach has occurred, the team may need to take quick action to restore functionality or protect sensitive information. Technical debt may be incurred in this situation, with the expectation that it will be addressed as soon as possible.

.Net Cron Validation

 

The code you shared is a C# implementation of a helper class for validating cron expressions. Cron expressions are used to schedule tasks to run at specific times or intervals, and are commonly used in Unix-based operating systems.

The CronHelper class provides a static method IsValidCron() which takes a cron expression as input and returns a boolean indicating whether the expression is valid. The class uses an array of tuples cronPartIntervals to define the valid range of values for each part of the cron expression (minute, hour, day of month, month, and day of week).

The IsValidCronPart() method is used to validate each part of the cron expression individually. It supports single values, ranges, and step values (e.g. "*/5" to run every 5 minutes).

By using this helper class, you can ensure that the cron expressions provided by users are valid before using them to schedule tasks, which can help prevent errors and improve the reliability of your application.

using System;
using System.Text.RegularExpressions;

namespace AppCore.Cron
{
    public static class CronHelper
    {
        readonly static Tuple<int, int>[] cronPartIntervals = new Tuple<int, int>[5]
        {
            new Tuple<int, int>(0, 59), //minute
            new Tuple<int, int>(0, 23), //hour
            new Tuple<int, int>(1, 31), //day of month
            new Tuple<int, int>(1, 12), //month
            new Tuple<int, int>(0, 6)   //day of week
        };

        public static bool IsValidCron(string cron)
        {
            if (string.IsNullOrEmpty(cron))
                return false;
           

            var parts = cron.Split(' ');
            if (parts.Length != cronPartIntervals.Length)
                return false;

            for (int i = 0; i < parts.Length; i++)
            {
                if (!IsValidCronPart(parts[i], cronPartIntervals[i].Item1, cronPartIntervals[i].Item2))
                    return false;
            }


            return true;
        }

        private static bool IsValidCronPart(string part, int minValue, int maxValue)
        {
            if (part == "*")
                return true;

            var values = part.Split(',');
            foreach (string value in values)
            {
                if (value.StartsWith("*/")) // step value
                {
                    if (!int.TryParse(value.Substring(2), out int step))
                        return false;

                    if (step <= 0 || step > maxValue || step < minValue)
                        return false;
                }
                else
                {
                    var range = value.Split('-');
                    if (range.Length == 2) // range value
                    {
                        if (!int.TryParse(range[0], out int start) || !int.TryParse(range[1], out int end))
                            return false;

                        if (start < minValue || start > maxValue || end < minValue || end > maxValue || start > end)
                            return false;
                    }
                    else // single value
                    {
                        if (!int.TryParse(value, out int single))
                            return false;

                        if (single < minValue || single > maxValue)
                            return false;
                    }
                }
            }

            return true;
        }
    }
}