Stop validating serial numbers manually in C#

It is common to use numerical validations every time we are dealing with serial numbers and even numbers that we don’t want to be negative and in that context most of the programmers make a lot of mistakes implementing unnecessary functionality that ca…


This content originally appeared on DEV Community and was authored by DEV Community

It is common to use numerical validations every time we are dealing with serial numbers and even numbers that we don't want to be negative and in that context most of the programmers make a lot of mistakes implementing unnecessary functionality that can be circumvented with basic knowledge about the primitive type system.

In this article I will bring and explain three ways to perform the validation of this numeric type.

1. The worst way

Unfortunately, the way most used by developers of projects I've already got my hands on is just a manual validation usually followed by a throw or "StatusCode that takes an int or long as an argument.

[HttpGet]
public IActionResult GetUserInformation([FromRoute] long idUser)
{
    if(idUser < 0)
        return StatusCode(400, "idUser should be non-negative."}

    return Ok(_userService.GetById(idUser));
}

2. The suspicious way

This approach may be valid but it is not so interesting since it is about overengineering for something that can be solved in a much simpler way.

[HttpGet]
public IActionResult GetUser([FromRoute] UserRequest request)
{
    return Ok(_userService.GetById(request.Id));
}
using System.ComponentModel.DataAnnotations;

public class UserRequest 
{
    [Range(0, long.MaxValue)]
    public long Id { get; set; }
}

3. The perfect way

There is a primitive data type that is not widely used that was inherited from older languages like C and C++, namely ulong, uint or ushort accepting only positive values. In addition to simplifying the process, the use of this data type will significantly decrease the weight of operations when it comes to scheduling.

[HttpGet]
public IActionResult GetUser([FromRoute] ulong idUser)
{
    return Ok(_userService.GetById(request.Id));
}


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-01T23:53:03+00:00) Stop validating serial numbers manually in C#. Retrieved from https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/

MLA
" » Stop validating serial numbers manually in C#." DEV Community | Sciencx - Tuesday March 1, 2022, https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/
HARVARD
DEV Community | Sciencx Tuesday March 1, 2022 » Stop validating serial numbers manually in C#., viewed ,<https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/>
VANCOUVER
DEV Community | Sciencx - » Stop validating serial numbers manually in C#. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/
CHICAGO
" » Stop validating serial numbers manually in C#." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/
IEEE
" » Stop validating serial numbers manually in C#." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/. [Accessed: ]
rf:citation
» Stop validating serial numbers manually in C# | DEV Community | Sciencx | https://www.scien.cx/2022/03/01/stop-validating-serial-numbers-manually-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.