64
zoifed
7y

Working with a "senior frontend developer"

HIM: committed this code, and took him 2 weeks before his first commit on our project
var str = ''

if (string == 'String Here') {
str = 'stringhere'
} else if ( string == 'Another String Here') {
str = 'anotherstringhere'
} and so on ...

ME: WTF?
ME: can we do it like this?
ME: str = string.replace(/\s/g, '').toLowerCase()

Maybe he is senior by age :D

Comments
  • 27
    Some people don't mature, they just get older.
  • 3
    Well, use regex and you'd have two problems ;)

    Though I use them quite a bit myself.
  • 3
    @rusty-hacker What's the problem with regex? 😉
  • 1
    Yep, what's the problem with regex?
  • 1
    Should've mentioned that it was a quote from Jamie Zawinski

    Badicaly some coders use it incorreclty because it's not well understood. Also using it in could let edge cases to be missed. For instance parsing html ...

    As I said, I use quite a bit, in code, vim, awk, sed ...
  • 1
    @rusty-hacker Well if anybody wants to parse HTML with regex, then something is clearly going wrong with his/her thought process...
  • 2
    @qbasic16 Then something is clearly going wrong with my thought process. I think regex are great for any parsing. Haven't always liked regex though, used to find them cumberlicated [my own word for cumbersome/complicated]. But, the more I've learnt about them, the more I've become to like them. Or as Arnie would put it: "The mooawr I luearn, the mooawr *in*hyooman I becooawm".
  • 1
    @TerriToniAX I see your point and in most languages this makes absolutely sense. The problem with HTML is, that too many things can be in the code which are too complicated for regex. Like embedded PHP, JS code in script blocks containing HTML tags as string, iframe stuff, ...
    If you master it with regex only I will personally handcraft you a medal 😂
  • 2
    @qbasic16 With one single regex only, no. With multiple subsequent regex, yes. The trick is to extract any embedded code, store those codes in an array and replace with some string like "{code}". Then, once you've parsed the remaining html, replace all those "{code}" with the elements in the array.
  • 1
    RegEx is great for parsing regular languages, which is why it's called "regular expression". However, people often use it to parse context-free languages, for which they're woefully inappropriate. That's where the saying comes from.

    Programming languages, markup languages, and any language which allows nesting definitions, are never regular languages.
Add Comment