Timeline for Is it always a best practice to write a function for anything that needs to repeat twice?
Current License: CC BY-SA 3.0
48 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 26, 2016 at 15:16 | comment | added | Ed Griebel | My rule of thumb, "write once, copy twice, refactor thrice". Not my original creation; I forget who said it, probably somewhere on Ward Christenson's wiki c2.com/cgi/wiki | |
Dec 29, 2015 at 0:15 | comment | added | Gaurav | Note that there are some cases in which greet(user) -- or even better, user.greet() -- make sense. What if some users need to be greeted in Hindi and others in Bengali? What if some users prefer a quick greeting at the top of the page, while others like a pop-up window warmly welcome them, while others hate all greetings? Isolating that code in a single place can be a huge help, but only if you think you'll need that isolation. If not, it's just a waste of space. | |
Nov 15, 2015 at 11:43 | comment | added | Thorbjørn Ravn Andersen | Introducing a function mean that you cannot just follow the flow of the code down the page, but you need to - just like the CPU - jump to the function code, run that, jump back to continue. It distrupts the reading flow and is the primary reason that method names needs to be good as it allows you to assume what the method does without actually deciphering it. | |
Nov 15, 2015 at 0:16 | history | edited | Aviv Cohn | CC BY-SA 3.0 |
Fixed minor English issues, made it read better.
|
May 28, 2015 at 19:57 | comment | added | John Hascall | Anything that will be repeated twice, will be repeated three times. | |
Mar 4, 2015 at 19:04 | comment | added | John R. Strohm | Actually, you want something like (mapc #'print name-list), and then something like (mapc (lambda (l) (mapc #'print l)) (list vip-list guest-list)). You can actually do things like this in C and C++, but it WILL baffle those of your coworkers who have never read the Wizard Book. | |
Mar 4, 2015 at 13:39 | comment | added | Panzercrisis |
But in cases where these aren't an issue, I'd generally go for at least three lines of code (or things like your for loops), because unless a lot of dots are coming into play or there's an if statement or something, two lines really do account for nothing.
|
|
Mar 4, 2015 at 13:39 | comment | added | Panzercrisis | My personal policy is three or more lines of code that get repeated, not two or more. And that's certainly not written in stone; as others have noted, you have to also be conscious of things like whether the repeated lines are actually all that related between the two spots they're used, whether the function can be named so that you don't have to read its definition to know what it does, etc. | |
Mar 4, 2015 at 13:26 | answer | added | user90766 | timeline score: 1 | |
Jan 30, 2015 at 11:30 | audit | First posts | |||
Jan 30, 2015 at 11:30 | |||||
Jan 23, 2015 at 5:31 | history | tweeted | twitter.com/#!/StackProgrammer/status/558497107922124801 | ||
Jan 20, 2015 at 6:52 | comment | added | Nathan Goings | I find myself programming from a readability standpoint rather than a "function by function" basis. You have to weigh the effort you spend now versus the effort you spend in the future. | |
Jan 18, 2015 at 12:19 | comment | added | nnnnnn | Anything that will be repeated twice, will be repeated three times. | |
Jan 16, 2015 at 22:25 | comment | added | Tulains Córdova | Anything that will repeated twice, will be repeated three times. | |
Jan 15, 2015 at 22:55 | comment | added | Samson | "If it happens once, it may not happen again. If it happens twice it's likely it will happen the 3rd time" (not sure who said this but you get the point) | |
Jan 15, 2015 at 20:12 | comment | added | Carsten S |
Your function should be called print_names , not print_name .
|
|
Jan 15, 2015 at 15:10 | answer | added | flamingpenguin | timeline score: 5 | |
Jan 15, 2015 at 14:39 | comment | added | s.m | @user16547 wait... do you always treat them as red flags? | |
Jan 15, 2015 at 8:06 | comment | added | Bakuriu | @OllieFord That creates a new list, so, during the iteration, you are using twice the memory. Surely you can use that if you know more or less how big the lists are going to be, but if you want a foolproof solution it's better to avoid the useless allocation of a new list. | |
Jan 15, 2015 at 4:41 | answer | added | Bill K | timeline score: 2 | |
Jan 14, 2015 at 23:23 | comment | added | Francisco Presencia | Say that now you want to translate those greetings. In which case it's easier? | |
Jan 14, 2015 at 22:14 | comment | added | OJFord |
@Bakuriu Or you could just do for name in vip_list+guest_list ..?
|
|
Jan 14, 2015 at 20:16 | comment | added | Radu Murzea | @user16547 is right: "always" and "never" should be avoided at all costs. Instead, the one I like most is "consistent" :) . | |
Jan 14, 2015 at 16:53 | answer | added | sdenham | timeline score: 1 | |
Jan 14, 2015 at 16:49 | comment | added | Nakilon | Python is all about 'always' and 'never'. And don't tell me about zen -- Python never followed it. | |
Jan 14, 2015 at 13:15 | comment | added | Ian | Also consider if you want "damp code" or "dry code" see stackoverflow.com/questions/6453235/… | |
Jan 14, 2015 at 8:38 | answer | added | user541686 | timeline score: 106 | |
Jan 14, 2015 at 4:40 | history | protected | gnat | ||
Jan 14, 2015 at 3:50 | comment | added | Zenon |
From the Zen of Python by Tim Peters: Special cases aren't special enough to break the rules. Although practicality beats purity.
|
|
Jan 13, 2015 at 23:51 | history | notice added | user28988 | Needs detailed answers | |
Jan 13, 2015 at 22:08 | answer | added | user52889 | timeline score: 4 | |
Jan 13, 2015 at 21:01 | comment | added | Brian | @user16547 We call those 'sithware developers'! | |
Jan 13, 2015 at 17:58 | answer | added | supercat | timeline score: 5 | |
Jan 13, 2015 at 16:22 | comment | added | Bakuriu |
In your last example you can do: from itertools import chain; for name in chain(vip_list, guest_list): print(name) .
|
|
Jan 13, 2015 at 15:30 | comment | added | async |
I treat always and never as red flags. There's a thing called "context", where the always and never rules, even if good in general, may not be that appropriate. Beware of software developers who deal in absolutes. ;)
|
|
Jan 13, 2015 at 13:53 | answer | added | Gere | timeline score: 9 | |
Jan 13, 2015 at 10:31 | answer | added | Svalorzen | timeline score: 25 | |
Jan 13, 2015 at 10:24 | answer | added | Simon Martin | timeline score: 22 | |
Jan 13, 2015 at 8:12 | answer | added | UldisK | timeline score: 3 | |
Jan 13, 2015 at 6:35 | review | Close votes | |||
Jan 23, 2015 at 3:01 | |||||
Jan 13, 2015 at 6:18 | comment | added | gnat | possible duplicate of Rule of thumb for cost vs. savings for code re-use | |
Jan 13, 2015 at 6:16 | answer | added | Macke | timeline score: 13 | |
Jan 13, 2015 at 5:47 | answer | added | Karl Bielefeldt | timeline score: 202 | |
S Jan 13, 2015 at 5:39 | history | suggested | Nathan Tuggy | CC BY-SA 3.0 |
Fixed grammar
|
Jan 13, 2015 at 5:05 | review | Suggested edits | |||
S Jan 13, 2015 at 5:39 | |||||
Jan 13, 2015 at 4:52 | answer | added | W.Walford | timeline score: 2 | |
Jan 13, 2015 at 4:41 | answer | added | Robert Harvey | timeline score: 60 | |
Jan 13, 2015 at 4:15 | history | asked | ZengJuchen | CC BY-SA 3.0 |