Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
b0lt
Apr 29, 2005

Blotto Skorzany posted:

You can't just interpret any random integer value as an address without a cast in ANSI C, and even with a cast there are some caveats (cf. the C FAQ # 4.14 and 4.5). You can take the address of any normal int that you declare because every variable declared with automatic storage duration (a normally declared variable; don't worry too much about what 'storage duration' means at this point) can be treated as if it has an address somewhere, and in fact probably does live on the stack.

That FAQ is a bit outdated, you can assume that a pointer can roundtrip between an integer in every single non-joke architecture, and most of the jokes as well. (Specifically, any implementation that defines intptr_t). There are some additional edge cases around accessing the zero page (i.e. you essentially can't in C in a standards compliant way), but doing stuff with the zero page is mostly pointless in the modern era anyway.

Also, this isn't disagreeing with you, but a sufficiently smart compiler can avoid spilling a variable to the stack even if you take its address, as long as it can prove that the pointer you get from it never escapes.

b0lt fucked around with this message at 02:49 on Sep 9, 2015

Adbot
ADBOT LOVES YOU

Linear Zoetrope
Nov 28, 2011

A hero must cook
If you want to deref an int as if it were a pointer you'd have to do some nonsense like:

code:
int x = 1;
int y = **(int **)(void *)(&x); // interpret int x as a pointer to an int, then deref it

printf("%d", y);
The problem with interpreting an int as a pointer is more because of C's type system than pointers and ints being different. In-register integer arithmetic is how you do it in most (all?) assembly languages.

But you'll get a compiler warning and it's almost never a good idea. Best case it segfaults, worst case the entire program is a huge security bug of some sort. I'm sure there's some case where it may be useful, but the examples mostly consist of "C pub trivia" questions. Maybe you could do something of the sort with embedded devices, but if you're really dead set on accessing things via integer arithmetic you may as well bite the bullet and write that function in pure assembly at that point.

Linear Zoetrope fucked around with this message at 02:54 on Sep 9, 2015

Yaoi Gagarin
Feb 20, 2014

If you're on an embedded system you might want to do something like this:

code:
intptr_t address = 0xABC42 // address of memory-mapped IO register
volatile int *p = (int *) address;

void foo()
{
	*p = *p + 1;
}
That's probably the most common case of integer-to-pointer reinterpretation, when you know that you have some *thing* at a fixed, predetermined memory location.

And perhaps you'd want to manipulate a pointer in some way, in which case you'd do this:

code:
void *foo(void *p1)
{
	intptr_t address;
	address = (intptr_t) p1;
	... // mess with address
	return (void *) address;
}
An example of that case might be: given a pointer, get a pointer to the start of the memory page that includes the address the pointer points to.

Again, mostly useful on embedded systems where it may be important to work with where things actually are in memory.

b0lt
Apr 29, 2005

Jsor posted:

If you want to deref an int as if it were a pointer you'd have to do some nonsense like:

code:
int x = 1;
int y = **(int **)(void *)(&x); // interpret int x as a pointer to an int, then deref it

printf("%d", y);
The problem with interpreting an int as a pointer is more because of C's type system than pointers and ints being different. In-register integer arithmetic is how you do it in most (all?) assembly languages.

But you'll get a compiler warning and it's almost never a good idea. Best case it segfaults, worst case the entire program is a huge security bug of some sort. I'm sure there's some case where it may be useful, but the examples mostly consist of "C pub trivia" questions. Maybe you could do something of the sort with embedded devices, but if you're really dead set on accessing things via integer arithmetic you may as well bite the bullet and write that function in pure assembly at that point.

*(int*)(intptr_t)x is an easier method that's at least as standards compliant.

Another common idiom in C is to have an API along the lines of void do_something(callback_fn_t callback, void *data), where the callback function receives the data argument passed in earlier. It's not strictly compliant to cast a regular integer to a pointer and pass it in, but no compiler or platform would hate you enough to make it not work.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Pollyanna posted:

code:
cc -Wall -g    test.c   -o test
test.c:5:8: warning: incompatible integer to pointer conversion initializing 'int *' with
      an expression of type 'int' [-Wint-conversion]
  int *thing = 0x10;
       ^       ~~~~
1 warning generated.
0x7fff507d5438
which kind of makes sense, it's trying to give me the location of the value 16. How it gets that, I don't have a loving clue, but whatever. The other way around, though...

It's not. Much like you use int foo = 13; to have a variable containing the integer value 13, you say int *foo = 0x10 to have a variable containing the pointer value 0x10.

Pollyanna posted:

I don't quite understand why it doesn't compile. I expected to get the value of whatever was at memory address 0x01, which I expect to just be some random value. Instead, it's invalid. What exactly does *thing return here?

In the same way you can't do int foo = "bar"; because strings are not integers, you also can't do int *foo = 0x10; because C's int type is not the same as a pointer.

As for what's at C address 0x10, who knows! On most systems, it will crash. This is because we've collectively considered it useful behavior to have accessing a NULL pointer crash the program, the NULL pointer is equivalent to the memory address at 0x0 on most systems, and for reasons you'll learn about later, the CPU considers memory in chunks called "pages". So, if 0x0 will crash the program, so will 0x10.

That said, I have seen embedded systems where they're pushing the system to the limit, and they need the extra space in memory, so dereferencing 0x0 works fine. As for what points there, it's up to the system. Once you get a better handle on pointers, we can tell you about the difference between virtual address space, physical memory, and how one maps to the other.

feedmegin
Jul 30, 2008

Suspicious Dish posted:

That said, I have seen embedded systems where they're pushing the system to the limit, and they need the extra space in memory, so dereferencing 0x0 works fine.

Also really ancient Unices. Ultrix anyone?

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord
Pollyanna are you by chance studying C with "Learn C The Hard Way"?

ExcessBLarg!
Sep 1, 2001

Jsor posted:

Maybe you could do something of the sort with embedded devices, but if you're really dead set on accessing things via integer arithmetic you may as well bite the bullet and write that function in pure assembly at that point.
It's not an uncommon thing to do in low-level kernel code, particularly memory managers or anything interacting with an MMU. Yes, you could write these functions in assembly, but the whole purpose of C originally was to provide a higher-level structured language to replace assembly language (particularly in the UNIX kernel) for this very kind of code.

If you're writing applications code then doing integer arithmetic on pointers is quite likely out of scope and something you shouldn't do. I'd probably go further and say you simply shouldn't write applications in C. :v:

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer

ExcessBLarg! posted:

It's not an uncommon thing to do in low-level kernel code, particularly memory managers or anything interacting with an MMU. Yes, you could write these functions in assembly, but the whole purpose of C originally was to provide a higher-level structured language to replace assembly language (particularly in the UNIX kernel) for this very kind of code.

If you're writing applications code then doing integer arithmetic on pointers is quite likely out of scope and something you shouldn't do. I'd probably go further and say you simply shouldn't write applications in C. :v:

I agree wholeheartedly. After learning about how all of C's features are so awesome and magical for OSes, and trying to implement various bits and pieces of a Unix analogue, I don't think I'd ever dream of trying to do day-to-day software development in C. That pain is only worth it if you actually need to do things at a low level.

Shaocaholica
Oct 29, 2002

Fig. 5E
Is there an easy shell way (in linux) to compare 2 directories and copy the outlier files to a 3rd directory based on filename only. I know file contents will differ but I just need the outliers.

eg:

big_images/001.jpg
big_images/003.jpg

small_images/001.jpg
small_images/002.jpg <- copy/link this file to some other dir
small_images/003.jpg

I'm talking about ~150k files in each dir with about 5-10% outliers.

Pollyanna
Mar 5, 2005

Milk's on them.


Part of the reason I'm learning C is because I want to do things with some Arduinos and a Raspberry Pi I've got lying around, so why not dip my toe in the embedded domain while I'm at it?

Symbolic Butt posted:

Pollyanna are you by chance studying C with "Learn C The Hard Way"?

Yes I am! Holy loving jesus. At least Valgrind actually works on recent OSX versions now.

LeftistMuslimObama posted:

I agree wholeheartedly. After learning about how all of C's features are so awesome and magical for OSes, and trying to implement various bits and pieces of a Unix analogue, I don't think I'd ever dream of trying to do day-to-day software development in C. That pain is only worth it if you actually need to do things at a low level.

I read somewhere that larger, more abstract applications are best implemented with higher level languages like Ruby or Python. Languages like C are great for lower-level details like the smaller programs used to interface with things like network ports. The C programs are almost like implementation details.

That does make me wonder how you get a Ruby program to start up a C program, and write that program such that it could potentially be used by a Java program. Someone should make a common program communication interface, like the Unix pipe.

Nippashish
Nov 2, 2005

Let me see you dance!

Shaocaholica posted:

Is there an easy shell way (in linux) to compare 2 directories and copy the outlier files to a 3rd directory based on filename only. I know file contents will differ but I just need the outliers.

eg:

big_images/001.jpg
big_images/003.jpg

small_images/001.jpg
small_images/002.jpg <- copy/link this file to some other dir
small_images/003.jpg

I'm talking about ~150k files in each dir with about 5-10% outliers.

If your folders are "big" and "small" then
code:
sort <(ls big) <(ls small) | uniq -u
will give you the names of files that appear in exactly one of big or small (but won't tell you which folder they're in). Alternatively,
code:
comm -23 <(ls big) <(ls small)
will list files only in "big" and
code:
comm -13 <(ls big) <(ls small)
will list files only in "small". You can do things with the outlier files by piping the results of these commands into xargs, for example you can use
code:
comm -23 <(ls big) <(ls small) | xargs -I{} mv big/{} target/.
to move all of the files that only appear in "big" into a folder called "target".

csammis
Aug 26, 2003

Mental Institution

Pollyanna posted:

Someone should make a common program communication interface, like the Unix pipe.

Several dozen someones have! The concept you're getting at is called remote procedure call or, generically, inter-process communication.

ExcessBLarg!
Sep 1, 2001

Pollyanna posted:

I read somewhere that larger, more abstract applications are best implemented with higher level languages like Ruby or Python.
"Larger" applications are probably "best" implemented with Java or C# as those languages were designed to target the space of long-lived, maintainable, enterprise-class applications.

Ruby and Python are generally better suited for "programming in the small", prototyping, and building concise and functional applications quickly, but aren't necessarily geared towards long-term projects with many potential maintainers. It's exactly the features of Ruby and Python that make it wonderful for quick stuff (duck typing, general expressiveness, etc.) that are at odds with large applications (static typing, strictly defined interfaces, etc.). Of course, you could write applications of all sizes in either, and folks do use Java for prototyping or Ruby for large applications. But that's the target audience.

Pollyanna posted:

Languages like C are great for lower-level details like the smaller programs used to interface with things like network ports.
So C is a rather minimalist language. Compared to most modern languages, it's a relatively thin layer above assembly. That makes it flexible enough to write OS kernels, interface with hardware, implement language runtimes, and things like that. Historically C has also been used where performance is important, but not so important as to write in assembly. C's minimalism though, ability to manipulate raw memory, and trivial subversion of the type system makes it easy to introduce crash and security bugs into programs. These are things that are necessary when implementing an OS, but usually not when writing applications. Thus, it's a good idea these days to write applications in a "safe" language.

Networking code can be written in any language these days. At the application layer, C (e.g., POSIX libc)'s offering aren't very different from other languages. The Sockets API is also somewhat painful. Lower-level networking code (TCP/IP, Ethernet, etc.) is pretty much all written in C and done in the kernel.

Pollyanna posted:

That does make me wonder how you get a Ruby program to start up a C program, and write that program such that it could potentially be used by a Java program.
So, unlike C, Ruby code isn't compiled to machine language that can run "directly" on hardware. Instead it's runs in an interpreter (these days a bytecode-based virtual machine). The most common Ruby implementation has an interpreter/VM that's written In C. Thus, the interpreter itself provides an interface for Ruby code to interact with other C libraries, which is how many of the language features are provided. Another implementation, JRuby, is written in Java and runs on the Java virtual machine, which itself is written in C++.

Pollyanna posted:

Someone should make a common program communication interface, like the Unix pipe.
That's been done, many times before.

ExcessBLarg! fucked around with this message at 00:15 on Sep 10, 2015

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
If you give somebody a socket of bytes, they will invent a message passing scheme on top of it. The new hotness is protobuf. Excuse me, flatbuffers. Excuse me, cap'n'proto.

sarehu
Apr 20, 2007

(call/cc call/cc)
I'm pretty sure at my last job at some point we were sending JSON over Protobuf. I think now they're serializing protobufs to JSON.

fritz
Jul 26, 2003

feedmegin posted:

Ultrix anyone?

No thank you.

Shaocaholica
Oct 29, 2002

Fig. 5E

Nippashish posted:

If your folders are "big" and "small" then
code:
sort <(ls big) <(ls small) | uniq -u
will give you the names of files that appear in exactly one of big or small (but won't tell you which folder they're in). Alternatively,
code:
comm -23 <(ls big) <(ls small)
will list files only in "big" and
code:
comm -13 <(ls big) <(ls small)
will list files only in "small". You can do things with the outlier files by piping the results of these commands into xargs, for example you can use
code:
comm -23 <(ls big) <(ls small) | xargs -I{} mv big/{} target/.
to move all of the files that only appear in "big" into a folder called "target".

Thanks. Ended up using a hokey way and I forgot to mention I only needed the outliers in one direction (in small).

code:
diff -q big_images small_images | grep Only | sed "s/^Only in small_images: //" > list.txt
then just fed that into xargs ln. Did it in 2 steps just to be safe. It was decently fast but I'm not sure exactly what unnecessary computation diff is doing since I know files won't match if you hash/size them.

Ekster
Jul 18, 2013

ExcessBLarg! posted:

"Larger" applications are probably "best" implemented with Java or C# as those languages were designed to target the space of long-lived, maintainable, enterprise-class applications.

Ruby and Python are generally better suited for "programming in the small", prototyping, and building concise and functional applications quickly, but aren't necessarily geared towards long-term projects with many potential maintainers. It's exactly the features of Ruby and Python that make it wonderful for quick stuff (duck typing, general expressiveness, etc.) that are at odds with large applications (static typing, strictly defined interfaces, etc.). Of course, you could write applications of all sizes in either, and folks do use Java for prototyping or Ruby for large applications. But that's the target audience.

So C is a rather minimalist language. Compared to most modern languages, it's a relatively thin layer above assembly. That makes it flexible enough to write OS kernels, interface with hardware, implement language runtimes, and things like that. Historically C has also been used where performance is important, but not so important as to write in assembly. C's minimalism though, ability to manipulate raw memory, and trivial subversion of the type system makes it easy to introduce crash and security bugs into programs. These are things that are necessary when implementing an OS, but usually not when writing applications. Thus, it's a good idea these days to write applications in a "safe" language.

Networking code can be written in any language these days. At the application layer, C (e.g., POSIX libc)'s offering aren't very different from other languages. The Sockets API is also somewhat painful. Lower-level networking code (TCP/IP, Ethernet, etc.) is pretty much all written in C and done in the kernel.

So, unlike C, Ruby code isn't compiled to machine language that can run "directly" on hardware. Instead it's runs in an interpreter (these days a bytecode-based virtual machine). The most common Ruby implementation has an interpreter/VM that's written In C. Thus, the interpreter itself provides an interface for Ruby code to interact with other C libraries, which is how many of the language features are provided. Another implementation, JRuby, is written in Java and runs on the Java virtual machine, which itself is written in C++.

That's been done, many times before.

Thanks for this writeup. I've been learning how to program mostly in C for the past ~2 months because I want to learn more about how a computer actually works before using higher level features. Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library. I was skeptical at first about higher level languages but I now understand that the extra performance overhead can definitely be worth it depending on what you're trying to accomplish. Now I'm reading up on C# for when I want to do something that's hard to do in C.

My programmer friend jokingly calls me a masochist for leaning how to program at a (very) low level first but I'm a stubborn fool like that.

Linear Zoetrope
Nov 28, 2011

A hero must cook

Ekster posted:

Thanks for this writeup. I've been learning how to program mostly in C for the past ~2 months because I want to learn more about how a computer actually works before using higher level features. Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library. I was skeptical at first about higher level languages but I now understand that the extra performance overhead can definitely be worth it depending on what you're trying to accomplish. Now I'm reading up on C# for when I want to do something that's hard to do in C.

My programmer friend jokingly calls me a masochist for leaning how to program at a (very) low level first but I'm a stubborn fool like that.

You may be interested in NAND 2 Tetris which starts you off with a virtual hardware simulator, and has you build up from basic logic gates/hardware, to assembly, ultimately to a Java/C#-like language where you write Tetris.

Ekster
Jul 18, 2013

Jsor posted:

You may be interested in NAND 2 Tetris which starts you off with a virtual hardware simulator, and has you build up from basic logic gates/hardware, to assembly, ultimately to a Java/C#-like language where you write Tetris.

I'm actually following that course right now, the assembler I wrote was part of an exercise. It's really good and I highly recommend it.

I got side-tracked for a while when I needed to implement a hashmap in C but I've decided to continue the course in C# and learn how to implement algorithms/data structures later for now.

ExcessBLarg!
Sep 1, 2001

Ekster posted:

Although I don't regret doing that it was painful at times, like trying to write an assembler in C with the standard C library.
There's definitely value in writing something--perhaps a limited-purpose command line utility if not a kernel module--in C, to understand both the features and pitfalls of the lanuage. But yeah, doing any significant text processing in C is an absolute headache. Lex and Yacc help, but at some point it's time to move on.

Veskit
Mar 2, 2005

I love capitalism!! DM me for the best investing advice!
Hi everyone! First and foremost thank you for all of the help, it was much appreciated!!


Just a quick update I kind of figured out everything and stumbled on some good solutions. I remembered I had a contact over in SF and asked him what his group uses to program, and they use IntelliJ and the company gets a discount for *reasons*. I asked for pricing and I can get a license through the company for PyCharm, so I got a hold of the community edition, and through a series of fun events got someone to install the interpreter. If/when I design anything of actual value, I can show them what was created then get cost justification ez pz.


I'm going through treehouse lessons and Python is like incredibly intuitive and I love it! I can actually understand what I'm programming and I'm happy with that. So as frustrating as it was for all of you, thank you for the help.

JawKnee
Mar 24, 2007





You'll take the ride to leave this town along that yellow line
python is great, it's what I first learned in high school (well, beyond a little Qbasic) and also what the intro programming class at my uni used

sausage king of Chicago
Jun 13, 2001
I'm looking for a Geo Ip provider that will give me the company name a visitor to my site is coming from. Like, if someone who is working at IBM hits my site, I want to be able to know they are at IBM.

So far I've found this https://www.maxmind.com/en/geoip2-services-and-databases, which appears to do what I'm looking for ( but doesn't come up with my company when I browse from work, and I work for a mid-sized company), but was curious as to whether or not anyone knows of any alternatives?

dougdrums
Feb 25, 2005
CLIENT REQUESTED ELECTRONIC FUNDING RECEIPT (FUNDS NOW)
Could you use a high-volume whois service and parse out the orgname line? For example:

code:
user@hostname:~$ whois 129.42.38.1 | grep OrgName | awk '{split($0,s,":"); gsub(/^[ \t]+/,"",s[2]);  print s[2]}'
IBM Corporation
I think that might be the best you can get though, without manually updating the ranges, when I try the forums it doesn't work so well:
code:
user@hostname:~$ dig forums.somethingawful.com +short | xargs whois | grep OrgName | awk '{split($0,s,":"); gsub(/^[ \t]+/,"",s[2]);  print s[2]}'
Steadfast Networks

dougdrums fucked around with this message at 21:05 on Sep 10, 2015

sausage king of Chicago
Jun 13, 2001
I'm looking for more of something that maps network ips to company names. Like my example, I need to know if someone who works at IBM and is on their network is looking at my site, if that makes sense. I know Hubspot and Marketo offer something like this. I want to do what they are doing.

Centripetal Horse
Nov 22, 2009

Fuck money, get GBS

This could have bought you a half a tank of gas, lmfao -
Love, gromdul
Am I the only person who doesn't immediately see what gong on when I look at code like this?

code:
return --C<N ?[1+ C/c/r|0,c+~(C/c%r), r-1-C%c]: [-1,-1,-1]
My solution to this problem was almost twice as long, and included a for loop.

No Safe Word
Feb 26, 2005

Centripetal Horse posted:

Am I the only person who doesn't immediately see what gong on when I look at code like this?

code:
return --C<N ?[1+ C/c/r|0,c+~(C/c%r), r-1-C%c]: [-1,-1,-1]
My solution to this problem was almost twice as long, and included a for loop.

Nobody should be expected to immediately grasp it, that's code golf and should be refactored.

Fergus Mac Roich
Nov 5, 2008

Soiled Meat

Centripetal Horse posted:

Am I the only person who doesn't immediately see what gong on when I look at code like this?

code:
return --C<N ?[1+ C/c/r|0,c+~(C/c%r), r-1-C%c]: [-1,-1,-1]
My solution to this problem was almost twice as long, and included a for loop.

I don't know what language that is, but is that using a variable named C and another named c?

Sedro
Dec 31, 2008
I'm guessing JavaScript? based on the usage of bitwise-or and lack of semicolon

Linear Zoetrope
Nov 28, 2011

A hero must cook
I was thinking it couldn't be a bitwise or in whatever language it was, because (anything|0) is a no-op. (In fact, I think on some assemblers, the nop instruction is literally a macro for OR reg, 0).

dougdrums
Feb 25, 2005
CLIENT REQUESTED ELECTRONIC FUNDING RECEIPT (FUNDS NOW)

idontcare posted:

I'm looking for more of something that maps network ips to company names. Like my example, I need to know if someone who works at IBM and is on their network is looking at my site, if that makes sense. I know Hubspot and Marketo offer something like this. I want to do what they are doing.

I'm not sure I understand what you're looking for? The only conceivable way I can think of is to pull the data from a registry, because they're the only source for it as far as I know. I'm so stumped, I actually called Marketo (Hubspot gave me a machine) and asked your exact question and they told me they provide no such product. Looking at MaxMind again (https://www.maxmind.com/en/geoip2-isp-database), it looks like they do it nearly the same way.

Actually I think I might understand you, are you looking to see if a user's company is statistically identifiable by way of cookies and whatnot? I.E. user x has "works at IBM" on facebook and tweets about watson?

dougdrums fucked around with this message at 23:52 on Sep 10, 2015

Sedro
Dec 31, 2008

Jsor posted:

I was thinking it couldn't be a bitwise or in whatever language it was, because (anything|0) is a no-op. (In fact, I think on some assemblers, the nop instruction is literally a macro for OR reg, 0).
In JS you can use x | 0 and x >> 0 to C-style cast some number x to an integer

Linear Zoetrope
Nov 28, 2011

A hero must cook

Sedro posted:

In JS you can use x | 0 and x >> 0 to C-style cast some number x to an integer

Well that's... something

Centripetal Horse
Nov 22, 2009

Fuck money, get GBS

This could have bought you a half a tank of gas, lmfao -
Love, gromdul

No Safe Word posted:

Nobody should be expected to immediately grasp it, that's code golf and should be refactored.

So, maybe it's not just me? I'd really like for it to not be that I really am just that bad at reading and parsing code.


Fergus Mac Roich posted:

I don't know what language that is, but is that using a variable named C and another named c?

Sedro posted:

I'm guessing JavaScript? based on the usage of bitwise-or and lack of semicolon

It is Javascript. Yeah, that's two variables using the same letter. The idea was to write the code in as few characters as possible, and the concepts being represented by those variables both start with "c".


Sedro posted:

In JS you can use x | 0 and x >> 0 to C-style cast some number x to an integer

I haven't touched Javascript in years, but that was actually the easiest part for me to understand. "Oh, I see. That's just making sure it's an int."

Fergus Mac Roich
Nov 5, 2008

Soiled Meat
If you are asking if people can understand that by reading in the normal, natural way, no. But, not knowing JS, I'll claim that you should probably be able to figure out what it does with a close examination. I assume this is a game or contest.

JawnV6
Jul 4, 2004

So hot ...

Centripetal Horse posted:

I haven't touched Javascript in years, but that was actually the easiest part for me to understand. "Oh, I see. That's just making sure it's an int."

I thought the ternary operator was the easy bit, it's checking C against N and returning a fixed tuple if that fails or computing the real vector.

What's eating me is C being referenced in the same line as --C. What does a javascript call a sequence point?

Centripetal Horse
Nov 22, 2009

Fuck money, get GBS

This could have bought you a half a tank of gas, lmfao -
Love, gromdul

Fergus Mac Roich posted:

If you are asking if people can understand that by reading in the normal, natural way, no. But, not knowing JS, I'll claim that you should probably be able to figure out what it does with a close examination. I assume this is a game or contest.

I wasn't asking if I should be able to read it natural-language style. I think the number of people who can do that is quite small. I was able to figure out what it does. I can always figure out what other people's code is doing, but I sometimes wonder if it takes me longer than it should. I've worked with numerous coders who were so competent that it was almost frightening. I've written hundreds of thousands of lines of code across countless projects, all of which do what I want them to do, but I see poo poo like that snippet, and I think, "That didn't even occur to me, but I'll bet Bob would have thought of that."

The code in question was the answer to a challenge on http://www.codefights.com/


JawnV6 posted:

What's eating me is C being referenced in the same line as --C. What does a javascript call a sequence point?

I don't know, but C is definitely decremented before the ? : is evaluated. Also, I just tested, and it doesn't matter if the -- operator precedes or succeeds the variable.

You're right, the ternary operator was really the most obvious bit of that code. I wasn't including that, because it's super-simple, and I don't think anyone would consider that when judging how hard it is to evaluate that bit of code.

Adbot
ADBOT LOVES YOU

baka kaba
Jul 19, 2003

PLEASE ASK ME, THE SELF-PROFESSED NO #1 PAUL CATTERMOLE FAN IN THE SOMETHING AWFUL S-CLUB 7 MEGATHREAD, TO NAME A SINGLE SONG BY HIS EXCELLENT NU-METAL SIDE PROJECT, SKUA, AND IF I CAN'T PLEASE TELL ME TO
EAT SHIT

Centripetal Horse posted:

I wasn't asking if I should be able to read it natural-language style. I think the number of people who can do that is quite small. I was able to figure out what it does. I can always figure out what other people's code is doing, but I sometimes wonder if it takes me longer than it should. I've worked with numerous coders who were so competent that it was almost frightening. I've written hundreds of thousands of lines of code across countless projects, all of which do what I want them to do, but I see poo poo like that snippet, and I think, "That didn't even occur to me, but I'll bet Bob would have thought of that."

The code in question was the answer to a challenge on http://www.codefights.com/

Code golf is a special niche where you use all the language tricks and weird edge cases you've picked up to cram the functionality/behaviour you need into the fewest characters possible. Its goals are pretty much the opposite of being universally clear and readable, and it encourages the use of all kinds of esoteric hacks that the average person might be completely unaware of

I mean there's nothing wrong with that as a challenge, but I wouldn't worry if you can't unpack something so condensed and potentially weird. There's a reason we don't write real code like that, and why clear behaviour is preferred over clever fuckery, or the fuckery is explained in documentation if there's an actual benefit to it being there

You'll get better at reading this kind of thing as you get more familiar with it, either through learning the language and its toolbox or from playing code golf yourself (and probably more from the latter). I guess the more important question is, once you unpack what the hell is going on in there with a bit of whitespace and readable names, does it make sense? Do you understand how it works and what tricks are being used? Or if you look up the tricks, do you feel like you should have got it, or do you go '...the hell is that about?'

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply