Function only returns unchanged parameter, useless?

I just found this function in the project I’m working at:

  -- Just returns the text unchanged.
  -- Note: <text> may be nil, function must return nil in that case!
  function Widget:wtr(text)
      return text
  end

Too sad, the coder does not work in the company anymore. Why would one make a function that does nothing, but returns the parameter it’s called with?

Is there any use to such a function, not specified to this example, but overall in any case?

Due to

function aFunction(parameter)
    return parameter
end

Ends in

aFunction(parameter) == parameter

Why would I write something like

aFunction(parameter) == whatIWantToCheck

instead of

parameter == whatIWantToCheck

?

12

Your question is sort of like asking what’s the good of the number zero if whenever you add it to something you get the same value back. An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a result. That’s why most functional programming languages have an id or identity in their standard library.

Put another way, it makes a handy default function. Just like you might want to set offset = 0 as a default integer, even though that makes an offset do nothing, it might come in handy to be able to set filterFunction = identity as a default function, even though that makes the filter do nothing.

5

Sure. Imagine an external API that allows you to retrieve things from somewhere that you absolutely need, but you have to specify filter criteria and output formatters for every query, even if you want all the results and want to get them exactly as stored.

Then you’d have to invent a trivial “accept everything” and a trivial “return unchanged” function to get at the data. wtr is precisely such a trivial pseudo-formatter. If you use that API a lot, it can be very useful to have this pseudo-helper function lying around rather than have to create an anonymous function whenever you query something. (You might consider calling it identity rather than wtr so that it’s immediately clear it does nothing.)

7

Isn’t that just a polymorphysm?

From my experience with Qt tr() and wtr() methods are supposed to be used (there, in Qt) in localization of Widget‘s text.

So, by returning unchanged text this method just says: Widget does no localization (translation of the text) by default. Override this function to enable your own logic.

1

A very common case for this is ‘later added functionality’. So the programmer thought that there could be some change to that function later. Since you access it as a function and not as the parameter itself, you can easily globally change this. Lets say you have an index with:

function getIndex(index)
    return index
end

and since your index starts at 0 this is all fine. You later change a component somewhere else, this component needs you to translate your index to start at 1. You would have to add hundreds of index+1 everywhere in your code. But with a method like this, simply say:

 function getIndex(index)
    return index+1
end

and you are fine. Functions like this can quickly lead to over-engineering but at some points they make a lot of sense!

1

Stub functions like this are common in type-inheritance situations. The base class might do nothing useful, but derived classes might do various things with the inputs. Declaring the stub function in the base class allows all derived classes to have a function of that name, and individual derived classes to override it with something more elaborate and useful.

One additional scenario is similar to the use in python and C# of properties, but in languages that don’t have the feature.

In general, you can declare something as just a member of the class; let’s say, ‘name’ as a string. This means that you access it like this:

someobject.name

Later, you decide that the name really needs to be dependent on what’s in the object. So it should really be a function. Oops! Even with no arguments, that means all code that accesses it now has to be changed to

someobject.name()

But if you have a lot of instances of this call, the chances of nothing going wrong with that is pretty low – somewhere this change won’t be made, the program will crash when you get to that point, etc.

Properties in C#/Python allow you to substitute a function in for what used to be an attribute, while still allowing it to be accessed like an attribute i.e. with no change. You don’t even have to have planned ahead.

If your language doesn’t have that, you have to plan ahead, but you can still sort of support it the way people did before by making it a function from the beginning that doesn’t do anything. In the beginning, it won’t do anything interesting, and look like it should be removed, and many functions of this type are never changed. But later, if you realize that really whenever you call Widget.wtr you need to strip some special characters out, it’s no big deal – it’s already a function, you just add that in and you’re done.

TL;DR This could just be a wise programmer planning ahead for future changes.

3

It’s not useless, actually it can be extremely useful, because it makes your code more uniform and flexible.

Say, for example, that you have a listing of people and a dropdown for the user to select if we wants to see “all” or just those that are “male” or “female”.

You can either handle “all” as a special case, in which case you’ll need an extra if and to check for this case explicitly, or you can have a filter function that just returns its parameter (e.g allows everything to pass), assigned to be used for the “all” case.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Function only returns unchanged parameter, useless?

