×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

Before you choose to head back to the Classic look of the site, we'd appreciate it if you share your thoughts on the Beta; your feedback is what drives our ongoing development.

Beta is different and we value you taking the time to try it out. Please take a look at the changes we've made in Beta and  learn more about it. Thanks for reading, and for making the site better!

Google Unveils Code Search

kdawson posted more than 7 years ago | from the let-the-regexp-begin dept.

212

derek_farn writes, "Google now has a page that supports source code searching. I hope they extend it to be more programming-language aware (e.g., search for identifiers and functions) like the specialist code search sites (Krugle, Koders, and Codease), who probably now have very worried investors. I don't see any option to search for Cobol. I guess there is not a lot of Cobol source available on the Internet, even although there is supposed to be more Cobol source in existence than any other language (perhaps that statement is not true in the noughties)." From the Cnet.com article: "Google engineers, many of whom participate in open-source projects, already use these code searching capabilities internally. Since it is a Google Labs project, the company is not yet seeking to monetize searches through ads."

cancel ×
This is a preview of your comment

No Comment Title Entered

Anonymous Coward 1 minute ago

No Comment Entered

212 comments

Re:the one thing everyone will search (3, Funny)

kripkenstein (913150) | more than 7 years ago | (#16319479)

A search for "bill gates" [google.com] turned up these tidbits:

if (gates == NULL) /* eat this, Billy-boy */
gates = (HINF **) Hmalloc (sizeof (HINF *) * 10);


int_fast16_t reserved1; /* Ask Bill Gates what this is all about. */

Re:the one thing everyone will search (1)

kjart (941720) | more than 7 years ago | (#16319639)

My first thought was this [google.com] . I didn't think anyone actually used those in real life.

Re:the one thing everyone will search (1)

Neil Hodges (960909) | more than 7 years ago | (#16320423)

Just like Google; search for any one (or sometimes more) word and they'll have something.

Re:the one thing everyone will search (0)

Anonymous Coward | more than 7 years ago | (#16319825)

Where ever you find wankers [google.com] , you find wankstains [google.com] .

How childish are we?

Re:the one thing everyone will search (2, Funny)

mr_tommy (619972) | more than 7 years ago | (#16319889)

Hilarious- the pains that writers have to code for browsers!

- Calendar.continuation_for_the_fucking_khtml_browse r = function() {
// check for function objects (as usual, IE is fucked up)

Re:the one thing everyone will search (0)

Anonymous Coward | more than 7 years ago | (#16320017)

I liked this one from kawa/gnu/expr/LocalDeclaration.java. It so... zen.

import gnu.mapping.Binding;
import kawa.standard.Scheme; // fuck

Re:the one thing everyone will search (1)

Barryke (772876) | more than 7 years ago | (#16320809)

Hilarious- the pains that writers have to code for browsers!
I like this one: IE_IS_A_FUCKING_SHIT = " unselectable='on' style='height=1px'>";

Re:the one thing everyone will search (4, Informative)

Anonymous Coward | more than 7 years ago | (#16320027)

You can also find some unfunny stuff too!

This could be abused by email spammers.
Search for: http://www.google.com/codesearch?q=%40 [google.com]

This reveals a lot of email addresses in code.
It's giving the spammers email addresses on a plate. (within a geek targeted audience)
That's definitely not funny but scary.

Re:the one thing everyone will search (0, Offtopic)

dema (103780) | more than 7 years ago | (#16320133)

Hahaha I was just about to post this. Fuck you!

Re:the one thing everyone will search (1)

SnprBoB86 (576143) | more than 7 years ago | (#16320305)

This is BEGGING for a competition! Google, I have a feature request: group-by project and occurance counts.

There must be one project with a bigger potty mouth than all the rest.

Re:the one thing everyone will search (0)

Anonymous Coward | more than 7 years ago | (#16320469)

I especially like this [google.com] one!

Another thing a *few* will search for... (0)

Anonymous Coward | more than 7 years ago | (#16320815)

If you are going to search for that you might as well search for this [google.com] .

COBOL hasn't been topdog for a while (2, Interesting)

BadAnalogyGuy (945258) | more than 7 years ago | (#16319491)

VB has been the language with the most LOC since the early-mid 90's.

As scary as that sounds.

Re:COBOL hasn't been topdog for a while (4, Insightful)

doti (966971) | more than 7 years ago | (#16319787)

It's true that not a lot of people write COBOL today, but the submiter was talking about legacy code. No wonder they're not on the Internet: not only they are from a pre-Internet era, but the vast majority of it is from corporations that keep their code very closed.

Re:COBOL hasn't been topdog for a while (0, Redundant)

BadAnalogyGuy (945258) | more than 7 years ago | (#16319871)

Total lines of code evar champion: VB.

It passed "legacy language" COBOL back in the 90's.

Re:COBOL hasn't been topdog for a while (0)

Anonymous Coward | more than 7 years ago | (#16320207)

"evar" is not evidence. Prove your statement or shut up.

Re:COBOL hasn't been topdog for a while (2, Interesting)

plopez (54068) | more than 7 years ago | (#16320871)

Well... if we want to quibble... :)

Basic was intended as a teaching language and so the language incorporated lots of syntax and ideas from the 2 major languages of the time: COBOL and FORTRAN.

BASIC eventually begat that idiot bastard child 'Visual Basic' and syntacticly hasn't changed much since. So you could say that there is a lot of COBOL in 'Visual Basic'. Sure, it became object focused and now OO, but it still resembles COBOL. So COBOL lives on, as Visual Basic. It will not die :)

And while I am on the soap box :) .....

So much of the code I have seen is just COBOL in drag. Java, VB, C++, C# etc. just COBOL programs in drag. Most people have not really changed their mindset since COBOL.

Case in point:
I showed my boss who is supposed to be an OO programmer how to abstract out a data access layer so that the layer knows nothing about the request sent to it or the exact DB structure and the requesting class knew nothing of the database. I used a few of the nicer features of C# and .Net.

My boss, supposedly a programmer didn't 'get it'. The upshot beign we ended up writing one class per DB table (as opposed to one table manager class total) and the results could have been quicker and easier in COBOL.

I will now step down off the soap box.

http://koders.com/ (0, Redundant)

Anonymous Coward | more than 7 years ago | (#16319499)

not to undermine the Google's work but in the meantime you may as well try this one

http://koders.com/ [koders.com]

Re:http://koders.com/ (1)

kafka47 (801886) | more than 7 years ago | (#16319559)

I would have tried it anyhow, considering that it was cited in the article summary.

(Sorry couldn't resist) :-)

/K

Re:http://koders.com/ (0)

Anonymous Coward | more than 7 years ago | (#16319629)

...one step further i didn't even RTFSummary

Re:http://koders.com/ (1)

tcopeland (32225) | more than 7 years ago | (#16319993)

The folks at Koders have been nice enough to give RubyForge [rubyforge.org] a little Javascripty thing that integrates nicely [rubyforge.org] into the regular GForge user interface so that folks can search the RubyForge CVS repositories. I've talked with them a couple of times and they seem like nice folks.

Restricted Use Due to Copyrights (5, Interesting)

eldavojohn (898314) | more than 7 years ago | (#16319507)

I made a simple search for "fade file:.js" in order to find a javascript function that would fade a div or table or anything really (I know scriptaculous [aculo.us] offers this already, just curious as to what's out there). I found something but the header of the file read:

All Code herein is Copyright 2005 Match.com
Do not copy, reproduce, reuse or sell any code herein
without the express, written consent of Match.com.
For information contact webmaster@match.com.
All Rights Reserved.

Which is expected. However, that means this tool isn't useful for finding a method or function or class I can use and then using it ... it seems to be restricted to one of two uses. If I'm looking for code that does natural language parsing, I could hope a comment somewhere contains NLP as a description of what's going on. Or, I could look for libraries out there with methods and then search for those methods to see how other people used them to get an idea of how they work. The vast majority of this code seems to be just web development front-end code at least from the few searches I've done. Too bad, that's a very small part of programming.

Re:Restricted Use Due to Copyrights (1)

Aladrin (926209) | more than 7 years ago | (#16319591)

You could always just search for code under the license you want. Instead of all code.

Re:Restricted Use Due to Copyrights (1)

doti (966971) | more than 7 years ago | (#16319863)

But wait, aren't all those licenses listed in the combobox free (as in open source) licenses?

Re:Restricted Use Due to Copyrights (1)

doti (966971) | more than 7 years ago | (#16319837)

But you can study the code, and try to apply the general idea to your own code.

Re:Restricted Use Due to Copyrights (1)

CastrTroy (595695) | more than 7 years ago | (#16320265)

I'm not sure how well licenses/copyright work on Javascript. Anything that you release in Javascript on the internet is basically open source. Sure you can tell people not to use it, but it's there for them to read, and use your ideas to make their own software. If they want to make something similar, they can change it just enough to look different, or they can take single functions, which aren't really that complicated, and change them just a little to look like their own code that was developed without even looking at the other code. That's why i don't get all these companies who place massive amounts of web application logic into Javascript. They are basically telling all their competitors exactly how their applications work.

Thankfully... (1)

nate nice (672391) | more than 7 years ago | (#16319531)

...I can forget RegEx's now.

But honestly, this might have some bells and whistles but I don't see myself getting rid of my regular expression searches any time soon.

Non-alphanumerics at last! (4, Informative)

maccallr (240314) | more than 7 years ago | (#16319563)

At last we can use regexps and search on all the important characters between the alphanumerics! For example the prefixed '@' in PHP - very hard to figure out what this is, without reading the reference cover to cover. Now at least we can search the codebase and hope to see some useful comments preceding it, or figure out from context what's going on.

e.g. "@fopen file:.php"

Re:Non-alphanumerics at last! (2, Informative)

serialdogma (883470) | more than 7 years ago | (#16319763)

The @ prefix in PHP just stops it from printing an error message if something goes tits-up.

Re:Non-alphanumerics at last! (2, Informative)

fruey (563914) | more than 7 years ago | (#16319855)

Not only stops it from printing an error, but ignores the error and carries on parsing the rest of the code.

Useful for including a file that might not be there, for example...

Re:Non-alphanumerics at last! (0)

Anonymous Coward | more than 7 years ago | (#16319919)

What, like "@echo off"?
(no, I'm not admitting to knowing that).

Re:Non-alphanumerics at last! (1)

CastrTroy (595695) | more than 7 years ago | (#16320325)

But like the original poster stated, it's not very obvious what it does from just looking at it, without comments stating that we are ignoring any errors produced. I mean, it's basically a hack so you don't have to write a try-catch with an empty catch statement. It's the PHP equivalent of VB's On Error GOTO Next. Why would you write code that's that unclear, when you have another way to write the code that makes it much more clear of what's actually going on? Why would you write code that just ignores errors, and does nothing about them, assuming your application won't crash later on because the error occured? I'm sure all the PHP gurus understand what it's for and all it's implications, but somebody new to the language might miss something like that when looking at code. I don't think that ignoring errors is something that good developers do.

Re:Non-alphanumerics at last! (1)

Dragonslicer (991472) | more than 7 years ago | (#16320507)

Just to clarify, I think you meant "executing" instead of "parsing". If there's a parse error in the file, none of it is ever executed, including any execution-time changes you make to PHP's error handling. Your example is also a terrible use of error suppression. Most of the time's I've seen people use @ for error suppression have been because the programmer is too lazy, stupid, or ignorant to check for expected failures. In your example, using file_exists() before trying to include the file would be much better, since you won't end up with problems that are difficult to find because you've hidden the error message. The only legitimate uses for error suppression I've found are for functions that generate error messages even on expected failures, with no possibility of knowing ahead of time that the function will fail (socket connection timing out is the first example that comes to mind).

Re:Non-alphanumerics at last! (1)

fruey (563914) | more than 7 years ago | (#16320751)

From the PHP manual entry on including files [php.net] (my example is too simple, and in fact this is where it came from):

My experience is you can use an if-statement to verify if the script was included (I havn't tested this on remote includes, there might be non-standard-404 pages that makes it impossible to verify you got the right page)
Example:
// ignore the notice and evaluate the return value of the script, if any.
if(@include(dirname(__FILE__)."/foo.php"))
echo "foo.php included";
else
echo "failed to include foo.php";

So if you need to see if the include might have worked (and not forgetting cases where testing if a file exists doesn't mean it can be included) then this type of construct might be useful. I use it to try to include a specific header file based on URL attributes, otherwise include a generic replacement file.

You are of course correct in saying that code should try not to be lazy, and ignoring errors is a Bad Thing. Sometimes you can find use for that, and in my case above I hope this is OK (feel free to post an alternative, I'd be happy to include it in my code).

You are also correct that parsing was the wrong term. The syntax is parsed, and then the execution begins which can throw errors. So it's not a question of parsing. My apologies.

I like this search better - fucking funny as (0)

Anonymous Coward | more than 7 years ago | (#16319567)

Re:I like this search better - fucking funny as (3, Funny)

kafka47 (801886) | more than 7 years ago | (#16319631)

LOL your search has now uncovered the hidden angst of a million programmers :

($backend && $backend->isa("Acme::Scurvy::Whoreson::BilgeRat::B ackend::$params{language}")) ||
die("For fuck's sake, the fucking backend's fucked");

/K

Re:I like this search better - fucking funny as (1)

radarsat1 (786772) | more than 7 years ago | (#16320297)

For some giggles, I highly recommended the following: /usr/src/linux# grep -iR fuck .

How many (1)

jfinke (68409) | more than 7 years ago | (#16319585)

Open Source projects that you know are written in COBOL? I don't know of any. It is a lot of legacy code. There is very few new projects being started in COBOL.

Awesome! (1)

MrP-(at work) (839979) | more than 7 years ago | (#16319599)

This is pretty cool.. i hate trying to search code on normal google, it usually filters out most of the search characters and you end up with nothing useful.

Now if only they'd add regex searching to normal google (unless it already has it and i'm missing it?)

mmmm regex

Useful to whom? (4, Interesting)

kjart (941720) | more than 7 years ago | (#16319603)

Whenever I search for something code related on the web it's usually because I want to know how to do something. In such cases I dont really know what the code itself would be (i.e. the reason why I'm searching) so this wouldn't help at all. I suppose if you were looking for specific code it could be useful, but why would you be doing that? That would likely be your own code, so wouldn't a simple grep be easier?

I'm sure I'm missing something here - Google doesn't (usually) release useless new products :)

"Given enough eyeballs... (3, Informative)

bloblu (891170) | more than 7 years ago | (#16319621)

all bugs are shallow."

Well, it looks like that's not really the case: http://www.google.com/codesearch?hl=en&lr=&q=++%5C sif%5C(%5B%5E)%5D*%5C)%3B+license%3Agpl+lang%3Ac%2 B%2B&btnG=Search [google.com]

I hope this service will help improve code quality...

Re:"Given enough eyeballs... (1)

doti (966971) | more than 7 years ago | (#16319931)

That's surprising to me.
I never made that typo, and never saw it on other's code.

Impressive.

No need to search for Cobol? (5, Funny)

krell (896769) | more than 7 years ago | (#16319643)

"I don't see any option to search for Cobol."

Well, that's one entire season of "Battlestar Galactica" rendered entirely pointless. Thanks a lot!

uses some form of regexp (1)

cyclomedia (882859) | more than 7 years ago | (#16319647)

i sign my code changes in projects with "// cyc" or "// ric".. so naturally i tried those (and came up with sod all, naturally)... but the latter will also return

"// a long bit of text written by eric smith "

searching for

"//.ric"

on the other hand gets closer matches to what i intended

A good start.. (5, Insightful)

sfraggle (212671) | more than 7 years ago | (#16319665)

It's a good start. They really need to start searching Subversion/CVS repositories as well. One of the most obvious things that they seem to have missed is to index all the Sourceforge downloads.

Re:A good start.. (3, Informative)

Slurfer (235512) | more than 7 years ago | (#16319827)

According to the help / FAQ page, they are searching cvs & subversion repositories.

Wow what a crappy summary (0, Offtopic)

p3d0 (42270) | more than 7 years ago | (#16319671)

I would just like to burn a few karma to say that is the most disjointed, stream-of-consciousness summary I have seen in some time. There's one tiny little terse sentence announding the existence of the new search functionality, and somehow it manages do do so without even what kind of "code" we're talking about. Then the rants begin. The remainder of the paragraph has an average of one parenthetical remark per sentence, including two in a single sentence; and fully two-thirds of the rant is complaining about the lack of Cobol, of all things.
Phew, I feel better.

Re:Wow what a crappy summary (1)

kafka47 (801886) | more than 7 years ago | (#16319877)

Sorta like your post.

/K

Re:Wow what a crappy summary (1)

jlarocco (851450) | more than 7 years ago | (#16320209)

What the hell are you talking about? The GP is right, that summary sucked. If I wanted to suffer through reading summaries written by 12 year olds, I would just read digg.

Not that useful (2, Interesting)

fellm (1009547) | more than 7 years ago | (#16319673)

As a programmer who needs to solve a problem I need a place to find answers to the problem I am solving. Searching for a code won't do it because I am looking for an answer and not how to code it. To find answers I use Omgili [omgili.com] - it is a vertical search engine that search ten of thousands of forums and millions of discussions. Usually someone already asked my question and hopefully it has an answer. It is highly recommended for troubleshooting and specific problems/questions.

Re:Not that useful (1)

19thNervousBreakdown (768619) | more than 7 years ago | (#16320433)

Thanks! I've already found that searching newsgroups usually finds better results than a web search, and have always wished forums were searched better. I'll check this out, let's hope it does better than Google.

security hole search (5, Insightful)

Sub Zero 992 (947972) | more than 7 years ago | (#16319697)

How to find security holes in PHP web applications:

http://www.google.com/codesearch?hl=en&lr=&q=Where +%5C%24_POST+-addslashes+lang%3Aphp [google.com]

Bad call, Google. (1)

captnitro (160231) | more than 7 years ago | (#16320475)

This entire project is either a very good or very bad idea.

(1) Automated searching for security vulnerabilities.
(2) A lot of that code is copyrighted. Which yes, it's transmitted over the Intarwebs regularly, but now it's just a little easier.

I'm not saying it's not a *cool* idea, but from the looks of the Slashdotters trying out this new power, I'm not sure Google thought this all the way through. (1) is great when your code runs a web service and nobody sees it but your team or organization. (2) I can foresee a lot of CS kids using this to find that magic snippet to complete Problem Set 13, and/or a lot of copyrighted code finding its way into private projects.

Just sayin'.

What, no Haskell? (0, Offtopic)

DieNadel (550271) | more than 7 years ago | (#16319727)

This functional language [haskell.org] is so interesting, there should be more talk about it.

Re:What, no Haskell? (1)

doti (966971) | more than 7 years ago | (#16319977)

Haskell is nice indeed. I have read about it, but not used it yet.

They also missed D [digitalmars.com] , which I hope will became more popular.

SCO to sue Gooooogle? (0)

Anonymous Coward | more than 7 years ago | (#16319731)

Will SCO be suing as their "copyrighted" code is now going to be available via Goooogle?

Re:SCO to sue Gooooogle? (1)

tomhudson (43916) | more than 7 years ago | (#16319823)

Maybe SCO can use it to find the millions of lines of misappropriated code that "got lost" in Blepp's suitcase?

The old saying is true . . . (4, Funny)

scottennis (225462) | more than 7 years ago | (#16319755)

Good programmers write good code. Great programmers find it on Google!

Re:The old saying is true . . . (1)

CastrTroy (595695) | more than 7 years ago | (#16320399)

This really is kind of true. Most of the people I know use Google Groups as a way to find a lot of code. There's no point spending a hour trying to write something, or figure out some obscure feature of your programming language when you can just search Google Groups and find the answer in 5 minutes.

Your Search (3, Funny)

mazarin5 (309432) | more than 7 years ago | (#16319781)

Your search 10 print "boobs" 20 goto 10 returned no results. Try searching again using fewer terms.

Bad moderation (Re:Your Search) (0)

Anonymous Coward | more than 7 years ago | (#16320623)

That was funny, not troll-y.

Google is not the first to do this (0, Redundant)

Aditi.Tuteja (1004231) | more than 7 years ago | (#16319789)

Google is not the first to unveil a code search service. Previously, Famous book publisher O'Reilly launched a beta of its Code Search service last month that allows users to search a combined 2.6 million lines of code published in its books.

Re:Google is not the first to do this (1)

Slurfer (235512) | more than 7 years ago | (#16320363)

And presumably the three sites that the article's summary links to also provided a similar service, prior to Google's announcement....

I'm obviously in the minority since I bothered to read the whole summary. Its not easy to read admittedly, but the links are relativly easy to spot ;-)

Some advice (1)

kafka47 (801886) | more than 7 years ago | (#16319797)

For online services : Don't put up code that states explicitly, not for production [google.com] .

For users : Stay away from online services that put up code that states "not for production". :-)

cpan.org
twiki.org
osuosl.org
...

/K

Re:Some advice (1)

syrion (744778) | more than 7 years ago | (#16320229)

Well, CPAN is very useful. You just check the code you're intending to include in your program to see if it says something like "this code is beta!" before you actually, you know, include it. There are a great number of "experimental" and "beta" perl modules on there--some, like B::CC, are mentioned in Programming Perl--but there are also some very useful, mature modules. Don't warn people off it; it's one of perl's best features.

Re:Some advice (1)

kafka47 (801886) | more than 7 years ago | (#16320303)

I think I was sort of going for humour, but I can see how it might have been misconstrued.

Perl on the other hand. Now that is funny! *duck* :-)

/K

Doesn't seem to work too well (1)

Salsaman (141471) | more than 7 years ago | (#16319833)

I searched for some of my own code from sourceforge CVS, and it couldn't locate it.

Re:Doesn't seem to work too well (1)

dstrupl (588119) | more than 7 years ago | (#16320881)

I have to agree. I have tried to search for my name in some files. Found it in one and not in another one in the same folder (?). Also they don't recognize the license. They still need to work on this IMHO.

Google Unveils Search Code (1)

Marvin01 (909379) | more than 7 years ago | (#16319925)

Ok, I read that headline wrong and though that Google had finally lost its mind completely...

http://www.omgili.com - is much more useful (1)

spale (1009629) | more than 7 years ago | (#16320029)

Anyone who is a coder knows that when it comes to solving a problem looking through someone else's code without knowing what to look for won't help you. Omgili [omgili.com] is a discussion search engine that covers millions of questions and answers. Whenever I have a question about how to do something I search Omgili [omgili.com] for answers - very good search engine!

Well this answered a question for me... (1)

jimstapleton (999106) | more than 7 years ago | (#16320069)

I know I'm not alone in my most hated language [google.com]

Re:Well this answered a question for me... (2, Interesting)

Roger_Wilco (138600) | more than 7 years ago | (#16320427)

I did a brief survey on "I hate [X]", and got the following:

perl 9
java 20
c 8000
c++ 11
c# 1
lisp 0
scheme 0
elisp 0
fortran 3

Looks like John McCarthy [paulgraham.com] wins.

Re:Well this answered a question for me... (1)

bogado (25959) | more than 7 years ago | (#16320757)

I hate perl : 9 results
I hate C++ : 11 results
I hate C : about 8000 results (?)
I hate Python : 7 results

It seems that the most hated language is 'C'. Well at least by the people who force them self to use it, witch may be a good measure...

Optimized away (1)

Waffle Iron (339739) | more than 7 years ago | (#16320221)

I guess there is not a lot of Cobol source available on the Internet, even although there is supposed to be more Cobol source in existence than any other language

Well, once the search database factored out all of the boilerplate declarations, there was pretty much nothing left to store.

mo3 do3n (-1, Troll)

Anonymous Coward | more than 7 years ago | (#16320273)

= 36400 FREEBSD on slashdot.org '*BSD Sux0rs'. ThIs OF AMERICA i8c Of events today, performing.' Even about half of the believe their already aware, *BSD The next round of

Thank goodness the SCO trial is almost over (1)

acomj (20611) | more than 7 years ago | (#16320285)

If sco had this to search through linux.. Just imagine..

SCO: You used our patented while loop over 4000 times in our code...

Major flaw in the Basic category (1)

RingDev (879105) | more than 7 years ago | (#16320323)

The search apparently lumps everything with the word "basic" in the title together. QBasic, Visual Basic, VB.Net, etc...

While the non-visual and Visual Basic merges aren't that bad, putting VB.Net into that category is a major headache. VB.Net is syntactically similar to VB6, but is fully object oriented and is coded in just like C#. So looking for VB.Net samples in the Basic category returns a lot of VB6 code solutions that may look syntactically correct, but are far from the best practices.

-Rick

I found orgasm code! (3, Funny)

Jugalator (259273) | more than 7 years ago | (#16320595)

This is just too funny :-)

void Mammal::mate( Mammal& partner ) { /* potential mating partner */
        M_partner = partner.getId(); /*printf( "." ); fflush( stdout );/**/ /* mating must be mutual */
        if( partner.getPartnerId() != M_id ) { /*M_wait += 15;/**/
                return;
        } /*printf( "+" ); fflush( stdout );/**/ /* this is male object */
        if( M_gender == 0 ) { /* perform breeding in female object */
                partner.mate( *this );
                return;
        } /* this is female object */
        assert( M_gender == 1 ); /* current position */
        int x = M_x, y = M_y; /* behind position */
        switch( M_direction ) {
                case EAST: x--; break;
                case NORTH: y++; break;
                case WEST: x++; break;
                case SOUTH: y--; break;
        } /* back to field wall */
        if( !M_field->in( x, y ) ) return; /* newborn's position */
        int cx = M_x, cy = M_y; /* move mother backward */
        M_x = x;
        M_y = y; /* conception */
        orgasm();
        partner.orgasm();
        Mammal* child;
        child = new Mammal( *M_field, cx, cy, NEWBORNENERGY, *this, partner ); /*printf( "CHILD%d ", child );/**/ /* birth */
        M_energy -= CHILDBIRTHENERGY;
        M_population->add( *child );

        printf( "MATE(%d,%d)->%d(%d) ", M_id, partner.getId(), child->getId(), child->getGeneration() ); /* partner.printGenotype();
        partner.printState();
        printGenotype();
        printState();
        child->printGenotype();
        child->printState();/**/
}

void Mammal::orgasm() {
        M_energy -= MATINGENERGY;
        M_result = 1;
}
Load More Comments
Slashdot Account

Need an Account?

Forgot your password?

Don't worry, we never post anything without your permission.

Submission Text Formatting Tips

We support a small subset of HTML, namely these tags:

  • b
  • i
  • p
  • br
  • a
  • ol
  • ul
  • li
  • dl
  • dt
  • dd
  • em
  • strong
  • tt
  • blockquote
  • div
  • quote
  • ecode

"ecode" can be used for code snippets, for example:

<ecode>    while(1) { do_something(); } </ecode>
Sign up for Slashdot Newsletters
Create a Slashdot Account

Loading...