Naming convention minutiae

Joined
Sep 20, 2022
Messages
348
Reaction score
42
What would you do if you had to name something that has no word in the English language?

Lets say my first function takes a string as an argument, and returns a string. What it does is replaces all \ with \\ and all newlines with \n.

My second function does the reverse. The purpose of the functions is to allow me to store multi-line text on a single line within a text file.

I have named them encode_esc and decode_esc, and that is ugly.

One school of thought suggests naming a function according to what it returns. There might be a word for this.

Times like this, I consider making a book of every programming concept I know, and giving them numbers.
 
Joined
Sep 20, 2022
Messages
348
Reaction score
42
I don't think the number idea is a good one, that's what astronomers do (M33), and though I like the style, the readability is poor.

Inventing a new word is tempting.

escit = a string with escape sequences encoded in it

escitae : plural

antiescit : opposite

The readability of that isn't great either.

Or perhaps using an old word in a new way, like "percolate up" and "percolate down" in the context of heaps.
 
Joined
Sep 20, 2022
Messages
348
Reaction score
42
According to gargle, programmers call strings that contain ASCII control characters, raw.

That doesn't ring a bell, but at least its short.

raw(
cooked(

raw_to_cooked(
cooked_to_raw(

y=cooked(thawed(frozen(x)))

To me, raw just means unprocessed, but I'll take it.
Code:
raw_to_escaped(
escaped_to_raw(
Job done, moving on.
 
Joined
Jul 12, 2020
Messages
100
Reaction score
11
create a single function named "convert_str" with the required parameter.
convert_str( option )

inside the function simply place a 'switch statement' to change the option from one to the other and return the string.
 
Joined
Sep 20, 2022
Messages
348
Reaction score
42
create a single function named "convert_str" with the required parameter.
convert_str( option )

inside the function simply place a 'switch statement' to change the option from one to the other and return the string.
Not a bad idea. A nice bit of lateral thinking.

I did something similar when I wanted to use a symbol in a name, and the language didn't allow it.
Code:
=calc(a,b,"+")
=calc(a,b,"-")
=calc(a,b,"*")
=calc(a,b,"/")
I could have used 4 functions, but this looked better. Calc dispatched to subroutines, and the mainline didn't need to know what I named them.
 
Joined
Jul 12, 2020
Messages
100
Reaction score
11
not sure what language you're using but applying a switch allows multiple options mot just character switches.
Code:
convert_str(o, s){
  if(  (o || s)  == ( 'undefined' || undefined) ){ return; }
  switch ( o ){
    case 'encode' : { str = str [operation] }
    case 'decode'  : { str = str [operation] }
   case 'uppercase'  : { str = str.toUpperCase(); }
   case 'lowercase'  : { str = str.toLowerCase(); }
   case 'calc_length' : { str = str.length; }
  }
  return str;
}
 
Joined
Sep 20, 2022
Messages
348
Reaction score
42
not sure what language you're using but applying a switch allows multiple options mot just character switches.
Code:
convert_str(o, s){
  if(  (o || s)  == ( 'undefined' || undefined) ){ return; }
  switch ( o ){
    case 'encode' : { str = str [operation] }
    case 'decode'  : { str = str [operation] }
   case 'uppercase'  : { str = str.toUpperCase(); }
   case 'lowercase'  : { str = str.toLowerCase(); }
   case 'calc_length' : { str = str.length; }
  }
  return str;
}
That is like the way I implement object oriented style in a language that doesn't support objects.

One argument is the name of the method to apply.

Which just moves the naming problem to the other side of the parenthesis.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top