![]() In programming, there is a commonly used principal called Don't Repeat Yourself(DRY). Also, Instead of using name_list, condition for your parameters, I would use names, criterion Instead of sublist, which really doesn't convey much meaning to me, use filter_names_by_criteria() or even just filter_by_criteria(). I should just be able to read your function name and have a good idea of what your function is suppose to accomplish. When creating names for functions give them meaningful names. Now is a good time to suggest a better name for your function. It also adds the benefit of making your code more explicit. If you make use of elif statements instead, as soon as one elif statement is true, Python can jump straight to the return statement in your function. When Python is reading your source code, the virtual machine which runs Python must check every if statement to test its validity, which takes time. ![]() Use elif statements instead of multiple if statements. So if you wanted you could be more specfic, you could considered using it. Instead of return_list you should name it something such as filtered_names.Īlso while on the topic of naming, I should mention that criterion is the singular form for criteria. Name the variable for what it is representing. Don't name a variable for what data type it is, or what its suppose to do. ![]() for example, the list return_list in the function sublist() could be improved. While your naming conventions are consistent, and most of your variable names are good, a few could use some work. Naming conventions are consistent, and your names are clear and spelled correctly. Overall your code looks fairly clean and well written. I wrote this code to filter a list of names based on certain conditions, and wanted to see if I can reduce this further and eliminate redundancy in code.
0 Comments
Leave a Reply. |