I just found this function in the project I’m working at:

  -- Just returns the text unchanged.
  -- Note: <text> may be nil, function must return nil in that case!
  function Widget:wtr(text)
      return text
  end

Too sad, the coder does not work in the company anymore. Why would one make a function that does nothing, but returns the parameter it’s called with?

Is there any use to such a function, not specified to this example, but overall in any case?

Due to

function aFunction(parameter)
    return parameter
end

Ends in

aFunction(parameter) == parameter

Why would I write something like

aFunction(parameter) == whatIWantToCheck

instead of

parameter == whatIWantToCheck

?

12

Your question is sort of like asking what’s the good of the number zero if whenever you add it to something you get the same value back. An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a result. That’s why most functional programming languages have an id or identity in their standard library.

Put another way, it makes a handy default function. Just like you might want to set offset = 0 as a default integer, even though that makes an offset do nothing, it might come in handy to be able to set filterFunction = identity as a default function, even though that makes the filter do nothing.

5

Sure. Imagine an external API that allows you to retrieve things from somewhere that you absolutely need, but you have to specify filter criteria and output formatters for every query, even if you want all the results and want to get them exactly as stored.

Then you’d have to invent a trivial “accept everything” and a trivial “return unchanged” function to get at the data. wtr is precisely such a trivial pseudo-formatter. If you use that API a lot, it can be very useful to have this pseudo-helper function lying around rather than have to create an anonymous function whenever you query something. (You might consider calling it identity rather than wtr so that it’s immediately clear it does nothing.)

7

Isn’t that just a polymorphysm?

From my experience with Qt tr() and wtr() methods are supposed to be used (there, in Qt) in localization of Widget‘s text.

So, by returning unchanged text this method just says: Widget does no localization (translation of the text) by default. Override this function to enable your own logic.

1

A very common case for this is ‘later added functionality’. So the programmer thought that there could be some change to that function later. Since you access it as a function and not as the parameter itself, you can easily globally change this. Lets say you have an index with:

function getIndex(index)
    return index
end

and since your index starts at 0 this is all fine. You later change a component somewhere else, this component needs you to translate your index to start at 1. You would have to add hundreds of index+1 everywhere in your code. But with a method like this, simply say:

 function getIndex(index)
    return index+1
end

and you are fine. Functions like this can quickly lead to over-engineering but at some points they make a lot of sense!

1

Stub functions like this are common in type-inheritance situations. The base class might do nothing useful, but derived classes might do various things with the inputs. Declaring the stub function in the base class allows all derived classes to have a function of that name, and individual derived classes to override it with something more elaborate and useful.

One additional scenario is similar to the use in python and C# of properties, but in languages that don’t have the feature.

In general, you can declare something as just a member of the class; let’s say, ‘name’ as a string. This means that you access it like this:

someobject.name

Later, you decide that the name really needs to be dependent on what’s in the object. So it should really be a function. Oops! Even with no arguments, that means all code that accesses it now has to be changed to

someobject.name()

But if you have a lot of instances of this call, the chances of nothing going wrong with that is pretty low – somewhere this change won’t be made, the program will crash when you get to that point, etc.

Properties in C#/Python allow you to substitute a function in for what used to be an attribute, while still allowing it to be accessed like an attribute i.e. with no change. You don’t even have to have planned ahead.

If your language doesn’t have that, you have to plan ahead, but you can still sort of support it the way people did before by making it a function from the beginning that doesn’t do anything. In the beginning, it won’t do anything interesting, and look like it should be removed, and many functions of this type are never changed. But later, if you realize that really whenever you call Widget.wtr you need to strip some special characters out, it’s no big deal – it’s already a function, you just add that in and you’re done.

TL;DR This could just be a wise programmer planning ahead for future changes.

3

It’s not useless, actually it can be extremely useful, because it makes your code more uniform and flexible.

Say, for example, that you have a listing of people and a dropdown for the user to select if we wants to see “all” or just those that are “male” or “female”.

You can either handle “all” as a special case, in which case you’ll need an extra if and to check for this case explicitly, or you can have a filter function that just returns its parameter (e.g allows everything to pass), assigned to be used for the “all” case.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Function only returns unchanged parameter, useless?

