*ฅ^•ﻌ•^ฅ* ✨✨  HWisnu's blog  ✨✨ о ฅ^•ﻌ•^ฅ

Importance of Type-safety

Introduction

Last week I decided to migrate all of my programming workflow into Linux system instead of Windows which I have been using for several decades.

However in the process, I encountered some road-blocks especially due to differences in spreadsheet processor to process data from another team. To cut the story short: some of the column types were changed due to the different spreadsheet processor and that means different datatypes --> this is where Python is particularly vulnerable.

What is Type-Safety?

Type-safety is a fundamental concept in programming that ensures the integrity and reliability of software applications. It prevents type-related errors at compile-time, rather than runtime, by ensuring that variables, functions, and data structures are used with the correct data types.

Type-safety is achieved through type checking, which verifies that the data types of variables, function arguments, and return values match the expected types.

Why is Type-Safety Important?

Type-safety is essential as it ensures that a program behaves as expected, prevents security vulnerabilities, makes code easier to understand and maintain, and improves performance by preventing unnecessary type conversions.

  1. Reliability: Type-safety ensures that a program behaves as expected, reducing the likelihood of errors and crashes.
  2. Security: Type-safety prevents security vulnerabilities, such as buffer overflows and null pointer dereferences, which can be exploited by attackers.
  3. Maintainability: Type-safety makes it easier to understand and maintain code, as the type system provides a clear and concise way to express the intended behavior of a program.
  4. Performance: Type-safety can improve performance by preventing unnecessary type conversions and reducing the likelihood of errors.

Why Python Falls Short

Python is dynamically-typed, which means it does not enforce type-safety at compile-time. This leads to type-related errors and security vulnerabilities. Python's dynamic typing allows for flexibility but also means that type-related errors are not caught until runtime, making it harder to detect and fix issues.

Consequences of Python's Lack of Type-Safety

Python's lack of type-safety has several consequences:

  1. Debugging Challenges: Debugging type-related errors in Python can be challenging, as the errors are not caught until runtime.
  2. Security Vulnerabilities: Python's lack of type-safety makes it more vulnerable to security threats, such as buffer overflows and null pointer dereferences.
  3. Code Quality: Python's lack of type-safety can lead to lower code quality, as developers may not be aware of type-related errors until runtime.
  4. Maintenance Challenges: Python's lack of type-safety makes it harder to maintain and refactor code, as type-related errors can be difficult to detect and fix.

My Case: Type Mismatches

I encountered a problem with some of the data columns, which contained percentage signs (%). This wasn't an issue in Windows, as Excel successfully converted the data without any problems. However, in the Linux system, none of the spreadsheet processors I tried (LibreOffice, SoftMaker Office, Only Office, WPS Office) were able to convert the % sign correctly.

When I read the data using the Pandas library, data with % sign was being interpreted as an Object or string data type, which was incorrect. It should have been read as a float. This caused significant issues with the number crunching in my finance-investment app, leading to a complete failure of the caching system and overwriting of the database with blank tables.

The user-facing app was also filled with blank data, making it unusable. This was the worst bug I've encountered so far. It was awful!

Solution

To resolve this issue, I had to change the column types in LibreOffice to decimal numbers for the data with % signs. What was surprising was that this had to be done specifically in LibreOffice. Using other spreadsheet processors like SoftMaker Office resulted in another problem: instead of being read as floats by Pandas, the data was read as integers, leading to incorrect calculation outputs.

Key takeaways

Type-safety is crucial for programming, and Python's lack of type-safety makes it more vulnerable to errors, security threats, and maintenance challenges. While Python's dynamic typing provides flexibility, it also means that type-related errors are not caught until runtime, making it harder to detect and fix issues.

#dynamic type #programming #python #static type #type safety