A well known folk theorem in computer science is that any program can be transformed into a semantically equivalent program of one procedure containing one switch statement inside a while loop. The transformation involves rewriting the program so that each case of the switch is an elementary operation of the original program followed by an assignment to a program counter variable that communicates to the switch statement the next operation to execute. The while loop terminates when the program counter reaches the last operation in the program.
Well in production code nobody will opt for this style, but I think there might be a case where this could come in handy. Few times there is a use case where we have to generate some unique id based on the customer attributes. This could used as slug for pretty urls, pseudo-username etc. Recently I had to write a stored procedure (PL/SQL) in Oracle to generate pseudo-usernames for some million email accounts that we had, with usual rules of length and uniqueness. Instead of writing multiple if-else blocks I came up with something like this.

Without making a myriad of if-else nested conditions, I simply wrote a while with condition that checks if pseudo-username generated is within acceptable norms or not. IF yes then program terminates there returning the uniqId created. Else program continues to go on to case statement for next set of steps to generate a psuedo-username. Key thing to note here is that there is a counter maintain which actually determines the which case statement would be picked in each iteration.
Interesting read : This technique has also been used to produce obfuscated C code that produces popular "Twelve Days of Christmas" poem.
Well in production code nobody will opt for this style, but I think there might be a case where this could come in handy. Few times there is a use case where we have to generate some unique id based on the customer attributes. This could used as slug for pretty urls, pseudo-username etc. Recently I had to write a stored procedure (PL/SQL) in Oracle to generate pseudo-usernames for some million email accounts that we had, with usual rules of length and uniqueness. Instead of writing multiple if-else blocks I came up with something like this.

Without making a myriad of if-else nested conditions, I simply wrote a while with condition that checks if pseudo-username generated is within acceptable norms or not. IF yes then program terminates there returning the uniqId created. Else program continues to go on to case statement for next set of steps to generate a psuedo-username. Key thing to note here is that there is a counter maintain which actually determines the which case statement would be picked in each iteration.
Interesting read : This technique has also been used to produce obfuscated C code that produces popular "Twelve Days of Christmas" poem.
The article presents an interesting application of a theoretical programming concept that would rarely be chosen as a general production coding style. Using a program counter with a while loop and switch statement provides a structured way to move through different operations until the generated pseudo-username satisfies the required conditions.
ReplyDeleteThe practical example makes the idea much more meaningful because the goal is not simply to demonstrate an unusual control-flow transformation, but to generate unique identifiers from customer attributes while handling rules such as length and uniqueness. This makes the discussion particularly relevant to Programming Courses, especially when studying control flow, algorithms, and alternative ways of structuring program logic.
I also found the use of a counter to determine which case executes in each iteration interesting. Instead of maintaining a large collection of nested if-else conditions, each case represents another step in the identifier-generation process, while the loop determines whether another step is necessary. This type of server-side logic is relevant to Backend Training, where application logic often has to process and validate data systematically.
ReplyDelete