Sometimes. And telling when it’s stupid is important to being a good coder, in my opinion.
Maybe I’m wrong. I don’t have a degree yet. But I know that when I write a Python script that I want to rotate the screen on my tablet, I’m not worrying about extensibility or modularity, I just want a small procedural script that does this, this, and this, and then finishes. The faster the better, both in writing the script and in the script’s execution.
So, in beginning work on JORSnix, I decided I’d get my tablet working perfectly first, as a system to model it on. Part of this made rewriting my tablet.rb script that rotated my tablet between landscape and portrait mode in Python. I spent about 20 minutes on it, not counting the time I spent learning that the os module was deprecated and thus learning how to use the subprocess module.
Anyway, the finished product is this:
#!/usr/bin/python
from subprocess import *
import re
p1 = Popen(["xrandr"],stdout=PIPE)
p2 = Popen(["grep", "current"], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
current = output[output.find("current "):output.find(", max")]
pattern = re.compile('[0-9]+')
resolution = pattern.findall(current)
if resolution[0] > resolution[1] : #landscape
Popen("./xrotate"+" 3", shell=True)
Popen("cellwriter &",shell=True)
Popen("easystroke &",shell=True)
else : #portrait
Popen("./xrotate"+" 0", shell=True)
Popen("pkill cellwriter && pkill easystroke", shell=True)
As you can see, it's pretty simple and straightforward. It's no piece of art, but it does what it needs to do. No more, no less. And it will work on any single-monitored tablet, no matter the resolution, for swapping the screen from portrait to landscape and back.
If it needs to be turned into a couple functions so I can use it in a class, that would work. But that's not what the plan is, at least not yet. But I was told by a friend on IRC that it was unreadable, and wrong simply because my code isn't "modular and extensible" without first examining why it would need to be extended upon. He gave this as the "correct version:"
#!/usr/bin/python
from subprocess import Popen,PIPE
def bash(c):
if c.startswith('cd '):
chdir(''.join(list(c)[3:]))
else:
return Popen(c, shell=True, stdout=PIPE).stdout.read().strip()
class Display():
def getCurrentResolution(self):
res = bash("xrandr")
res = res[res.find("current ")+8:res.find(", max")].replace(' ','')
return (res[:res.find('x')],res[res.find('x')+1:])
def rotate(self):
res = getCurrentResolution()
if res[0] > res[1]: # Landscape Mode
self.xrotate(3);
bash('cellwriter &')
bash('easystroke &')
else: # Portrait Mode
self.xrotate(0)
bash('killall cellwriter')
bash('killall easystroke')
def xrotate(self, mode):
# unknownmosquito can fill it in from here
class Wacom(Display):
def getStylusMode(self):
return bash('xsetwacom get stylus Mode')
display = Wacom()
display.rotate()
Besides going into the fact that the "better version" would no longer use regular expressions (and thus would be less resilient to oddly shaped displays), it aliases Popen to bash for "readability." Overall, being accused of using procedural programming for a systems script in a language like Python (which allows you to do this for a reason), and to have this given to me as a "correction," just rubs me the wrong way.
Especially when I'm told that it needs to be done like this to make it more "readable."
Object Oriented Programming is wonderful. Unless using it is just wasting time.
Note: If it becomes necessary to integrate this script into a larger part, obviously it will get OOP'd. But for now, it'll remain a script.


5 comments
Comments feed for this article
June 14, 2009 at 7:39 pm
Anonymous
You have to admit, even if it didn’t require object-oriented features, that code isn’t the easiest to read! I just hope you don’t write all code like that. Good luck with JORSnix.
June 14, 2009 at 8:02 pm
unknownmosquito
I usually comment more, as I like to make my code succinct and explain in comments.
Also, you read this before I was done (I accidentally clicked Publish long before I was ready). Just saying.
June 16, 2009 at 10:10 pm
Eric Lifka
I agree with you, OO programming is a useful paradigm, but most people seem to think entirely in OO. In languages like Java I guess that’s fine, since you can’t program any other way, but most languages provide it as a feature, not a requirement, and forcing absolutely everything into an object model is absurd. Most languages provide objects as a feature, not a requirement, and as you said, there’s a reason for that. Just because OO is more recent than procedural does not make it absolutely better. I will concede that OO can make huge projects more understandable because everything is modularized and self contained, but the same can be done in C (or any other non OO language) quite easily. But I would brush it off, the close mindedness of other programmers can only lead to your potential gain. (Well, that’s not entirely true, but you can certainly play off that in certain situations)
-Eric Lifka
September 26, 2009 at 4:56 am
Patrick Rutkowski
Adding comments, as you mentioned, definitely would have made the procedural version readable. Though I shouldn’t restrict that suggestion to the procedural version.
Given what you already think about OOP, I wonder if you would take another step in that direction and additionally consider the opinions which I recently voiced here:
http://www.rutski89.com/dynamic/journal.html?name=no_exceptions
-Patrick
P.S.
Have you considered posting an E-Mail / AIM Nick / IRC Handle for readers to contact you at?
Also, your comment on degree caught my eye:
“Maybe I’m wrong. I don’t have a degree yet…”
Having intelligence and experience so hugely outweighs the value of having an undergrad degree that it makes me wince whenever I see someone, such as yourself, anticipating attack due to lacking one.
Presumably, earning a degree is itself a process of accruing experience, and also of proving that one has intelligence. But I’ll leave that argument for other places.
September 26, 2009 at 11:00 am
unknownmosquito
Patrick,
First of all, comments go right to my email, so I get them quickly :P
and I’m trying to keep this blog largely anonymous so that I don’t have to worry about sticking my foot in my mouth, although if you notice my screenname happening elsewhere on the Internet it is most likely me. I’ve never signed up for anything and had ‘unknownmosquito’ be taken.
Now then, on topic:
I read your post and I certainly agree with you. And I think you’re griping along the same vein as me: there are tools for certain situations, and they are best used in the appropriate situations, not just blindly used every time you sit down to write some code.