I just found this function in the project I’m working at:

  -- Just returns the text unchanged.
  -- Note: <text> may be nil, function must return nil in that case!
  function Widget:wtr(text)
      return text
  end

Too sad, the coder does not work in the company anymore. Why would one make a function that does nothing, but returns the parameter it’s called with?

Is there any use to such a function, not specified to this example, but overall in any case?

Due to

function aFunction(parameter)
    return parameter
end

Ends in

aFunction(parameter) == parameter

Why would I write something like

aFunction(parameter) == whatIWantToCheck

instead of

parameter == whatIWantToCheck

?

12

Your question is sort of like asking what’s the good of the number zero if whenever you add it to something you get the same value back. An identity function is like the zero for functions. Kind of useless by itself, but occasionally useful as part of an expression using higher-order functions, where you can take a function as a parameter or return it as a result. That’s why most functional programming languages have an id or identity in their standard library.

Put another way, it makes a handy default function. Just like you might want to set offset = 0 as a default integer, even though that makes an offset do nothing, it might come in handy to be able to set filterFunction = identity as a default function, even though that makes the filter do nothing.

5

Sure. Imagine an external API that allows you to retrieve things from somewhere that you absolutely need, but you have to specify filter criteria and output formatters for every query, even if you want all the results and want to get them exactly as stored.

Then you’d have to invent a trivial “accept everything” and a trivial “return unchanged” function to get at the data. wtr is precisely such a trivial pseudo-formatter. If you use that API a lot, it can be very useful to have this pseudo-helper function lying around rather than have to create an anonymous function whenever you query something. (You might consider calling it identity rather than wtr so that it’s immediately clear it does nothing.)

7

Isn’t that just a polymorphysm?

From my experience with Qt tr() and wtr() methods are supposed to be used (there, in Qt) in localization of Widget‘s text.

So, by returning unchanged text this method just says: Widget does no localization (translation of the text) by default. Override this function to enable your own logic.

1

A very common case for this is ‘later added functionality’. So the programmer thought that there could be some change to that function later. Since you access it as a function and not as the parameter itself, you can easily globally change this. Lets say you have an index with:

function getIndex(index)
    return index
end

and since your index starts at 0 this is all fine. You later change a component somewhere else, this component needs you to translate your index to start at 1. You would have to add hundreds of index+1 everywhere in your code. But with a method like this, simply say:

 function getIndex(index)
    return index+1
end

and you are fine. Functions like this can quickly lead to over-engineering but at some points they make a lot of sense!

1

Stub functions like this are common in type-inheritance situations. The base class might do nothing useful, but derived classes might do various things with the inputs. Declaring the stub function in the base class allows all derived classes to have a function of that name, and individual derived classes to override it with something more elaborate and useful.

One additional scenario is similar to the use in python and C# of properties, but in languages that don’t have the feature.

In general, you can declare something as just a member of the class; let’s say, ‘name’ as a string. This means that you access it like this:

someobject.name

Later, you decide that the name really needs to be dependent on what’s in the object. So it should really be a function. Oops! Even with no arguments, that means all code that accesses it now has to be changed to

someobject.name()

But if you have a lot of instances of this call, the chances of nothing going wrong with that is pretty low – somewhere this change won’t be made, the program will crash when you get to that point, etc.

Properties in C#/Python allow you to substitute a function in for what used to be an attribute, while still allowing it to be accessed like an attribute i.e. with no change. You don’t even have to have planned ahead.

If your language doesn’t have that, you have to plan ahead, but you can still sort of support it the way people did before by making it a function from the beginning that doesn’t do anything. In the beginning, it won’t do anything interesting, and look like it should be removed, and many functions of this type are never changed. But later, if you realize that really whenever you call Widget.wtr you need to strip some special characters out, it’s no big deal – it’s already a function, you just add that in and you’re done.

TL;DR This could just be a wise programmer planning ahead for future changes.

3

It’s not useless, actually it can be extremely useful, because it makes your code more uniform and flexible.

Say, for example, that you have a listing of people and a dropdown for the user to select if we wants to see “all” or just those that are “male” or “female”.

You can either handle “all” as a special case, in which case you’ll need an extra if and to check for this case explicitly, or you can have a filter function that just returns its parameter (e.g allows everything to pass), assigned to be used for the “all” case.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật